I have this code:
if [[ $1 = "-s" ]] && [[ $2 = 0-9 ]]
then
echo "yes"
fi
0-9 does not work for me. What I really want is to type, for example, -x 3 (or any number).
I have this code:
if [[ $1 = "-s" ]] && [[ $2 = 0-9 ]]
then
echo "yes"
fi
0-9 does not work for me. What I really want is to type, for example, -x 3 (or any number).
if [[ $1 = "-s" ]] && [[ $2 -ge 0 ]] && [[ $2 -le 9 ]]
-ge: greater-than-or-equal
-le: less-than-or-equal
This is possible, but you need to use a character class ([0-9]).For example:
if [[ "$1" = "-s" ]] && [[ "$2" = [0-9] ]]
then
echo "yes"
fi
However, the above is only true of $2 consists of a single digit. If you want $2 to consist of one or more digits, use:
if [[ "$1" = "-s" ]] && [[ "$2" != *[!0-9]* && "$2" = [1-9]* ]]
then
echo "yes"
fi
In newer bash versions, you can also use regular expressions:
if [[ "$1" = "-s" ]] && [[ "$2" =~ ^[0-9]+$ ]]
then
echo "yes"
fi
The ^ matches the start of the string, and $ the end. The + means "0 or more". So, ^[0-9]*$ will only be true of the string consists of nothing but one or more digits from the beginning until its end.
[ "${#1}${1#-s}" = "$((${2%%*[!0-9]*}0?2:-1))" ] &&
echo yes
...if you balance the comparison you can sometimes shorten the test.
I prefer case though:
case ${1#-s}:$2 in
(*:*[!0-9]*|*:) ;;
("${1:+:$2}") echo yes.
esac
Basically the idea is to rule out any match for a non-digit. For example:
[ "${2:+1$2}" = "1${2##*[!0-9]*}" ] &&
echo '"$2"' contains at least one character which \
is a digit and zero which are not digits.
It's much more simple with case because you have multiple branches.
case $1:$2 in
(*:*?"$2")
echo '"$1"' contains a "':'";;
(*:*[!0-9]*|*:0*[89]*|*:)
echo '"$2"' either contains a not-digit character, no characters, \
or is an invalid octal constant - or perhaps a mix-and-match;;
([!-]*|?[!s]*|??[!:]*)
echo '"$1"' is either empty, doesn\'t match '-s' in its first two characters, \
or is more than two characters. Or perhaps a mix-and-match.;;
(*) echo there are no more possibilities other than the one you want.
echo but you can get here a lot sooner, as demonstrated above.
esac