0

I am writing the following code:

if [ $opt -ge $max -o $opt -le  0 ]
then
    echo "Bad";
else
    echo "Good";
    if [ $opt = "\" -o $opt = "/"  ]
    then
        echo "Good";
    else
        echo "Invlaid"; //Line number 21
    fi
fi //Line number 23 no Line number 24.

this shows an error:

./file.sh: line 21: unexpected EOF while looking for matching `"'
./file.sh: line 24: syntax error: unexpected end of file

If I place this code:

if [ $opt -ge $max -o $opt -le  0 ]
then
    echo "Bad";
else
    echo "Good";
fi //Line number 23 no Line number 24.

Then there is no error. I am not able to figure out the problem.

2
  • 1
    See the syntax highlighting - you have accidentily "escaped" the second quote in if [ $opt = "\" -o $opt = "/" ]. Commented Jul 26, 2013 at 5:55
  • Note that -o for logical OR is deprecated; you should use [ "$opt" -ge "$max" ] || [ "$opt" -le 0 ] instead--and quote your expansions! Commented Jul 26, 2013 at 13:09

3 Answers 3

3

Where you write "\" you start a string literal whose first character is a double quote. To include a back slash in a string you have to precede it with another:

"\\"
Sign up to request clarification or add additional context in comments.

Comments

1

Backslash \ inside double quotes needs to be escaped or else you can use single quotes like this:

if [ $opt = '\' -o $opt = '/' ]; then
   echo "Good"
fi

Single quotes treat the wrapped string literally that's the precise reason single quote in shell cannot be escaped.

Comments

0
opt="\\"

echo $opt
\

if [ $opt = "/" -o $opt = "\\" ]; then echo "Hi"; else echo "bye"; fi
Hi

Comments

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.