0% found this document useful (0 votes)
20 views

Relational Operators: Show Examples

The document discusses various operators supported by the Bourne shell including relational operators like -eq and -lt for numeric comparisons, boolean operators like -a and -o, string operators like = and !=, and file test operators like -f and -s. Relational operators check relations between values, boolean operators combine conditions with AND and OR, string operators compare strings, and file test operators check properties of files.

Uploaded by

rahul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Relational Operators: Show Examples

The document discusses various operators supported by the Bourne shell including relational operators like -eq and -lt for numeric comparisons, boolean operators like -a and -o, string operators like = and !=, and file test operators like -f and -s. Relational operators check relations between values, boolean operators combine conditions with AND and OR, string operators compare strings, and file test operators check properties of files.

Uploaded by

rahul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Relational Operators

Bourne Shell supports the following relational operators that are specific to numeric
values. These operators do not work for string values unless their value is numeric.
For example, following operators will work to check a relation between 10 and 20 as
well as in between "10" and "20" but not in between "ten" and "twenty".
Assume variable a holds 10 and variable b holds 20 then −
Show Examples

Operato Description Example


r

-eq Checks if the value of two operands are [ $a -eq $b ] is not true.
equal or not; if yes, then the condition
becomes true.

-ne Checks if the value of two operands are


equal or not; if values are not equal, then the [ $a -ne $b ] is true.
condition becomes true.

-gt Checks if the value of left operand is greater


than the value of right operand; if yes, then [ $a -gt $b ] is not true.
the condition becomes true.

-lt Checks if the value of left operand is less


than the value of right operand; if yes, then [ $a -lt $b ] is true.
the condition becomes true.

-ge Checks if the value of left operand is greater


than or equal to the value of right operand; if [ $a -ge $b ] is not true.
yes, then the condition becomes true.

-le Checks if the value of left operand is less


than or equal to the value of right operand; if [ $a -le $b ] is true.
yes, then the condition becomes true.

It is very important to understand that all the conditional expressions should be


placed inside square braces with spaces around them. For example, [ $a <= $b ] is
correct whereas, [$a <= $b] is incorrect.
Boolean Operators
The following Boolean operators are supported by the Bourne Shell.
Assume variable a holds 10 and variable b holds 20 then −
Show Examples

Operato Description Example


r

! This is logical negation. This inverts


a true condition into false and vice [ ! false ] is true.
versa.

-o This is logical OR. If one of the [ $a -lt 20 -o $b -gt 100 ] is true.


operands is true, then the condition
becomes true.

-a This is logical AND. If both the


operands are true, then the
[ $a -lt 20 -a $b -gt 100 ] is false.
condition becomes true otherwise
false.

String Operators
The following string operators are supported by Bourne Shell.
Assume variable a holds "abc" and variable b holds "efg" then −
Show Examples

Operato Description Example


r

= Checks if the value of two operands are equal [ $a = $b ] is not true.


or not; if yes, then the condition becomes true.

!= Checks if the value of two operands are equal [ $a != $b ] is true.


or not; if values are not equal then the
condition becomes true.

-z Checks if the given string operand size is zero; [ -z $a ] is not true.


if it is zero length, then it returns true.

-n Checks if the given string operand size is non- [ -n $a ] is not false.


zero; if it is nonzero length, then it returns true.

str Checks if str is not the empty string; if it is [ $a ] is not false.


empty, then it returns false.

File Test Operators


We have a few operators that can be used to test various properties associated with
a Unix file.
Assume a variable file holds an existing file name "test" the size of which is 100
bytes and has read, write and execute permission on −
Show Examples

Operato Description Example


r

-b file Checks if file is a block special file; if yes, then [ -b $file ] is false.
the condition becomes true.

-c file Checks if file is a character special file; if yes, [ -c $file ] is false.


then the condition becomes true.

-d file Checks if file is a directory; if yes, then the [ -d $file ] is not true.
condition becomes true.

-f file Checks if file is an ordinary file as opposed to a [ -f $file ] is true.


directory or special file; if yes, then the
condition becomes true.

-g file Checks if file has its set group ID (SGID) bit [ -g $file ] is false.
set; if yes, then the condition becomes true.

-k file Checks if file has its sticky bit set; if yes, then [ -k $file ] is false.
the condition becomes true.
-p file Checks if file is a named pipe; if yes, then the [ -p $file ] is false.
condition becomes true.

-t file Checks if file descriptor is open and associated [ -t $file ] is false.


with a terminal; if yes, then the condition
becomes true.

-u file Checks if file has its Set User ID (SUID) bit set; [ -u $file ] is false.
if yes, then the condition becomes true.

-r file Checks if file is readable; if yes, then the [ -r $file ] is true.


condition becomes true.

-w file Checks if file is writable; if yes, then the [ -w $file ] is true.


condition becomes true.

-x file Checks if file is executable; if yes, then the [ -x $file ] is true.


condition becomes true.

-s file Checks if file has size greater than 0; if yes, [ -s $file ] is true.
then condition becomes true.

-e file Checks if file exists; is true even if file is a [ -e $file ] is true.


directory but exists.

C Shell Operators
Following link will give you a brief idea on C Shell Operators −
C Shell Operators

You might also like