-1

So I am pretty familiar with bash but the second if statement keeps throwing me an error.

./run.sh: line 39: [: q: integer expression expected
./run.sh: line 39: [: q: integer expression expected

I am not exactly sure what is the problem. I am pretty sure my syntax is correct.

read -p "Option Number-> " answer
#  Check to see if the answer is only letters
if [[ "$answer" =~ ^[a-zA-Z]+$ ]];then
    if [ "$answer" -eq "q" ] || [ "$answer" -eq "Q" ];then
        exit
    fi
j jones
  • 1
  • 1
  • 1
    Add a shebang and then paste your script there: http://www.shellcheck.net/ – Cyrus Feb 04 '19 at 22:52
  • [How can I compare a variable to a text string, rather than integer, in an if/else statement?](https://superuser.com/q/543793/173513), [Bash - Integer expression expected](https://unix.stackexchange.com/q/293495/56041), [Bash: Integer expression expected](https://stackoverflow.com/q/17958855/608639), etc. – jww Feb 05 '19 at 04:03

1 Answers1

3

-eq is used for integer comparisons, for text comparisons use =

From the bash man pages:

arg1 OP arg2

          OP  is one of -eq, -ne, -lt, -le, -gt, or -ge.  These arithmetic
          binary operators return true if arg1 is equal to, not equal  to,
          less  than, less than or equal to, greater than, or greater than
          or equal to arg2, respectively.  Arg1 and arg2 may  be  positive
          or negative integers.

and

string1 == string2

string1 = string2

          True if the strings are equal.  = should be used with  the  test
          command  for  POSIX conformance.  When used with the [[ command,
          this performs pattern matching as described above (Compound Com-
          mands).

By the way, your comparison could be written as a pattern:

if [[ "$answer" == [Qq] ]]
Community
  • 1
  • 1
cdarke
  • 42,728
  • 8
  • 80
  • 84