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

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
user150802
  • 21
  • 1

3 Answers3

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

-ge: greater-than-or-equal

-le: less-than-or-equal

Cyrus
  • 12,059
  • 3
  • 29
  • 53
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.

terdon
  • 234,489
  • 66
  • 447
  • 667
  • 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). – Gilles 'SO- stop being evil' Jan 09 '16 at 19:50
  • 1
    Why are you breaking out the regexes for integer comparisons? – Blacklight Shining Jan 09 '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 00: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
mikeserv
  • 57,448
  • 9
  • 113
  • 229