IF statement failure

Hi Guys,

i have a variable which hold a string value of "in" when i try to compare this value against another value the if statement is not working.

$a=in
 
if [ "$a" == "=" ]
then
    echo "Not a Walid match"
else 
    echo "Walid Match"
fi

Thank You.

try this
if($a eq "=")

tried the way u said ... still its not working error message : binary operator expected

code

if [ $a eq "=" ]; then echo "Not a walid match"; else echo "walid match"; fi

suppose if i change to

if [ $a == "=" ]; then echo "Not a walid match"; else echo "walid match"; fi

it works but the output is "Walid Match" where as my ouput should be "Not a walid Match"

copy "=" to a variable
$b = "="

The red '$' is your problem. Remove it, and it should work. To check for further errors, run (depending on your shell)

ksh -n your.script
bash -n your.script

Also, if you're assigning strings it's always a good idea to put quotes around them.

Moderator,

that was a typo in my script ... the $ is not there in my script ... but still its not working and not giving me the excepted result ... pls help

Your logic is reversed : you try if $a equal '=' then not valid match
Use '!=' (not equal) or reverse the statements

a=in
if [ "$a" != "=" ]
then
    echo "Not a Walid match"
else
    echo "Walid Match"
fi

That could also be written

a=in
[ "$a" = "=" ] && echo "Valid Match" || echo "Not a Valid match"

which shell?

you have value of variable "a" as "in". And you are comparing a with symbol "=" !?
moreover, for true condition, you are echoing not valid!
can you please elaborate with your requirement.

by the way, if all true, please try = instead ==

awk -v a="in" '{if(a=="=") {print hi} else print "bye"}'

Thanks for pointing out the typo .... its working now ...

---------- Post updated at 01:28 AM ---------- Previous update was at 01:25 AM ----------

Sorry for the confusion, there was a typo from my sdie ...