2

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).

1
8
if [[ $1 = "-s" ]] && [[ $2 -ge 0 ]] && [[ $2 -le 9 ]]

-ge: greater-than-or-equal

-le: less-than-or-equal

6

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.

3
  • 2
    You don't need the regex match operator to test if a variable consists of a single digit: [[ "$1" = "-s" && "$2" = [0-9] ]]. You don't need the regex match operator to test if a variable consists solely of digits: [[ "$1" = "-s" && "$2" != *[!0-9]* && "$2" = [1-9]* ]] (here I also reject leading zeros and empty strings). Jan 9 '16 at 19:50
  • 1
    Why are you breaking out the regexes for integer comparisons? Jan 9 '16 at 23:32
  • @BlacklightShining for no good reason. Simply because that was the first approach that occurred to me and because I like regexes. Still, fair enough, answer edited.
    – terdon
    Jan 10 '16 at 0:14
1
[ "${#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

Not the answer you're looking for? Browse other questions tagged or ask your own question.