Check a variable value through if clause

Hi guys,

I am trying to check the values i have for two variables.

 
if [ $Test1 == 'Dummy' ] && [ $Count -eq 0 ]; then
echo "Success";
fi

Now Test1 can have any Alpha Variable and Count is a integer value.

Even though we have given 'and' Condition, even one condition is sucess, i am getting the Success message.

Can somebody point out where i am going wrong.

Cheers!!!!!

don't know what's wrong..

this works fine:

wonderer@wonderer:~$ export test=dummy
wonderer@wonderer:~$ export count=1
wonderer@wonderer:~$ echo $test
dummy
wonderer@wonderer:~$ echo $count
1
wonderer@wonderer:~$ if [ "$test" == "dummy" ] && [ $count -eq 0 ]; then echo "success"; fi

Remove one of the equal to sign and try [ $Test1 = 'Dummy' ] . Also which OS your using..?

Also the quotes should be the other way around:

[ "$Test1" = Dummy ]

You need quotes around the second part too if there are spaces or special characters..

For an "and" condition, I'd expect the syntax:

#ksh93
if [[ "$Test1" == 'Dummy' && $Count -eq 0 ]]; then
#ksh & Posix
if [[ "$Test1" = 'Dummy' && $Count -eq 0 ]]; then
#ksh & Posix   (my preferred syntax)
if [ "$Test1" = 'Dummy' -a $Count -eq 0 ]; then

However, please post what Shell you are using and your test data values.

@methyl -a is deprecated, the use of two test statements with && is encouraged..

They couldn't possibly depreciate -a, or guarantee && will work in all shells.

All Posix compliant shells, yes...

test isn't even a shell builtin. They couldn't possibly depreciate -a. They can roll their eyes and sigh at people who use it, but it's not going away.

Not everyone has the luxury of a POSIX compliant shell, either. Don't make me lecture you for not using PAX instead of tar -- that's the better, POSIX way to do archiving now :wink:

Posix "standards" changes year-on-year over the decades and have the occasional touch of madness in threatening to depreciate feature which are in widespread use in manufacture-supplied system scripts as well as custom scripts.
I prefer the "-a" and "-o" syntax myself compared with the ancient "&&" and "||" syntax. Those with a bias towards "C" and "awk" seem to prefer the older syntax. I do use "&&" and "||" in crontab but without a leading "if".
It's about 50-50 in the system scripts which come with my system.

From the moment Posix didn't include compatibility settings with other Bourne-like Shells we found machines shipped with multiple alternate Shells and manufacturers offering downloads of alternate Shells.

I love the this line from the Wikipedia on Posix:

No, obviously, it will not go away entirely, but that is not what deprecation means. Of course anyone can keep using whatever they like. But is it is good practice on this forum to show solutions that work across platforms and the best guarantee for that is POSIX. I hardly think think pointing that out can be seen as "lecturing".

Apparently there are reasons for obsolescing -a and -o . They are explained here: test: application usage

Also, test is a shell builtin as well as an external program in practically any shell nowadays. POSIX requires that regular built-in commands also exist as separate binaries.

$ bash
$ type test
test is a shell builtin
$ dash
$ type test
test is a shell builtin
$ zsh
% type test
test is a shell builtin
% ksh
$ type test
test is a shell builtin

That is why the use of ampersands to connect tests statements isn't detrimental to speed anymore..

What systems are you using that do not have a "luxury" POSIX compliant shell? Solaris?

---------- Post updated at 08:04 ---------- Previous update was at 07:46 ----------

The reason I prefer this syntax is not because of C or awk, but because it conforms to POSIX recommendations plus apparently there are reasons for avoiding -o and -a.

1 Like

Really useful content..Thanks