Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AND / OR usage in a script 1

Status
Not open for further replies.

logic2fun

IS-IT--Management
Mar 19, 2008
8
0
0
US
Friends i am trying to use both AND and OR in one command and my OR doesnt seem to be working. Am i missing anything here

if [ $srcCnt -ge 5000 ] && [ $ldCnt -le 200 ] || [ $sleepVar > 120 ]
then
Do something
else
SLEEP
fi


It looks like it is not accounting for my OR command. It works fine when i check just AND or Just OR.

Thanks ahead
Logic4fun
 
Hi

You forgot to mention what shell are you using. In meantime you could try these in [tt]bash[/tt] and [tt]ksh[/tt] :
Code:
if [ $srcCnt -ge 5000 [red]-a[/red] $ldCnt -le 200 [red]-o[/red] $sleepVar [red]-gt[/red] 120 ]

[gray]# or[/gray]

if [[red][[/red] $srcCnt -ge 5000 && $ldCnt -le 200 || $sleepVar [red]-gt[/red] 120 [red]][/red]]

Feherke.
 
That && and || is not what you're after

[tt]command1 && command2[/tt]

is short for

[tt]if command1
then
command2
fi[/tt]

[tt]command1 || command2[/tt]

is short for

[tt]if command1
then
:
else
command2
fi[/tt]

Try this:

[tt]if [ \( $srcCnt -ge 5000 -a $ldCnt -le 200 \) -o $sleepVar > 120 ]
then
...
fi[/tt]

with ksh or bash, inside (( )) tests the && and || do work and that is cheaper (no $s):

[tt]if (( ( (srcCnt>5000)&&(ldCnt<200) ) || (sleepVar>120) ))
then
...
fi[/tt]


HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top