Multiple comparison statement in bash fails - bash: [: missing `]'

I need to compare a variable to multiple variables as shown below.

if [ [ "$SECONDS" -ne "$one" || "$SECONDS" -ne "$two" ] ]

This statement is giving me the error

[: missing `]'

How can I compare the value to SECONDS to both one and two. All these are integer comparison.

2 Answers

The correct syntax of if for expresions in bash are this refrence:

Table 7-2. Combining expressions

Operation Effect
------------------ ------------------
[ ! EXPR ] True if EXPR is false.
[ ( EXPR ) ] Returns the value of EXPR. This may be used to override the normal precedence of operators.
[ EXPR1 -a EXPR2 ] True if both EXPR1 and EXPR2 are true.
[ EXPR1 -o EXPR2 ] True if either EXPR1 or EXPR2 is true.

your if statement should be like this:

if [ "$SECONDS" -ne "$one" -o "$SECONDS" -ne "$two" ]

0

You can also use [[ keyword:

if [[ "$SECONDS" -ne "$one" || "$SECONDS" -ne "$two" ]];

Here is it's chart help [[:

 EXPR1 && EXPR2 True if both EXPR1 and EXPR2 are true; else false EXPR1 || EXPR2 True if either EXPR1 or EXPR2 is true; else false

When you start your statement using [ then space and another [; Bash will think that you are running a test (First [) on an other test (second [).

And before your || it will look for a ]literal, can't find it and complains about it.

0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like