Shell script that performs following string handling operations i) Calculate the length of the string ii) locate a position of a character in a string iii) extract last three characters from string

#!/bin/sh
str=tanu
echo “string:$str”
echo “lengh of string:”
z=`expr “$str”:’.*’`
echo $z
echo “substring is”
z=`expr “$str”:’.*\(…\)’`
echo $z
echo “position of character n is”
z=`expr “$str”:’[^n]*n’`
echo $z
*******************

output:

$sh a.sh
String:tanu
Length of string:4
Substring is anu
Position of character n is 3
********************

Leave a Reply