Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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.

share|improve this question
    
single brace is old style of shell programming and you have to use -o and -a for OR and AND. –  PersianGulf Nov 18 '14 at 2:37

2 Answers 2

up vote 3 down vote accepted

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
share|improve this answer
    
i have used both the statements mentioned but after providing the values of a and b respectively, it is not taking me to the if condition even if the condition satisfies. –  Sudev Jash Nov 17 '14 at 20:00
    
@SudevJash see update. –  muru Nov 17 '14 at 20:03
    
Thanks a lot Muru... Making the required changes worked as expected... –  Sudev Jash Nov 17 '14 at 20:12

Instead of:

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

I would use:

if [ $a != STARTED && $b == STARTED ] || [ $b != STARTED && $a == STARTED ]; then
share|improve this answer
    
I m getting the error as : blserver1.sh: line 11: [: missing ]' blserver1.sh: line 11: [: missing ]' –  Sudev Jash Nov 17 '14 at 19:57
    
Recommending [ over [[ in a bash script is a retrograde step; and if you are, you must quote your variables... –  jasonwryan Nov 17 '14 at 20:02
    
And is && supported in [? –  muru Nov 17 '14 at 20:04
    
Thanks jasonwryan and Michael... i used quotes and it is working fine now... if [[ "$a" != "STARTED" && "$b" == "STARTED" ]] || [[ "$b" != "STARTED" && "$a" == "STARTED" ]]; then –  Sudev Jash Nov 17 '14 at 20:14
    
@SudevJash You don't need the quotes in [[ (although they are not harmful): see the link I posted above... –  jasonwryan Nov 17 '14 at 20:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.