Computing.Net > Forums > Unix > compound conditions

compound conditions

Reply to Message Icon

Original Message
Name: Adam
Date: February 25, 2003 at 07:47:28 Pacific
Subject: compound conditions
OS: SunOS 5.7
CPU/Ram: dont know
Comment:

I'm running ksh and am having trouble with the compound condition below. the way im running it, the first condition should evaluate to false and therefore the second condition shouldnt be evaluated but i think it is and it is being evaluated to true. do you see anything wrong with the if statement?

if [[ $SIZE2 != 0 && $SIZE1 != $SIZE2 ]]


Report Offensive Message For Removal


Response Number 1
Name: James Boothe
Date: February 25, 2003 at 08:09:24 Pacific
Subject: compound conditions
Reply: (edit)

For numerical comparisons, you will get proper results by using the numerical operators -eq -ne -lt -gt. The operators = and != are for string comparison, and 0 will compare unequal to 00 and 000.


Report Offensive Follow Up For Removal

Response Number 2
Name: adam
Date: February 25, 2003 at 08:14:31 Pacific
Subject: compound conditions
Reply: (edit)

I know its ok to use the != instead of -ne. -ne is for the test command. plus if I just use the condition [ $SIZE2 != 0 ] it works. it only starts messing up when i use the compound condition. do i need parenthesis anywhere?


Report Offensive Follow Up For Removal

Response Number 3
Name: adam
Date: February 25, 2003 at 08:32:40 Pacific
Subject: compound conditions
Reply: (edit)

Nevermind guys, I figured it out. the following syntax works:

if [ $SIZE2 != 0 ] && [ $SIZE1 != $SIZE2 ]


Report Offensive Follow Up For Removal

Response Number 4
Name: WilliamRobertson
Date: February 25, 2003 at 09:13:51 Pacific
Subject: compound conditions
Reply: (edit)

[[ $SIZE2 != 0 && $SIZE1 != $SIZE2 ]]

is equivalent to

[[ $SIZE2 != 0 ]] && [[ $SIZE1 != $SIZE2 ]]

though James makes a good point about arithmetic vs string tests. I prefer to separate them out as you did, as I find it easier to follow.

[ ] and -ne are Bourne shell syntax supported by ksh for backward compatibility. Being a fan of ksh I always use the ksh versions which give you more options.

ksh also provides (( )) for arithmetic, so if you are comparing integers, you could use

integer SIZE=1 SIZE2=2
(( SIZE2 != 0000 )) && (( SIZE1 != SIZE2 ))

Note that you don't need the $ for variable names within arithmetic expressions. You can use a whole range of arithmetic operators e.g. >=, %, == and (( SIZE += 1)).


Report Offensive Follow Up For Removal

Response Number 5
Name: James Boothe
Date: February 25, 2003 at 10:23:16 Pacific
Subject: compound conditions
Reply: (edit)

Oops, sorry for that puny answer of mine, and thanks to WilliamRobertson for that great explanation.

By the way, regarding not needing the $ for variable names within arithmetic expressions, you usually will not want them, but sometimes you could. It is a form of parenthetic control:

var1=3+4
var2=4+5
print $((var1*var2))
63
print $(($var1*$var2))
24


Report Offensive Follow Up For Removal


Response Number 6
Name: WilliamRobertson
Date: February 26, 2003 at 06:45:16 Pacific
Subject: compound conditions
Reply: (edit)

Wow, hadn't seen that before!

Another good reason to declare numeric variables with integer / typeset -i.

$ charvar=4+5
$ integer intvar=4+5
$ print charvar: $charvar intvar: $intvar
charvar: 4+5 intvar: 9


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: compound conditions

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software