Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
General
cancel
Showing results for 
Search instead for 
Did you mean: 

Abnormal if condition behavior in script.

Abnormal if condition behavior in script.

Hi all,
I input the attached file to the following script.
+++++++++++++++++++++++++++++++++++++++++++++
#!/bin/bash
num="`awk '/X-MailRelay-ToCount/{print $2}' $1`"
echo $num
if test "${num}" = "1"
then
echo num eq to 1
else
echo num not eq to 1
fi
exit
++++++++++++++++++++++++++++++++++++++++++++
But when I execute the script it doesn't not output the desired output.
Means when $num =1, it should output "num eq to 1" ,BUT it outputs the else part.

Please help me to resolve this abnormal behavior.Any modifications to the script.
Big thank.




To get out of a difficulty, one usually must go through it
4 REPLIES
Alexander Chuzhoy
Honored Contributor

Re: Abnormal if condition behavior in script.

I think you should have == instead of =
in this line:
if test "${num}" = "1"

I mean you want to say if it's equal then do something.
In your script it says it's equal .
Not that I'm great with bash scripts...
Mark Grant
Honored Contributor

Re: Abnormal if condition behavior in script.

Alexander is obviously good enough with bash scripts :)

He has your error :)



P.S. No points for this one please.
Never preceed any demonstration with anything more predictive than "watch this"
Olivier Drouin
Trusted Contributor

Re: Abnormal if condition behavior in script.

for example...
you use == for string comparison.
you use -eq for integer comparison.
Penguin_1
Occasional Visitor

Re: Abnormal if condition behavior in script.

use this
if [ ${num} -eq 1 ]; then
.....

penguin