Monday, 15 October 2012

Practical no-4: Write a UNIX Shell script to Execute various oprations on string


Practical no-4: Write a UNIX Shell script to Execute various oprations on string
clear
i="y"
a=0
t=0
while [ $i = "y" ]

do
clear
echo "1.Compare 2 strings :"
echo "2.Concatanet string"
echo "3.Find length of string"
echo "4.Occurance of word"
echo "5.Reverse of string"
echo "6.Exit"
echo "Enter your choice"
read ch
case $ch in
    1)echo "Enter first String"
     read s1
     echo "Enter second string "
     read s2
     if [ $s1 = $s2 ]
     then   
        echo "Two strings are equal "else
        echo "Two strings are not equal"
     fi;;

    2)echo "Enter one string "
      read s1
     echo "Enter second string "
     read s2
     echo $s1 $s2;;

    3)echo "Enter any String"
     read s1
     t=`echo $s1|wc -c`
     t=`expr $t - 1`
     echo "Length of "$s1" is "$t;;

    4)echo "Enter any String "
     read s1
     echo "Enter word u want to find occurance of:"
     read c1
     t=`echo $s1|wc -c`
     t=`expr $t - 1`
     echo "length "$t
         while [ $t -ne 0 ]
     do
          temp=`echo $s1|cut -c $t`
      temp2=`echo $temp2 $temp`
          #echo $temp2
       if [ $temp2 = $c1 ]
       then
        a=`expr $a + 1`
        t=`expr $t - 1`
       else
        t=`expr $t - 1`
       fi
    done
     echo "Occurance of "$c1" is "$a;;

    5)echo "Enter any string :"
      read s1
       t=`echo $s1|wc -c`
           t=`expr $t - 1`
           echo "length "$t
           while [ $t -ne 0 ]
       do
         temp=`echo $s1|cut -c $t`
         echo $temp
         temp2=`echo $temp2 $temp`
         t=`expr $t - 1`
      done
      echo $temp2;;

    6)exit;;
    *)echo "Invalid choice";;
esac
echo "Do u want to continue ?"
read i
if [ $i != "y" ]
then
    exit
fi
done

OUTPUT
***********
[root @LINTEL]$ sh string.sh
1.Compare 2 strings :
2.Concatanet string
3.Find length of string
4.Occurance of word
5.Reverse of string
6.Exit
Enter your choice1
Enter first String String1
Enter second string String2
Two strings are not equal

Practical no-2 :Shell Script to make a menu driven oprations


Practical no-2 :Shell Script to make a menu driven oprations

clear
sum=0
i="y"

echo " Enter one no."
read n1
echo "Enter second no."
read n2

while [ $i = "y" ]
do
echo "1.Addition"
echo "2.Subtraction"
echo "3.Multiplication"
echo "4.Division"
echo "Enter your choice"
read ch
case $ch in
    1)sum=`expr $n1 + $n2`
     echo "Sum ="$sum;;

    2)sum=`expr $n1 - $n2`
     echo "Sub = "$sum;;

    3)sum=`expr $n1 \* $n2`
     echo "Mul = "$sum;;

    4)sum=`expr $n1 / $n2`
     echo "Div = "$sum;;

    *)echo "Invalid choice";;
esac
echo "Do u want to continue ?"
read i
if [ $i != "y" ]
then
    exit
fi
done   









OUTPUT
***********
[root @LINTEL]$ sh calculator.sh
Enter any no.
121
 Enter one no.
 21
 Enter second no.
 58
 1.Addition
 2.Subtraction
 3.Multiplication
 4.Division
 Enter your choice
 1
 Sum =79
 Do u want to continue ?
 y
 1.Addition
 2.Subtraction
 3.Multiplication
 4.Division
 Enter your choice
 2
 Sub = -37
 Do u want to continue ?
 y
 1.Addition
 2.Subtraction
 3.Multiplication
 4.Division
 Enter your choice
 3
 Mul = 1218
 Do u want to continue ?
 y
 1.Addition
 2.Subtraction
 3.Multiplication
 4.Division
 Enter your choice
 4
 Div = 0
 Do u want to continue ?
 n