Free Hosting Online for WorkStations

< Previous | Contents | Next >

Integer Expressions

The following expressions are used with integers:


Table 27-3: test Integer Expressions


Expression Is True If...

Expression Is True If...

integer1 -eq integer2 integer1 is equal to integer2. integer1 -ne integer2 integer1 is not equal to integer2. integer1 -le integer2 integer1 is less than or equal to integer2. integer1 -lt integer2 integer1 is less than integer2.


integer1 -ge integer2 integer1 is greater than or equal to integer2.


#!/bin/bash


# test-integer: evaluate the value of an integer.


INT=-5


if [ -z "$INT" ]; then

echo "INT is empty." >&2 exit 1

fi


if [ $INT -eq 0 ]; then echo "INT is zero."

else

if [ $INT -lt 0 ]; then echo "INT is negative."

else

echo "INT is positive."

fi

if [ $((INT % 2)) -eq 0 ]; then echo "INT is even."

else

echo "INT is odd."

fi

fi

#!/bin/bash


# test-integer: evaluate the value of an integer.


INT=-5


if [ -z "$INT" ]; then

echo "INT is empty." >&2 exit 1

fi


if [ $INT -eq 0 ]; then echo "INT is zero."

else

if [ $INT -lt 0 ]; then echo "INT is negative."

else

echo "INT is positive."

fi

if [ $((INT % 2)) -eq 0 ]; then echo "INT is even."

else

echo "INT is odd."

fi

fi

integer1 -gt integer2 integer1 is greater than integer2. Here is a script that demonstrates them:



The interesting part of the script is how it determines whether an integer is even or odd. By performing a modulo 2 operation on the number, which divides the number by two and returns the remainder, it can tell if the number is odd or even.


Top OS Cloud Computing at OnWorks: