Operating System - HP-UX
1828625 Members
1686 Online
109983 Solutions
New Discussion

Re: put two conditions in if

 
SOLVED
Go to solution
Gemini_2
Regular Advisor

put two conditions in if

I kept getting wrong on the synatax, can someone please advise me?

if [ ! conditon 1 ] && [ condition " ]
];then

something

fi

thank you
12 REPLIES 12
Patrick Wallek
Honored Contributor

Re: put two conditions in if

What exact syntax are you trying to use? Seeing the conditions youare attempting would be a big help.

In general:

if [[ "${A}" = "A" && "${B} = "B" ]] ; then
do something
else
do something else
fi
Geoff Wild
Honored Contributor

Re: put two conditions in if

Like:

if [ -z "$VARIABLE" ] && [ -f /path/somefile]

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Chan 007
Honored Contributor

Re: put two conditions in if

Hi,

if [ != $TEST ] && [ != $TEST11 ] then
something
else
something
fi

(use for both conditions to be satisfied, if you need any one condition to be satisfied then use ||)

Chan
James R. Ferguson
Acclaimed Contributor

Re: put two conditions in if

Hi Gemini:

# X=1;Y=1;if [ $X -ne 1 ] && [ $Y -eq 1 ];then echo "ok";fi

Regards!

...JRF...
Joseph C. Denman
Honored Contributor

Re: put two conditions in if

if [ $count -ge 0 -a $count -lt 10 ]
then
else
fi


...jcd...
If I had only read the instructions first??
Gemini_2
Regular Advisor

Re: put two conditions in if

thank you!!1

yes, I was looking for the "-a" option!!!

A. Clay Stephenson
Acclaimed Contributor
Solution

Re: put two conditions in if

These two if's are equivalent:

if [ ${count} -ge 0 -a ${count} -lt 10 ]
then
else
fi


if [[ ${count} -ge 0 && ${count} -lt 10 ]]
then
else
fi

however, the second form is more efficient because the '[[ ]]' is evaluated directly by the shell (Korn or POSIX) whereas the more traditional '[ ]' is evaluated by the external expr command.


If it ain't broke, I can fix that.
Gemini_2
Regular Advisor

Re: put two conditions in if

thanks for the clear explaination..

if [[ ${count} -ge 0 && ${count} -lt 10 ]]
then
else

I thought that [[ was very ugly syntax, I didnt know the significian behind it..


appreciate it very much
A. Clay Stephenson
Acclaimed Contributor

Re: put two conditions in if

Oops, I'm stupid.

however, the second form is more efficient because the '[[ ]]' is evaluated directly by the shell (Korn or POSIX) whereas the more traditional '[ ]' is evaluated by the external EXPR command

should be:

however, the second form is more efficient because the '[[ ]]' is evaluated directly by the shell (Korn or POSIX) whereas the more traditional '[ ]' is evaluated by the external TEST command

I was thinking one thing and typed another.
If it ain't broke, I can fix that.
Dennis Handly
Acclaimed Contributor

Re: put two conditional expressions in if

>the second form is more efficient because the '[[ ]]' is evaluated directly by the shell (Korn or POSIX) whereas the more traditional '[ ]' is evaluated by the external test(1) command

 

This is incorrect.  test is documented as a builtin and tusc shows that ksh has [ ... ] builtin.

And especially since /usr/bin/test is just a Posix shell script that invokes the sh test builtin.

Stephan.
Honored Contributor

Re: put two conditions in if


@Dennis Handly wrote:

>the second form is more efficient because the '[[ ]]' is evaluated directly by the shell (Korn or POSIX) whereas the more traditional '[ ]' is evaluated by the external test(1) command

 

This is incorrect.  tusc shows that ksh has [ ... ] builtin.

And especially since /usr/bin/test is just a Posix shell script that invokes the builtin.


So to give it some sense to dig up this one - what would be the technical exact answer for the difference of

 

[ ]

or

[[ ]]

 ?

Dennis Handly
Acclaimed Contributor

Re: put two conditional expressions in if

>what would be the technical exact answer for the difference of: [ ... ]  [[ ... ]]

 

There are three ways to compare in a real shell:

 1) Arithmetic expressions: (( )), only handles numbers, not strings

 

2) [ ... ] or test builtin

    Compound expressions are composed with -a and -o.  Use of () must be quoted.

    Arithmetic expressions, without (( )), can be used in arithmetic comparison operators.

    String = and != compare strings.

    String < and > don't exist.  (Treated as redirection.)

 

 3) [[ ... ]]

    Compound expressions are composed with && and ||.  Use of () doesn't need to be quoted.

    Arithmetic expressions can't be used in arithmetic comparison operators.

    String = and != match patterns.

    String < and > exist.