Shell Programs
Shell Programs
val=`expr $a + $b`
echo "a + b : $val"
val=`expr $a - $b`
echo "a - b : $val"
val=`expr $a \* $b`
echo "a * b : $val"
val=`expr $b / $a`
echo "b / a : $val"
val=`expr $b % $a`
echo "b % a : $val"
if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
then
echo "a is not equal to b"
fi
Shell Relational Operators Example
Operator Description Example
Checks if the value of two operands are equal or not; if yes,
-eq [ $a -eq $b ] is not true.
then the condition becomes true.
Checks if the value of two operands are equal or not; if values
-ne [ $a -ne $b ] is true.
are not equal, then the condition becomes true.
Checks if the value of left operand is greater than the value of
-gt [ $a -gt $b ] is not true.
right operand; if yes, then the condition becomes true.
Checks if the value of left operand is less than the value of
-lt [ $a -lt $b ] is true.
right operand; if yes, then the condition becomes true.
Checks if the value of left operand is greater than or equal to
-ge the value of right operand; if yes, then the condition becomes [ $a -ge $b ] is not true.
true.
Checks if the value of left operand is less than or equal to the
-le [ $a -le $b ] is true.
value of right operand; if yes, then the condition becomes true.
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
Shell String Operators Example
Operator Description Example
Checks if the value of two operands are equal or not; if
= [ $a = $b ] is not true.
yes, then the condition becomes true.
Checks if the value of two operands are equal or not; if
!= [ $a != $b ] is true.
values are not equal then the condition becomes true.
Checks if the given string operand size is zero; if it is zero
-z [ -z $a ] is not true.
length, then it returns true.
Checks if the given string operand size is non-zero; if it is
-n [ -n $a ] is not false.
nonzero length, then it returns true.
Checks if str is not the empty string; if it is empty, then it
str [ $a ] is not false.
returns false.
a="abc"
b="efg"
if [ $a = $b ]
then
echo "$a = $b : a is equal to b"
else
echo "$a = $b: a is not equal to b"
fi
if [ $a != $b ]
then
echo "$a != $b : a is not equal to b"
else
echo "$a != $b: a is equal to b"
fi
if [ -z $a ]
then
echo "-z $a : string length is zero"
else
echo "-z $a : string length is not zero"
fi
if [ -n $a ]
then
echo "-n $a : string length is not zero"
else
echo "-n $a : string length is zero"
fi
if [ $a ]
then
echo "$a : string is not empty"
else
echo "$a : string is empty"
fi
#!/bin/bash
i=1
until [ $i -gt 6 ]
do
echo "Welcome $i times."
i=$(( i+1 ))
done
#!/bin/bash
n=1
while (( $n <= 5 ))
do
echo "Welcome $n times."
n=$(( n+1 ))
done
for var in 0 1 2 3 4 5 6 7 8 9
do
echo $var
done