Operators in Linux Programming

Linux shell programming has the following operators,they compare or evaluate mathematical,logical and relational expression.

Arithmetic Operators

Operator

Example

Descriptions

+

$a + $b

Perform Addition

-

$a - $b

Perform Subtraction

\*

$a \* $b

Perform Multiplication

/

$a / $b

Perform Division

%

$a % $b

Return Reminder

^

$a ^ $b

Perform Power value

Arithmetic Operator Example

write a program to perform all arithmetic operation


#Create a New File With .sh

$vi file1.sh

#Write Your Script Here

echo "Enter Number 1:"
read no1

echo "Enter Number 2:"
read no2

$add = `$expr $no1 + $no2'

$sub = `$expr $no1 - $no2'

$mul = `$expr $no1 \* $no2'

$div = `$expr $no1 / $no2'
 
echo "Addition is = $add"

echo "Subtraction = $sub"

echo "Multiplication = $mul"

echo "Division = $div"

echo "By Veewom"

# Press ESC Key on Keyboard
						

Output


#Execute Your Script

$vi sh file1.sh

Enter Number 1: 50
Enter Number 2: 50

Addition = 100
Subtraction = 0
Multiplication = 2500
Division = 1
By Veewom						
					

Relational Operators

Operator

Example

Descriptions

-eq

$a -eq $b

equal to (==)

-ne

$a -ne $b

not equal to (!=)

-lt

$a -lt $b

less than (<)

-le

$a -le $b

less than or equal to (<=)

-gt

$a -gt $b

greater than (>)

-gt

$a -gt $b

greater than or equal to (>=)

Logical Operators

Operator

Example

Descriptions

!

!$b

Logical NOT

-a

$a -a $b

logical AND

-o

$a -o $b

Logical OR

String Operators

Operator

Example

Descriptions

=

$str = $str

To check the value of two operands are equal or not.is yes,then condition will be true.

!=

$str != $str

To check the value of two operands are equal or not.if they are not equal,then condition will be true

-Z

-z $str

to check if the given string operands size is zero.if zero length,then it return true.

-n

-n $str

to check if the given string operand size is non zero.if it is non-zero length,then it return true.

str

str($str)

to check if str is not empty string.if it is an empty string then it returns false.

Test File & Directory Type

Operator

Descriptions

-s file

Non empty file

-f file

is file exist or nomar file and not a directory

-d file

is directory exist and not a file

-w file

is writable

-r file

read-only file

-x file

file is executable

Share Share on Facebook Share on Twitter Share on Google+ Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit