Shell Programming: Text Editors
Shell Programming: Text Editors
TEXT EDITORS :
STARTING emacs:
First we will begin by loading emacs .Do so by entering
the command prompt.
$ emacs filename
OUTPUT:
Enter any number 6
The entered number is 6
2. Program to find whether entered number is greater
than 100.
OUTPUT:
Enter a number
101
Entered number is greater than 100
3. Program to check whether a character is vowel or
not.
OUTPUT:
Enter a character
a
Character is vowel
4. Program to display the table of any number.
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
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
OUTPUT:
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
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
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
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