postheadericon Write a shell script to Perform Following String Operations


Definition : Write A Script To Perform Following String Operations Using Menu: 

                       1. COMPARE TWO STRINGS.
                       2. JOIN TWO STRINGS.
                       3. FIND THE LENGTH OF A GIVEN STRING.
                       4. OCCURRENCE OF CHARACTER AND WORDS
                       5. REVERSE THE STRING.

File Name : ex18.sh


clear
choice=y
while [ "$choice" = "y" ]
do
echo "____________________________________________"
echo "1. COMPARE TWO STRINGS"
echo "2. JOIN TWO STRINGS"
echo "3. FIND THE LENGTH OF A GIVEN STRING"
echo "4. OCCURRENCE OF CHARACTER AND WORDS"
echo "5. REVERSE THE STRING"
echo "6. EXIT"
echo "____________________________________________"
echo "Enter Choice: \c"
read ch
echo "____________________________________________"
case $ch in
1)
        echo "Enter String1: \c"
        read str1
        echo "Enter String2: \c"
        read str2

        if [ $str1 = $str2 ]
        then
                echo "String is equal"
        else
                echo "String is not equal"
        fi
        ;;
2)
        echo "Enter String1: \c"
        read str1
        echo "Enter String2: \c"
        read str2

        str3=$str1$str2
        echo "Join String: \c" $str3
        ;;
3)
        len=0
        echo "Enter String1: \c"
        read str1
            len=$(echo "$str1" | wc -c)
            len=`expr $len - 1`
        echo "Length: " $len
        ;;
4)
        echo "Enter String: \c"
        read str

        echo "Enter Word to find : \c"
        read word

        echo $str | cat > str1.txt

        grep -o $word str1.txt | cat > str2.txt
        count=`grep -c $word str2.txt`
            echo "Count:  \c"$count
    ;;
5)
        echo "Enter String1: \c"
        read str

        len=`expr $str | wc -c`
        len=`expr $len - 1`
        while [ $len -gt 0 ]
        do
                rev=`expr $str | cut -c $len`
                ans=$ans$rev
                len=`expr $len - 1`
        done
        echo "Reverse String: \c"$ans
        ;;

6)         exit      ;;

*)         echo "Invalid Choice ....."                    ;;
            esac
 echo "Do u want to continue.....? [y/n]"
 read choice
 case $choice in
  Y|y) choice=y;;
  N|n) choice=n;;
  *) choice=y;;
 esac
done

 

0 comments:

Blog Archive

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.