Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1
string1 not equal   to  string2
string2 not equal to string1
string1 is not empty
string2 has a length greater than zero
string1 has a length greater than zero

If two strings are not equal in size, the system pads out the shorter string with
trailing spaces for comparison. That is, if the value of string1 is “abc”
and that of string2 is “ab”, string2 is padded with a trailing space for
comparison purposes; it has the value “ab “ (with a space after the letters).


Numeric Comparison

The following operators can be used to compare two numbers:


-eq—Compares    whether two numbers are equal
-ge—Compares whether one number is greater than or equal to the other
number
-le—Compares whether one number is less than or equal to the other
number
-ne—Compares whether two numbers are not equal
-gt—Compares whether one number is greater than the other number
-lt—Compares whether one number is less than the other number

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


Click here to view code image
#!/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
Free download pdf