echo Enter Salary of an Employee
read sal
if [ $sal -ge 5000 ]
then
bonus=‘echo $sal \* .05 | bc’
else
bonus=250
fi
echo bonus is: $bonus
$sh prg25
Enter Salary of an Employee
6500
bonus is: 325.00
$sh prg25
Enter Salary of an Employee
7000
bonus is: 350.00
$sh prg25
Enter Salary of an Employee
3500
bonus is: 250
26. Write a shell script to find out greatest among n input integers where n is to be input by the user.
$vi prg26
clear
echo “Enter number of integers”
read n
echo “enter value of integer number 1”
read j
i=2
while test $i -le $n
do
echo enter value of integer number $i
read k
if [ $j -lt $k ]
then
j=$k
fi
i=‘expr $i + 1’
done
echo Greatest input is $j
$sh prg26
Enter number of integers
5
enter value of integer number 1
8
enter value of integer number 2
3
enter value of integer number 3
22
enter value of integer number 4
44
enter value of integer number 5
11
Greatest input is 44
27. Write a shell script to read an integer and print its digits in reverse order.
$vi prg27
clear
echo “enter any integer”
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
$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
$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
$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
$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
$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
$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
$sh prg33
enter text
that is a table
characters = 15
words = 4
spaces = 3