0

Below is the bash script

the value of b returned by the script is 0 or 1.

 b=`tail -l /apps_data_01/mc_migs/inbound/logs/fp.syslog | awk c=$7} {print $c}


if ((!"$b" eq "0"));then

fi
if (($b==0));then

fi

Error:

((: ! eq 0: syntax error in expression (error token is "0")

((: ==0: syntax error: operand expected (error token is "==0")

2
  • @Code-Apprentice thanks will try that now Commented Sep 11, 2016 at 4:00
  • Please use shellcheck.net. Commented Sep 11, 2016 at 13:30

2 Answers 2

3

Correct syntax is the following:

b=0
if [[ ! "$b" == "0" ]];then
echo "Not EQ"
fi
if [[ $b == 0 ]];then
echo "EQ"
fi

Please note [[ and spaces.

Using the [[ ... ]] test construct, rather than [ ... ] can prevent many logic errors in scripts. For example, the &&, ||, <, and > operators work within a [[ ]] test, despite giving an error within a [ ] construct. its a good practice to use them

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

2 Comments

Are nested braces required?
[ ] is the original test command, which is portable to all POSIX shells, but has nasty syntactic oddities. [[ ]] is a bash extension, with much cleaner syntax and some additional capabilities (like regular expression matching with =~). (( )) is another bash extension that does arithmetic only, not string comparisons (but can do more math, like if (( x*2 >= y+5 )); then...).
-1

bash uses [] to enclose the condition for an if statement. Also you need spaces before and after the == operator. See http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html for more details about if syntax in bash scripts.

Note that you can use an else to avoid duplicating the comparison.

2 Comments

(( )) is appropriate for strictly numeric comparisons, but in this case it's acting like $b is blank, making for invalid numeric expressions. If some "numbers" might not actually be numbers (and you don't need greater/less comparisons, just equal/not equal), [[ ]] or [ ] is safer.
[ does not "enclose" the condition for an if statement. if simply takes any list of commands as the condition, and [ is just one command (an unfortunate alias of test) that is commonly used.

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.