Integer values are unequal | '$J' -ne 10 | |
'$K' -gt 25 | ||
'$L' -ge 25 | ||
'$M' -lt 75 | ||
'$N' -le 75 | ||
'$P' -gt 36 -a '$P' -lt 71 | ||
'$P' -lt 12 -o '$P' -eq 50 |
So if you wanted to print 'Too high!' if the value of the variable A was over 50, you would write:
if test '$A' -gt 50
then
echo 'Too high!'
fi
The variable expression $A is quoted in case A has a null value ('') or doesn't existin which case, if unquoted, a syntax error would occur because there would be nothing to the left of -gt .
The square brackets ( [] ) are a synonym for test , so the previous code is more commonly written:
if [ '$A' -gt 50 ]
then
echo 'Too high!'
fi
You can also use
while [ '$(who | wc -l)' -lt 100 ]
do
sleep 15
done
echo 'Over 100 users are now logged in!'|mail -s 'Overload!' alert
4.12.1.4. Integer arithmetic
Inside double parentheses, you can read a variable's value without using the dollar sign (use A=B+C instead of A=$B+$C).
Here's an example using a while loop that counts from 1 to 20 using integer arithmetic:
A=0
while [ '$A' -lt 20 ]
do
(( A=A+1 ))
echo $A
done
The C-style increment operators are available, so this code could be rewritten as:
A=0
while [ '$A' -lt 20 ]
do
echo $(( ++A ))
done
The expression $(( ++A )) returns the value of A after it is incremented. You could also use $(( A++ )) , which returns the value of A before it is incremented:
A=1
while [ '$A' -le 20 ]
do
echo $(( A++ ))
done