Bash – syntax error in conditional expression

bashshell-script

I am writing a script where I am using the combination of logical 'OR' and logical 'AND' statement. This is the script:

#!/bin/bash

echo "Enter the value of a"
read $a
echo "Enter the value of b"
read $b
if

[[ $a != STARTED && $b == STARTED ] || [ $b != STARTED && $a == STARTED ]]; then

echo "Either of the JVMs is not yet up, so lets wait for some more time"

i=$(($i+1))
sleep 1s

fi

and getting the following error while executing it:

line 13: syntax error in conditional expression
line 13: syntax error near `]'
line 13: `[[ $a != STARTED && $b == STARTED ] || [ $b != STARTED && $a == STARTED ]]; then'

I am using bash shell. Any help on this is really appreciated.

Best Answer

You have mismatched [[ with ]. [[ should always be closed with ]] and [ with ]. Use:

if [[ $a != STARTED && $b == STARTED ]] || [[ $b != STARTED && $a == STARTED ]]; then

Better yet, since you are using [[ anyway:

if [[ ($a != STARTED && $b == STARTED) || ($b != STARTED && $a == STARTED) ]]; then

The other mistake, which I didn't notice until formatting was applied, is that you're doing:

read $a
read $b

You should be doing:

read a
read b

With the first form, $a and $b are replaced by the shell with their contents, so if you hadn't set them before this line, the final command would be:

read

(in which case the value read would be stored in the REPLY variable.) And if you had set a to something (like a="blah blah"), it would look like:

read blah blah
Related Question