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

Shell Programs

The document provides examples of shell arithmetic, relational, and string operators, including their descriptions and usage in scripts. It illustrates how to perform operations like addition, subtraction, and comparisons between variables. Additionally, it includes examples of control flow structures such as 'until', 'while', and 'for' loops in shell scripting.

Uploaded by

viruuuuuuu.007
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)
3 views

Shell Programs

The document provides examples of shell arithmetic, relational, and string operators, including their descriptions and usage in scripts. It illustrates how to perform operations like addition, subtraction, and comparisons between variables. Additionally, it includes examples of control flow structures such as 'until', 'while', and 'for' loops in shell scripting.

Uploaded by

viruuuuuuu.007
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

Shell Arithmetic Operators Example

Operator Description Example


`expr $a + $b` will give
+ (Addition) Adds values on either side of the operator
30
Subtracts right hand operand from left hand `expr $a - $b` will give -
- (Subtraction)
operand 10
`expr $a \* $b` will give
* (Multiplication) Multiplies values on either side of the operator
200
/ (Division) Divides left hand operand by right hand operand `expr $b / $a` will give 2
Divides left hand operand by right hand operand
% (Modulus) `expr $b % $a` will give 0
and returns remainder
a = $b would assign value
= (Assignment) Assigns right operand in left operand
of b into a
Compares two numbers, if both are same then [ $a == $b ] would return
== (Equality)
returns true. false.
Compares two numbers, if both are different [ $a != $b ] would return
!= (Not Equality)
then returns true. true.
a=10
b=20

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

You might also like