cancel
Showing results for 
Search instead for 
Did you mean: 

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.