Shell Programming With Linux
Shell Programming With Linux
Shailendra Tiwari
"echo" Command
Syntax:
if condition
then
if condition
then
.....
..
do this
else
....
..
do this
fi
else
...
.....
do this
fi
Example:
$ vi nestedif.sh
osch=0
echo "1. Unix (Sun Os)"
echo "2. Linux (Red Hat)"
echo -n "Select your os choice [1 or 2]? "
read osch
if [ $osch -eq 1 ] ; then
echo "You Pick up Unix (Sun Os)"
else #### nested if i.e. if within if ######
if [ $osch -eq 2 ] ; then
echo "You Pick up Linux (Red Hat)"
else
echo "What you don't like Unix/Linux OS."
fi
fi
Run the above shell script as follows:
• $ chmod +x nestedif.sh
$ ./nestedif.sh
1. Unix (Sun Os)
2. Linux (Red Hat)
Select you os choice [1 or 2]? 1
You Pick up Unix (Sun Os)
• $ ./nestedif.sh
1. Unix (Sun Os)
2. Linux (Red Hat)
Select you os choice [1 or 2]? 2
You Pick up Linux (Red Hat)
• $ ./nestedif.sh
1. Unix (Sun Os)
2. Linux (Red Hat)
Select you os choice [1 or 2]? 3
What you don't like Unix/Linux OS.
Exercises:
1. Input basic salary of an employee through keyboard. Calculate
Gross_Salary, Deduction & Net_Salary with the following
conditions.
a. If Salary is less than 5000, DA = 2.5% of basic, HRA = 5% of
basic, PF = 2.5% of basic, Tax = 750.
b. If Salary is between 5000 and 15000, DA = 4% of basic, HRA =
10% of basic, PF = 3.75% of basic, Tax = 10%.
c. If Salary is between 15000 and 30000, DA = 6.5% of basic,
HRA = 14% of basic, PF = 5% of basic, Tax = 12%.
d. If Salary is between 30000 and 50000, DA = 8% of basic, HRA
= 15% of basic, PF = 6.25% of basic, Tax = 14%.
e. If Salary is greater than 50000, DA = 10% of basic, HRA = 20%
of basic, PF = 7.5% of basic, Tax = 16.5%.
(GS = BASIC + DA + HRA, Deduct = PF + Tax & NS = GS – Deduct).
2. Input the marks obtained by a student in five different
subjects through keyboard. Find the total and percentage.
Using If .. Else statements determine the Grade of the
student. Also check if marks of any of the subject is less than
40 then “FAIL” should be displayed as result.
Exercise:
• Write a shell script to sort an array in
ascending order.
#Script to find the maximum number from an array
# Declare the array of 5 subscripts to hold 5 numbers
#
declare nos[5]=(4 -1 2 66 10)
#
# Prints the array
#
echo "Numbers in array:"
for (( i = 0; i <= 4; i++ ))
do
echo ${nos[$i]}
done
#
# Finding maximum value
#
max=${nos[0]}
for (( i = 0; i <= 4 ; i++ ))
do
if [ $max -lt ${nos[$i]} ]; then
max=${nos[$i]}
fi
done
echo “The maximum value in an array is $max”
The case Statement
• The case statement is good alternative to Multilevel if-then-else-fi statement. It enables you to
match several values against one variable. Its easier to read and write.
Syntax:
• case $variable-name in
pattern1) command
...
..
command;;
pattern2) command
...
..
command;;
patternN) command
...
..
command;;
*) command
...
..
command;;
esac
echo -n "Enter A, B, or C: "
read letter
case "$letter" in
a|A)echo "You entered A"
;;
b|B)echo "You entered B"
;;
c|C)echo "You entered C"
;;
*)echo "You did not enter A, B, or C"
;;
esac
• Note that esac is always required to indicate end
of case statement.
Simple Calculator
(
while [ $ != 'x' ]
do
echo "Welcome to calculator (x to quit)"
echo "Enter the first operand: "
read value1
echo "Enter an operator (+, -, *, /): "
read operator
echo "Enter the second operand: "
read value2
if [ " $ operator " = " + " ] ; then
answer=$(echo "scale=2;value1+value2" |bc);
elif [ " $ operator " = " - " ] ; then
answer=$(echo " scale=2;value1-value2" |bc);
elif [ " $ operator " = " / " ] ; then
answer=$(echo "scale=2;value1/value2" |bc);
elif [ " $ operator " = " * " ] ; then
answer=$(echo "scale=2;value1*value2" |bc);
elif [ " $ operator " = " % " ] ; then
answer=$(echo "scale=2;value1%value2" |bc);
fi
echo "Answer: $value1 $operator $value2 = $answer";
done
)