Menu Driven Shell Program to Perform Following Functions
1.Factorial of number
2.Greatest number of 3
3.Prime number or not
4.number is palindrome or not
5.string is palindrome or not
*******************************************************************************
ans="y"
while [ $ans = "y" ] #loops till answer is yes
do
echo "-----MENU-----" #prints menu
echo "1.Factorial of number"
echo "2.Greatest number of 3"
echo "3.Prime number or not"
echo "4.number is palindrome or not"
echo "5.string is palindrome or not"
echo "6.Exit"
echo –n "enter your choice :"
read ch #reads users choice
case $ch in
1)echo –n "Enter a number: "
read num
i=2
res=1
if [ $num -ge 2 ]
then
while [ $i -le $num ] #calculates factorial of given number
do
res=`expr $res \* $i`
i=`expr $i + 1`
done
fi
echo "Factorial of $num = $res"
;;
2)echo –n "Enter the 1st number :"
read num1
echo –n "Enter the 2nd number :"
read num2
echo –n "Enter the 3rd number :"
read num3
if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ] #compare 3
numbers
then
echo "$num1 is biggest number!!!"
elif [ $num2 -gt $num3 ]
then
echo "$num2 is biggest number!!!"
else
echo "$num3 is biggest number!!!"
fi
;;
3)i=2
flag=0
echo –n "Enter the number :"
read num
while [ $i -lt `expr $num - 1` ] #check whether number is prime
do
if [ `expr $num % $i` = 0 ]
then
flag=1 #if number is prime sets the flag
fi
i=`expr $i + 1`
done
if [ $flag = 1 ]
then
echo "$num is not prime."
else
echo "$num is prime."
fi
;;
4)l=0
cnt=1
tag=0
echo –n "Enter a number :"
read num
l=`echo $num|wc -c`
l=`expr $l - 1`
lh=`expr $l / 2`
while [ $cnt -le $lh ] #loops till half length of number
do
c1=`echo $num|cut -c $cnt`
c2=`echo $num|cut -c $l`
if [ $c1 != $c2 ] #compare first and last number
then
cnt=$lh
tag=1
fi
cnt=`expr $cnt + 1`
l=`expr $l - 1`
done
if [ $tag -eq 0 ]
then
echo "Number is Palindrome"
else
echo “Number is not Palindrome”
fi
;;
5)l=0
cnt=1
tag=0
echo –n "Enter a String :"
read str
l=`echo $str|wc -c`
l=`expr $l - 1`
lh=`expr $l / 2`
while [ $cnt -le $lh ] #ckecks whether string is palindrome
do
c1=`echo $str|cut -c $cnt`
c2=`echo $str|cut -c $l`
if [ $c1 != $c2 ]
then
cnt=$lh
tag=1
fi
cnt=`expr $cnt + 1`
l=`expr $l - 1`
done
if [ $tag -eq 0 ]
then
echo "String is Palindrome"
else
echo "String is not Palindrome"
fi
;;
6)exit;; #exit from shell script
*)echo "Please enter correct choice !!!";;
esac
echo –n "Do you want to continue?"
read ans
done
echo "Thank you!!!"
while [ $ans = "y" ] #loops till answer is yes
do
echo "-----MENU-----" #prints menu
echo "1.Factorial of number"
echo "2.Greatest number of 3"
echo "3.Prime number or not"
echo "4.number is palindrome or not"
echo "5.string is palindrome or not"
echo "6.Exit"
echo –n "enter your choice :"
read ch #reads users choice
case $ch in
1)echo –n "Enter a number: "
read num
i=2
res=1
if [ $num -ge 2 ]
then
while [ $i -le $num ] #calculates factorial of given number
do
res=`expr $res \* $i`
i=`expr $i + 1`
done
fi
echo "Factorial of $num = $res"
;;
2)echo –n "Enter the 1st number :"
read num1
echo –n "Enter the 2nd number :"
read num2
echo –n "Enter the 3rd number :"
read num3
if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ] #compare 3
numbers
then
echo "$num1 is biggest number!!!"
elif [ $num2 -gt $num3 ]
then
echo "$num2 is biggest number!!!"
else
echo "$num3 is biggest number!!!"
fi
;;
3)i=2
flag=0
echo –n "Enter the number :"
read num
while [ $i -lt `expr $num - 1` ] #check whether number is prime
do
if [ `expr $num % $i` = 0 ]
then
flag=1 #if number is prime sets the flag
fi
i=`expr $i + 1`
done
if [ $flag = 1 ]
then
echo "$num is not prime."
else
echo "$num is prime."
fi
;;
4)l=0
cnt=1
tag=0
echo –n "Enter a number :"
read num
l=`echo $num|wc -c`
l=`expr $l - 1`
lh=`expr $l / 2`
while [ $cnt -le $lh ] #loops till half length of number
do
c1=`echo $num|cut -c $cnt`
c2=`echo $num|cut -c $l`
if [ $c1 != $c2 ] #compare first and last number
then
cnt=$lh
tag=1
fi
cnt=`expr $cnt + 1`
l=`expr $l - 1`
done
if [ $tag -eq 0 ]
then
echo "Number is Palindrome"
else
echo “Number is not Palindrome”
fi
;;
5)l=0
cnt=1
tag=0
echo –n "Enter a String :"
read str
l=`echo $str|wc -c`
l=`expr $l - 1`
lh=`expr $l / 2`
while [ $cnt -le $lh ] #ckecks whether string is palindrome
do
c1=`echo $str|cut -c $cnt`
c2=`echo $str|cut -c $l`
if [ $c1 != $c2 ]
then
cnt=$lh
tag=1
fi
cnt=`expr $cnt + 1`
l=`expr $l - 1`
done
if [ $tag -eq 0 ]
then
echo "String is Palindrome"
else
echo "String is not Palindrome"
fi
;;
6)exit;; #exit from shell script
*)echo "Please enter correct choice !!!";;
esac
echo –n "Do you want to continue?"
read ans
done
echo "Thank you!!!"