Operating System - Linux
1753775 Members
7343 Online
108799 Solutions
New Discussion юеВ

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 4
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
New Member

Re: Abnormal if condition behavior in script.

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

penguin