Logo 
Search:

Unix / Linux / Ubuntu Articles

Submit Article
Home » Articles » Unix / Linux / Ubuntu » Homework HelpRSS Feeds

Shell script to perform operations like compare string, concatenate, find length, occurrence of word in a string and reverse a string

Posted By: Jack Bouchard     Category: Unix / Linux / Ubuntu     Views: 15922

Write a shell script to perform operations like compare strings, concatenate strings, find length of a string, occurrence of word in a string and reverse a string

Code for Shell script to perform operations like compare string, concatenate, find length, occurrence of word in a string and reverse a string in Unix / Linux / Ubuntu

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

  
Share: 



Jack Bouchard
Jack Bouchard author of Shell script to perform operations like compare string, concatenate, find length, occurrence of word in a string and reverse a string is from Montreal, Canada.
 
View All Articles

Related Articles and Code:


 

Other Interesting Articles in Unix / Linux / Ubuntu:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!