Книга: Fedora™ Unleashed, 2008 edition

Number Comparison

Number Comparison

The following operators can be used to compare two numbers:

-eq — To compare whether two numbers are equal

-ge — To compare whether one number is greater than or equal to the other number

-le — To compare whether one number is less than or equal to the other number

-ne — To compare whether two numbers are not equal

-gt — To compare whether one number is greater than the other number

-lt — To compare whether one number is less than the other number

The following shell program compares three numbers, number1, number2, and number3:

#!/bin/sh
number1=5
number2=10
number3=5
if [ $number1 -eq $number3 ]; then
 echo "number1 is equal to number3"
else
 echo "number1 is not equal to number3"
fi
if [ $number1 -ne $number2 ]; then
 echo "number1 is not equal to number2"
else
 echo "number1 is equal to number2"
fi
if [ $number1 -gt $number2 ]; then
 echo "number1 is greater than number2"
else
 echo "number1 is not greater than number2"
fi
if [ $number1 -ge $number3 ]; then
 echo "number1 is greater than or equal to number3"
else
 echo "number1 is not greater than or equal to number3"
fi
if [ $number1 -lt $number2 ]; then
 echo "number1 is less than number2"
else
 echo "number1 is not less than number2"
fi
if [ $number1 -le $number3 ]; then
 echo "number1 is less than or equal to number3"
else
 echo "number1 is not less than or equal to number3"
fi

When you execute the shell program, you get the following results:

number1 is equal to number3
number1 is not equal to number2
number1 is not greater than number2
number1 is greater than or equal to number3
number1 is less than number2
number1 is less than or equal to number3

Оглавление книги

Оглавление статьи/книги

Генерация: 8.036. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз