-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
2

1 Answer 1

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] ]]
Sign up to request clarification or add additional context in comments.

8 Comments

There must be dozens of duplicates for this. Although I'm not sure how to search for them.
@Barmar: probably, but it's quicker to answer it.
Well, this is a nice, succinct answer, I'll make it my dupe candidate.
@Barmar I only found lots of questions with that error message that are almost duplicates, but no good one.
However, I don't think "quicker to answer" is a good reason to answer duplicates.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.