Basic Operators in Shell Scripting

C++Server Side ProgrammingProgramming

Shell is an interface using which the programmer can execute command and interact directly to the operating system. Shell scripting is giving commands that a shell can execute.

In shell also there are variables and operators that are used to manipulate these variables. There are 5 basic operators in shell scripting.

  • Arithmetic Operators
  • Relational Operators
  • Boolean Operators
  • Bitwise Operators
  • File Test Operators

Arithmetic Operators

Arithmetic operators in shell scripting are used to perform general arithmetic/ mathematical operations. There are 7 valid arithmetic operators in shell scripting −

  • Addition (+) is used to add two operands (variables).

  • Subtraction (-) is used to subtract two variables (operands) in shell scripting.

  • Multiplication (*) is used to multiply two variables (operands) in shell scripting.

  • Division (/) is used to divide two variables (operands) in shell scripting.

  • Modulus (%) is used to find the remainder on division of operands in shell scripting.

  • Increment operator (++) is used to add one to the current value of the operator.

  • Decrement operator (--) is used to subtract one from the current value of the operator.

Example

To show implementation of arithmetic operator in shell scripting −

a = 32
b = 23
   add = $((a + b))
   echo sum of a and b is $add
   sub = $((a - b))
   echo Subtraction of a and b is $sub
   mul = $((a * b))
   echo product of a and b is $mul
   div = $((a / b))
   echo division of a and b is $div
mod = $((a % b))
   echo remainder when a is divided b is $mod
((++a))
   echo Increment operator when applied on "a" results into a = $a
((--b))
echo Decrement operator when applied on "b" results into b = $b

Output

sum of a and b is 55
Subtraction of a and b is 9
product of a and b is 736
division of a and b is 1
remainder when a is divided b is 9
Increment operator when applied on a results into a = 33
Decrement operator when applied on b results into b = 24

Relational Operator

The relational operator in shell scripting defines the relations between operands. The return value of these are either true or false depending on the operator and operands. There are 6 types of valid relational operators in shell scripting −

  • == operator is the operator that equates the values of two operators. It returns true if the values are equal and returns false otherwise.

  • != operator is the operator that equates the values of two operators and check for their inequality. It returns true if the values are not equal and returns false otherwise.

  • < operator is the less than operator comparing the values of two operators. If first operend’s value is smaller than seconds one then operator returns true otherwise returns false.

  • <= operator is less than or equal to operator that compares the values of two operators. If first operend’s value is smaller than or equal to seconds one then operator returns true otherwise returns false.

  • >operator is the greater than operator comparing the values of two operators. If first operend’s value is larger than seconds one then operator returns true otherwise returns false.

  • >= operator is greater than or equal to operator that compares the values of two operators. If first operend’s value is larger than or equal to seconds one then operator returns true otherwise returns false.

Example

a = 32
b = 67
if(( $a==$b ))
then
   echo a is equal to b.
else
   echo a is not equal to b.
fi
if(( $a!=$b ))
then
   echo a is not equal to b.
else
   echo a is equal to b.
fi
if(( $a<$b ))
then
   echo a is less than b.
else
   echo a is not less than b.
fi

if(( $a<=$b ))
then
   echo a is less than or equal to b.
else
   echo a is not less than or equal to b.
fi
if(( $a>$b ))
then
   echo a is greater than b.
else
   echo a is not greater than b.
fi
if(( $a>=$b ))
then
   echo a is greater than or equal to b.
else
   echo a is not greater than or equal to b.
fi

Output

a is not equal to b.
a is not equal to b.
a is less than b.
a is less than or equal to b.
a is not greater than b.
a is not greater than or equal to b.

Boolean operator

Boolean operator also known as logical operators are used to perform logical operations in shell scripting. There are 3 types of valid logical operators in shell scripting −

  • Logical AND (&&) calculates the logic AND of the value that boolean. It returns true if both operands are true, otherwise false.

  • Logical OR (||) calculates logical OR operation of boolean operands. It returns false if both operands are false otherwise true.

  • Logical Not Equal to (!) calculates the negation of the single operator passed. If the value of operand is true it returns false otherwise true.

Example

a = true
b = false
if(($a == "true" & $b == "true" ))
then
   echo Both are true.
else
   echo Both are not true.
fi
if(($a == "true" || $b == "true" ))
then
   echo Atleast one of them is true.
else
   echo None of them is true.
fi
if(( ! $a == "true" ))
then
   echo "a" was intially false.
else
   echo "a" was intially true.
fi

Output

Both are not true.
Atleast one of them is true
a was intially true.

Bitwise operator are the operators that perform bitwise operations on bit variables. There are 6 types of bitwise operators in shell scripting −

  • Bitwise AND (&) is the operator that does the binary AND operation on the bits of the operands i.e. each bit of first variable is operated with respective bit of second operator.

  • Bitwise OR (|) is the operator that does the binary OR operation on the bits of the operands i.e. each bit of first variable is operated with respective bit of second operator.

  • Bitwise XOR (^) is the operator that does the binary XOR operation on the bits of the operands i.e. each bit of first variable is operated with respective bit of second operator.

  • Bitwise complement (~) is the operator that does the binary NOT operation on the bits of the operands i.e. each bit of first variable is operated with respective bit of second operator.

  • Bitwise Left Shift (<<) is the operator that shifts the bits of the operand to the left by n times specified at the right of the operator.

  • Bitwise Left Shift (>>) is the operator that shifts the bits of the operand to the right by n times specified at the right of the operator.

Example

a = 14
b = 67
bitwiseAND=$(( a&b ))

echo Bitwise AND of a and b is $bitwiseAND
bitwiseOR=$(( a|b ))

echo Bitwise OR of a and b is $bitwiseOR
bitwiseXOR=$(( a^b ))

echo Bitwise XOR of a and b is $bitwiseXOR
bitiwiseComplement=$(( ~a ))

echo Bitwise Compliment of a is $bitiwiseComplement
leftshift=$(( a<<1 ))

echo Left Shift of a is $leftshift
rightshift=$(( b>>1 ))

echo Right Shift of b is $rightshift

Output

Bitwise AND of a and b is 2
Bitwise OR of a and b is 79
Bitwise XOR of a and b is 77
Bitwise Compliment of a is -15
Left Shift of a is 28
Right Shift of b is 33

File Test Operator

The file test operators are used to test particular properties of the file. Some of the file test operators are :

  • -b operator is used to check if the specified file is a block special file or not. If the file is a block special file then the function returns true otherwise returns false.

  • -s operator is the operator that is used to check the size of the given file. If the file size is greater than 0, it returns true otherwise returns false.

  • -r operator is the operators that check if the access to read file contents is granted or not. If read access is granted than it returns true otherwise false.

  • -w operator is the operators that check if the access to write into file is granted or not. If write access is granted than it returns true otherwise false.

  • -x operator is the operators that check if the access to execute the file is granted or not. If execution access is granted than it returns true otherwise false.

raja
Published on 22-Nov-2019 11:47:01
Advertisements