4.2 Shell Comparisons

5. Korn Shell Built in Comparison operators

Use [[ ]] to compare string values Use (( )) to compare numerical values
i.e. string equality, lexical order, length and comparison with a pattern i.e. numerical relational operators, equality, inequality. no $ needed within (( ))


	
6. NOTE: The result or status of any Unix command is given by: 
					0  	means successful
				    non-zero 	means unsuccessful
									        
	Show the result via:  $ print $? immediately after the unix command
			
    BUT:    The result of the command:   (( n )) is given as successful 
    				if n is non-zero
					 unsuccessful if n is 0
									      
			Show the result via:	$  (( n ))
						 $  print $?  $n
						     
7. test (and [   ] ) Comparison operators for numbers (Bourne and Korn Shell)
Symbol Meaning
-eq equal
-ne not equal
-gt greater than
-lt less than
-ge greater or equal
-le
less than or equal
! not
-a Boolean AND
-o Boolean inclusive OR
-z string true if string length = 0
-n string true if string length > 0
string1 = string2 true if strings are the same
string1 != string2 true if strings are not equal
string true if string not the null string
8. Example using test and [   ]

	$ x=4
	$ y=6
	$ test $x -gt $y		# same as    $ [ $x -gt $y ]
	$ print $?
	1
	$ test $x -lt $y		# same as    $ [ $x -lt $y ]
	$ print $?
	0

9. Korn Shell String Comparison Syntax Summary
Comparison Meaning
[[ string1 = string2 ]] string1 is identical to string2
[[ string1 = pattern ]] string1 matches (shell) pattern
[[ string1 != string2 ]] string1 is not identical to string2
[[ string1 != pattern ]] string1 does not match (shell) pattern
[[ string1 < string2 ]] string1 precedes string2 in lexical order
[[ string1 > string2 ]] string1 follows string2 in lexical order
[[ -z string ]] string's length is 0 ( string is the null string)
[[ -n string ]] string's length is non-zero

10. Korn Shell Arithmetic Operators These operators can be used by the built-in let command and by (( )). Their precedence order is from highest to lowest.

Operator Symbol Meaning Example
(...) Grouping To override precedence order $ (( a=(7+5) * (5-3) ))
$ print $a
24
- unary minus $ negative=-1
$ print -- $negative
-1
! Logical negation $ a=1
$ (( b = ! a ))
$ print $a $b
1 0

~ Bitwise negation $ a=2#1011
$ typeset -i2 c
$ (( c = ~ a ))
$ print $a $c
2#1011 -2#1100
* / % Multiplication; Division; Remainder $ (( y = 5 * 8 ))
$ (( x = 37 / 5 ))
$ (( z = 37 % 5 ))
$ print $y $x $z
40 7 2

+ - Addition; Subtraction $ (( r = x + z ))
$ (( s = x - z ))
$ print $r $s
9 5
<< >> Left Shift; Right Shift $ (( y = 2#1101 << 2 ))
$ (( z = 2#1011 >> 2 ))
print $y $z
52 2
<= >= < > == != Comparison Operators $ print $(( y > z ))
1
$ print $(( y < z ))
0
& Bitwise AND $ typeset -i2 n
$ (( n = w = 2#1010 & 2#1100 ))
$ print $w $n
8 2#1000
^ Bitwise Exclusive OR (XOR) $ typeset -i2 m
$ (( m = v = 2#1010 ^ 2#1100 ))
$ print $v $m
6 2#110
| Bitwise Inclusive OR $ typeset -i2 t
$ (( u = 2#1010 | 2#1100 ))
$ t=$u
$ print $u $t
14 2#1110
&& Logical AND $ a=7 b=-5 c=0
$ print $(( a && b)) $(( a && c ))
1 0



|| Logical (Inclusive) OR $ a=7 b=-5 c=0
$ print $(( a || b)) $(( a || c )) $(( c || c ))
1 1 0
= Assignment $ a=b=c=7
$ print $a $b $c
7 7 7
*= /= %= Compound Arithmetic $ typeset -i2 b
$ a=36
$ (( b = (a /= 12) ))
$ print $ a $b
3 2#11
+= -= <<= >>= Compound Arithmetic; Compound Shift $ typeset -i2 b
$ a=36
$ (( b = ( a >>= 3 ) ))
$ print $a $b
4 2#100

&= ^= |= Compound Bitwise AND, XOR, OR $ typeset -i2 w=2#1010
$ ((n = (w &= 2#1100 ) ))
$ print $w $n
8 2#1000

11. Korn Shell File Object Comparisons

Bourne Shell
test?
File Object Comparison Returns true ( Successful) if object:
  [[ -a object ]] exists; any file type is ok
yes [[ -f object ]] is a regular file (or symbolic link!)
yes [[ -d object ]] is a directory
  [[ -c object ]] is a character special file
  [[ -b object ]] is a block special file
  [[ -p object ]] is a named pipe file
  [[ -S object ]] is a socket file
  [[ -L object ]] is a symbolic (Soft) link
yes [[ -s object ]] is not empty; number of bytes greater than 0
yes [[ -r object ]] is readable
yes [[ -w object ]] is writeable or modifiable
yes [[ -x object ]] is executable or searchable (directory)
  [[ -u object ]] has setuid bit set
  [[ -g object ]] has setgid bit set
  [[ -k object ]] has sticky bit set
  [[ -O object ]] is owned by the user
  [[ -G object ]] is in the group owned by the user
  [[ object1-nt object2 ]] object1 is newer than object2
  [[ object1-ot object2 ]] object1 is older than object2
  [[ object1-ef object2 ]] object1 is effectively another name for object2


Questions? Robert Katz: rkatz@ned.highline.ctc.edu
Last Update January 29, 2002