Sunday, December 21, 2014

Shell Scripting : AND, OR operators

To use AND, OR operators in shell scripting, there are two ways to do this

1) -a for AND
    -o for OR

the usage is  [ exp1 -a exp2 ] , [ exp1 -o -exp2 ]
Single square bracket for using -a, -o

2) && for AND
    ||      for OR

the usage is [[ exp1 && exp2 ]] , [[exp1 || exp2]]
Double square bracket for using &&, ||


Eg1:

#!/bin/bash

a=4
b=5

if [ "$a" -ne "$b" -a "$a" -lt "$b" ]
then
   echo "$a is not equal to $b"

fi

Eg2:

#!/bin/bash

a=4
b=5

if [[ "$a" -ne "$b" && "$a" -lt "$b" ]]
then
   echo "$a is not equal to $b"
fi

No comments:

Post a Comment