4

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.

1
  • single brace is old style of shell programming and you have to use -o and -a for OR and AND.CommentedNov 18, 2014 at 2:37

2 Answers 2

6

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 
3
  • 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.CommentedNov 17, 2014 at 20:00
  • @SudevJash see update.
    – muru
    CommentedNov 17, 2014 at 20:03
  • Thanks a lot Muru... Making the required changes worked as expected...CommentedNov 17, 2014 at 20:12
0

Instead of:

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

I would use:

if [ $a != STARTED && $b == STARTED ] || [ $b != STARTED && $a == STARTED ]; then 
10
  • I m getting the error as : blserver1.sh: line 11: [: missing ]' blserver1.sh: line 11: [: missing ]'CommentedNov 17, 2014 at 19:57
  • 1
    Recommending [ over [[ in a bash script is a retrograde step; and if you are, you must quote your variables...CommentedNov 17, 2014 at 20:02
  • 1
    And is && supported in [?
    – muru
    CommentedNov 17, 2014 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" ]]; thenCommentedNov 17, 2014 at 20:14
  • 1
    This isn't syntactically correct. Did you mean to use double brackets instead of single brackets? That's pretty important, given that this is the topic of the question.CommentedNov 18, 2014 at 8:54

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.