Operating System - Linux
1752667 Members
5446 Online
108788 Solutions
New Discussion юеВ

Re: Shell script: Logical operator precedence

 
SOLVED
Go to solution
SGK
Occasional Advisor

Shell script: Logical operator precedence

Hi All,

How to specify the precedence in the if/while loop?

In C,

if( ik || k
Like that, how to do the precedence in Shell script?

Thanks in advance.
4 REPLIES 4
Sergejs Svitnevs
Honored Contributor
Solution

Re: Shell script: Logical operator precedence

You can make logical expressions by grouping them with -a and -o operators (similar to the && and || operators).


if [ $i -lt $j ] && [ $j -gt $k -o $k -lt $i ] ; then
<...>
fi

Regards

Peter Nikitka
Honored Contributor

Re: Shell script: Logical operator precedence

Hi,

that depends on the shell you are using.
In ksh or bash you can use:

if [[ $i < $j && ($j > $k || $k < $i) ]]
then echo true
else echo false
fi

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
SGK
Occasional Advisor

Re: Shell script: Logical operator precedence

Hi,

Thanks all for the response.

Peter Nikitka
Honored Contributor

Re: Shell script: Logical operator precedence

Hi,

since you are new to this forum, I may put your attention to this:

http://forums1.itrc.hp.com/service/forums/helptips.do?#28

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"