linux - Check ranges with Bash using regex -
i've made helper function determine if specified length generate password valid. how can translate if conditional regex check ranges of 8-64?
# check see if password length valid # @param $1 length specified user # @return true (0) if not valid, false (1) if we're okay # usage: if is_valid "$length" is_valid() { local length="$1" if (( "$length" < 8 || "$length" > 64 )); return 0 else return 1 fi }
you can use normal pattern matching instead of regex:
function is_valid () { [[ $1 == @([8-9]|[1-5][0-9]|[6][0-4]) ]] } regex similar:
function is_valid () { [[ $1 =~ ^([8-9]|[1-5][0-9]|[6][0-4])$ ]] } i not find of them more readable plain
function is_valid () { (( 8 <= $1 && $1 <= 64 )) } note if , return not needed.
Comments
Post a Comment