Unix & Shell Programming (Essential Programming part-2)
Computation (expr)
expr can perform the four basic arithmetic operations as well as moudus (remainder) function:
$ x=3 y=5
$ expr 3 + 5
$ expr $x - $y
-2
$ expr 3 \*5 Asterisk has to be escaped
15
$ expr $y / $x
$ expr 13 % 5 Decimal portion truncated
There must be whitespace on both side of operator in the expr command. expr is often used with
command substitution to assign a variable.
$ x=6 y=2 ; z=`expr $x + $y`
It is also used to increment the variable.
Z=`expr $z + 1`
String Handling
expr is also used to handle string. For manipulating strings, expr uses two expressions separated by a
colon. expr can perform three important string functions:
Determine the length of the string
Extract a substring
Locate the position of a character in a string
1|Page
Unix & Shell Programming (Essential Programming part-2)
The length of string
To find the length of string .* is used with expr command.
$ expr “sumit” : ‘.*’
Extract a Substring
expr can extract a string enclosed by the escaped characters \( and \).
Example:
If you want to extract 2-digit year from 4-digit string, you must create a pattern group and extract in this
way:
$ yr=2003
$ expr “$yr” : ‘..\(..\)’
03
It will take last two digit.
Locating position of a character
expr can also return the location of the first occurrence of a character inside a string.
Example:
To locate the position of the character c in the string “Welcome”, you have to count the number of
characters which are not c , i.e., [^c]*c, followed by c.
$str1=”Welcome” ; expr “$str1” : ‘[^c]*c’
2|Page
Unix & Shell Programming (Essential Programming part-2)
Programs
Script name: n_num.sh
Program 1: Display numbers upto n
echo –e “How many number you want to display: \c”
read n
i=1
while [ $i –le $n ]
do
echo $i
$i=`expr $i + 1`
done
output:
$ sh rev_str.sh
How many number you want to display: 5
3|Page
Unix & Shell Programming (Essential Programming part-2)
Program 2: Input string and reverse it
Script name : str_rev.sh
echo –e “Enter string: \c”
read str1
len1=`expr “$str1” : ‘.*’`
rev=””
while [ $len1 -ge 1 ]
do
ch1=`echo “$str1” | cut –c $len1`
rev=`echo $rev$ch1`
len1=`expr $len1 – 1`
done
echo $rev
output:
$ sh str_rev.sh
Enter string:welcome
emoclew
4|Page
Unix & Shell Programming (Essential Programming part-2)
Program 3: Get the word from file and reverse it
Script name: file_word_rev.sh
for str1 in `cat wordfile`
do
len1=`expr “$str1” : ‘.*’`
$rev=””
while [ len1 -ge 1 ]
do
$ch1=`echo “$str1” | cut –c $len1`
$rev=`echo $rev$ch1`
done
echo $rev
done
output:
$ cat > filedata
amit
malayalam
ravi
masam
$ sh file_word_rev.sh
tima
malyalam
ivar
5|Page
Unix & Shell Programming (Essential Programming part-2)
masma
Program 4: Input string and check wheter it is palindrome or not.
Script Name : string_palindrom.sh
echo –e “Enter string: \c”
read str1
len1=`expr “$str1” : ‘.*’`
$rev=””
while [ $len1 -ge 1 ]
do
$ch1=`echo “$str1” | cut –c $len1`
$rev=`echo $rev$ch1`
done
if [ $str1 = $rev ]
then
echo “The String is Palindrom”
else
echo “The String is not Palindrom”
fi
output:
Enter string: rohit
The String is not Palindrom
Enter string: masama
The String is Palindrom
6|Page
Unix & Shell Programming (Essential Programming part-2)
7|Page
Unix & Shell Programming (Essential Programming part-2)
Program 5: Read all words from file and display the palindroms words.
Script Name: file_word_palindrom.sh
echo “The palindrome strings are:”
for str1 in `cat wordfile`
do
len1=`expr “$str1” : ‘.*’`
$rev=””
while [ $len1 -ge 1 ]
do
$ch1=`echo “$str1” | cut –c $len1`
$rev=`echo $rev$ch1`
done
if [ $str1 = $rev ]
then
echo $rev
fi
done
output:
The palindrome strings are:
malayalam
masama
8|Page