read num
b=0
while test $num -gt 0
do
a=‘expr $num % 10’
b=‘expr \( $b + $a \) \* 10’
num=‘expr $num / 10’
done
b=‘expr $b / 10’ echo reverse=$b
Sample Run
$sh prg27
enter any integer
123
reverse=321
28. Sort the given numbers in the given order, i.e., either in ascending or descending order.
$vi prg28
clear
ans=y
while test $ans = y
do
echo Enter no. of elements to be sorted
read no
echo Enter $no elements
i=1
rm sort1
while test $i –le $no
do
read n
‘echo $n >> sort1’
i=‘expr $i + 1’
done
clear
echo input order of sorting
echo 1.Ascending
echo 2.Descending
echo enter choice
read ch
clear
case $ch in
1) sort –n sort1>file1
echo Inputted elements in Ascending order:
cat file1 ;;
1) sort –r sort1>file1
echo Inputted elements in Descending order:
cat file1 ;;
1) echo “Invalid Input” ;;
esac
echo continue…….y/n
read ans
done
Sample Run
$sh prg28
Enter no. of elements to be sorted
4
Enter 4 elements
3
5
2
1
input order of sorting
1.Ascending Press 1
2.Descending Press 2
enter choice
1
Inputted elements in Ascending order:
1
2
3
5
continue…….y/n
y
Enter no. of elements to be sorted
5
Enter 5 elements
4
6
1
3
3
input order of sorting
1.Ascending Press 1
2.Descending Press 2
enter choice
2
Inputted elements in Descending order:
6
4
3
3
1
continue…….y/n
n
29. Write a shell script to compare two strings input by the user for equality.
$vi prg29
clear
echo enter string1
read str1
echo enter string2
read str2
if test $str1 = $str2
then
echo strings are equal
else
echo strings are not equal
fi
Sample Run
$sh prg29
enter string1
abc
enter string2
abc
strings are equal
$sh prg29
enter string1
xyz
enter string2
abc
strings are not equal
30. Write a shell script to print the characters of an input string into reverse order.
$vi prg30
clear
echo enter any string
read str
len=‘echo $str | wc -c’
len=‘expr $len - 1’
while test $len -ne 0
do
i=‘echo $str | cut -c $len’
a=$a$i
len=‘expr $len - 1’
done
echo reverse is $a
Sample Run
$sh prg30
enter any string
programming
reverse is gnimmargorp
31. Write a shell script to tell whether input string is palindrome or not.
$vi prg31
clear
echo enter any string
read str
len=‘echo $str | wc –c’
len=‘expr $len -1’
while test $len –ne 0
do
i=‘echo $str | cut –c $len’
a=$a$i
len=‘expr $len -1’
done
if test $str = $a
then
echo String is Palindrome
else
echo String is not Palindrome
fi
Sample Run
$sh prg31
enter any string
cmc
String is Palindrome
$sh prg31
enter any string
abc
String is not Palindrome
32. Write a shell script to find out the location of an input character into an input string.
$vi prg32
clear
echo enter any string
read str
echo enter character
read c
len=‘echo $str | wc –c’
len=‘expr $len – 1’
i=1
while test $i –le $len
do
a=‘echo $str | cut –c $i’
if test $a = $c
then
echo Position=$i
fi
i=‘expr $i + 1’
done
Sample Run
$sh prg32
enter any string
Programming
enter character
g
Position=4
Position=11
33. Write a shell script to count the number of characters, words, spaces in a given text.
$vi prg33
clear
echo “enter text”
read t
w=‘expr $t | wc –w’
c=‘expr $t | wc –c’
c=‘expr $c - 1’
s=‘expr $w – 1’
echo characters = $c
echo words = $w
echo spaces = $s
Sample Run
$sh prg33
enter text
that is a table
characters = 15
words = 4
spaces = 3
34. Write a shell script to print Fibonacci series.
$vi prg34
clear
echo enter the last element of series
read n
echo
a=0
b=1
echo $a
echo $b
i=1
while test $i –lt $n
do
c=‘expr $a + $b’
if test $c –gt $n
then
exit
fi
echo $c
a=$b
b=$c
i=‘expr $i + 1’
done
Sample Run
$sh prg34
enter value of series
5
0
1
1
2
3
5
35. Write a shell script to translate the contents of a file into UPPER CASE, where file name is entered through command line.
$vi prg35
clear
if test $# -eq 0
then
echo “No argument”
exit
fi
while test $# -gt 0
do
if test –s $1
then
if test –f $1
then
cat $1 | tr a-z A-Z >$1.up
cat $1.up
fi
else
echo $1 is not a file
fi
shift
done
echo Translation successful
Sample Run
$sh prg35 file.txt
WELCOME
HELLO
Translation successful
In file.txt, welcome and hello are written in small letters. After running this program, welcome and hello are converted in capital letters and saved in 1.up file
36. Write a shell script to perform following tasks-
(
a
)
Display the present working directory.
(
b
)
Clear the screen.
(
c
)
Display the current date.
(
d
)
Make a directory with its sub-directory d1.
(
e
)
Change the directory to the directory having sub directory d1.
(
f
)
Create two files (say file1 & file2) within this.
(
g