gt Checks if the value of left operand is greater than the value of right

Gt checks if the value of left operand is greater

This preview shows page 9 - 12 out of 26 pages.

-gt Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. [ \$a -gt \$b ] is not true. -lt Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. [ \$a -lt \$b ] is true. -ge Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. [ \$a -ge \$b ] is not true. -le Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. [ \$a -le \$b ] is true. It is very important to note here that all the conditional expressions would be put inside square braces with one spaces around them, for example [ \$a -gt \$b ] is correct where as [\$a -gt \$b] is incorrect. Here is an example which uses all the relational operators: #!/bin/sh a = 10 b = 20 if [ \$a - eq \$b ] then echo "\$a -eq \$b : a is equal to b" else echo "\$a -eq \$b: a is not equal to b" fi if [ \$a - ne \$b ] then echo "\$a -ne \$b: a is not equal to b" else echo "\$a -ne \$b : a is equal to b" fi if [ \$a - gt \$b ] then echo "\$a -gt \$b: a is greater than b" else echo "\$a -gt \$b: a is not greater than b" fi if [ \$a - lt \$b ] then echo "\$a -lt \$b: a is less than b"
else echo "\$a -lt \$b: a is not less than b" fi if [ \$a - ge \$b ] then echo "\$a -ge \$b: a is greater or equal to b" else echo "\$a -ge \$b: a is not greater or equal to b" fi if [ \$a - le \$b ] then echo "\$a -le \$b: a is less or equal to b" else echo "\$a -le \$b: a is not less or equal to b" fi This would produce following result − 10 - eq 20 : a is not equal to b 10 - ne 20 : a is not equal to b 10 - gt 20 : a is not greater than b 10 - lt 20 : a is less than b 10 - ge 20 : a is not greater or equal to b 10 - le 20 : a is less or equal to b 6.7 Boolean Operators Assume variable a=10 and variable b=20 then: Operato r Description Example ! This is logical negation. This inverts a true condition into false and vice versa. [ ! false ] is true. -o This is logical OR. If one of the operands is true then condition would be true. [ \$a -lt 20 -o \$b -gt 100 ] is true. -a This is logical AND. If both the operands are true then condition would be true otherwise it would be false. [ \$a -lt 20 -a \$b -gt 100 ] is false. Here is an example which uses all the boolean operators #!/bin/sh a = 10 b = 20 if [ \$a != \$b ] then echo "\$a != \$b : a is not equal to b" else echo "\$a != \$b: a is equal to b"
fi if [ \$a - lt 100 - a \$b - gt 15 ] then echo "\$a -lt 100 -a \$b -gt 15 : returns true" else echo "\$a -lt 100 -a \$b -gt 15 : returns false" fi if [ \$a - lt 100 - o \$b - gt 100 ] then echo "\$a -lt 100 -o \$b -gt 100 : returns true" else echo "\$a -lt 100 -o \$b -gt 100 : returns false" fi if [ \$a - lt 5 - o \$b - gt 100 ] then echo "\$a -lt 100 -o \$b -gt 100 : returns true" else echo "\$a -lt 100 -o \$b -gt 100 : returns false" fi This would produce following result − 10 != 20 : a is not equal to b 10 - lt 100 - a 20 - gt 15 : returns true 10 - lt 100 - o 20 - gt 100 : returns true 10 - lt 5 - o 20 - gt 100 : returns false 6.8 String Operators Assume variable a holds "abc" and variable b holds "efg" then: Operato r Description Example = Checks if the value of two operands are equal or not, if yes then condition becomes true.

You've reached the end of your free preview.

Want to read all 26 pages?

• Fall '17
• Mr Son
• Shell, relational operator