SHELL PROGRAMMING
TEXT EDITORS :
The most common editors are Vi ,ED and emacs .Vi is
actually an advanced version of ED.
We will use the emacs editor because it provides additional features
that are not provided by other editors . Unlike Vi or ED ,emacs work in only
one mode ,so you don't need to worry about hitting the ESC key to enter a
command . You can type text directly in emacs after starting it.
STARTING emacs:
First we will begin by loading emacs .Do so by entering
the command prompt.
$ emacs filename
SAVING script : ctrl x and ctrl s
TO exit : ctrl x and ctrl c
TO Change mode : $ chmod 744 (adding ) filename .sh
or $ chmod a+x filename .sh
TO run : $ .\ filename .sh
or $ source filename .sh
1. Program to accept the number from the user &
display them
echo Enter any number
read num
echo The entered number is $num
OUTPUT:
Enter any number 6
The entered number is 6
2. Program to find whether entered number is greater
than 100.
echo Enter a number
read num
if test $num -gt 100
then
echo Entered number is greater than 100
else
echo Entered number is less than 100
fi
OUTPUT:
Enter a number
101
Entered number is greater than 100
3. Program to check whether a character is vowel or
not.
echo Enter a character
read ch
if [ $ch == 'a' -o $ch == 'e' -o $ch == 'i' -o $ch == 'o' -o $ch == 'u' ]
then
echo Character is vowel
else
echo Character is not a vowel
fi
OUTPUT:
Enter a character
a
Character is vowel
4. Program to display the table of any number.
echo Enter any number
read n
count=1
while [ $count -le 10]
do
result=`expr $count \* $n`
echo $n "*" $count "=" $result
count=`expr $count +1`
done
OUTPUT:
Enter any number
2
2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20
5. Program to check a no. is even or odd
echo enter the number
read n1
n2='expr $n1 % 2'
if [ $n2 == 0 ]
then
echo ENTERED NUMBER IS EVEN
else
echo ENTERED NUMBER IS ODD
fi
OUTPUT:
Enter the number
4
ENTERED NUMBER IS EVEN
6. Program to calculate the cube of 1-10.
num=1
while [ $num -le 10 ]
do
cube=`expr $num \* $num \* $num`
echo $cube
num=`expr $num + 1`
done
OUTPUT:
1
8
27
64
125
216
343
512
729
1000
7. Program to add, subtract, multiply or divide two
no.s using switch -case structure.
echo enter first no.
read n1
echo enter second no.
read n2
echo enter your choice
echo 1. to add
echo 2. to subtract
echo 3. to divide
echo 4. to multiply
read
case $REPLY in
1.)
n3='expr $n1 + $n2'
echo sum is $n3
;;
2.)
n4='expr $n1 _ $n2'
echo difference is $n4
;;
3.)
n5='expr $n1 / $n2'
echo division is $n5
;;
4.)
n6='expr $n1 \* $n2'
echo multiplication is $n6
;;
echo your choice is wrong
;;
esac
OUTPUT:
Enter the first number
2
Enter the second number
4
Enter your choice
1
Sum is 6
8. Program to print odd numbers less than 100.
num=1
echo $num
while [ $ num -lt 99]
do
num=`expr $num+2`
echo $num
done
OUTPUT:
1
3
5
7
9
:
:
:
95
97
99
9. Program to display the number 1-10.
num=1
while [ $num -le 10 ]
do
echo $num
num=`expr $num +1`
done
OUTPUT:
1
2
3
4
5
6
7
8
9
10
10. Program to find the factorial of any number
echo Enter the number
read n
fact=1
while [$n -gt 1]
do
fact=`expr $fact \* $n`
n=`expr $n - 1`
done
echo Factorial of the number is $fact
OUTPUT:
Enter the number
4
Factorial of the number is 12
11. Program to find the factorial of any number.
echo Enter the number
read n
fact=1
while [$n -gt 1]
do
fact=`expr $fact \* $n`
n=`expr $n - 1`
done
echo Factorial of the number is $fact
OUTPUT:
Enter the number
4
Factorial of the number is 12
12. Program to find greater of two numbers.
echo Enter the first number
read num1
echo Enter the second number
read num2
if test $num1 -gt num2
then
echo First number is greater
else
echo Second number is greater
fi
OUTPUT:
Enter the first number
3
Enter the second number
1
First number is greater
13. Program to find grade of a student by accepting his
marks as input.
echo Enter ur marks for three subjects
read m1
read m2
read m3
total=' expr $m1 + $m2 + $m3 '
echo TOTAL is $total
perc=' expr $total / 3 '
echo PERCENTAGE is $perc
if test $perc -ge 80
then
echo GRADE A
elif test $perc -ge 70 -a $perc -lt 80
then
echo GRADE B
elif test $perc -ge 60 -a $perc -lt 70
then
echo GRADE C
elif test $perc -ge 50 -a $perc -lt 60
then
echo GRADE D
elif test $perc -ge 40 -a $perc -lt 50
then
echo GRADE E
else
echo FAILED
fi
OUTPUT:
Enter ur marks for three subject
90
90
90
TOTAL IS 270
PERCENTAGE IS 90
GRADE A
14. Program to find area of a rectangle & a square
using switch -case structure
echo enter your choice
echo To find
echo 1. area of rectangle
echo 2. area of square
read case $REPLY in
1.)
echo enter the length & breadth of rectangle
read le
read br
area='expr $le \* $br'
echo area of rectangle is $area
;;
2.)
echo enter the length of side of square
read sq
area='expr $sq \* $sq'
echo area of square is $area
;;
echo your choice is wrong
;;
esac
OUTPUT:
enter your choice
To find
1. area of rectangle
2. area of square
2
enter the length of side of square
2
area of square is
4
15. Program to swap two numbers
echo Enter the First number
Read n1
echo Enter second number
Read n2
n3=`expr $n3 + $n2`
n1=`expr $n3 - $n1`
n2=`expr $n3 - $n2`
echo swapped numbers are a= $n1 and b= $n2
OUTPUT:
enter first number 5
enter second number 10
swapped numbers are a=10 and b=5
16. Program to input a number and then display the
sum of its digits
Echo enter the number
Read num
While [ $num –ge 0 ]
Do
m=`expr $num%10`
num=`expr $num/10`
s=`expr $s + $m`
done
echo Sum of digits is $s
OUTPUT:
Sum of digits is 9
enter the number 45
17. Program to enter a number and display the number
in the reverse order.
Echo enter the number to be reversed
Read num
While [ $num –ge 0 ]
m=`expr $num%10`
num=`expr $num/10`
echo reversed number is $m
done
OUTPUT:
enter the number to be reversed 54
reversed number is 45
18. Program to display all the odd numbers less than
100
Echo list of odd numbers less than 100 are:
i=1
While [$I –lt 100 ]
Do
Echo $i
i=`expr $i + 2`
done
OUTPUT:
1
3
5
7
9
…
…
…
…
…
…
99
19. Program to illustrate the working of SAVING
BANK account.
Echo enter your account number
Read m
am=0
wd=0
:loop
echo what do you want to do
echo 1.Deposit
echo 2.Withdrawl
echo 3.Exit
read n
if [ $n –eq 1 ]
then
echo enter the amount to be deposited
read am
s=`expr $am`
echo your balance is $s
goto loop
elif [$n –eq 2 ]
then
echo enter the amount to be withdrawn
read wd
s=`expr $s - $wd `
if [ $s –ge 0 ]
then
echo your balance is $s
else
echo Invalid amount
fi
goto loop
else
echo Thanks for accessing services of BANK OF KANGALS
fi
OUTPUT:
enter your account no pb08ab0367
what do u want to do
1.Deposit
2.Withdrawl
3.Exit
1
enter amout to be deposited 10
your balance is Rs. 10