4. A departmental store announces its festival scheme to customers on cash payment. The scheme is as follows-
(
a
) If purchase amount is less than 1000 then Tax=2% and discount=10%.
(
b
) If purchase amount is greater than 1000 then Tax=5 % and discount=20%.
$vi prg4
clear
echo “enter purchase amount”
read pa
if [ $pa –lt 1000 ]
then
tax=‘echo $pa \* 2 /100 | bc’
discount=‘echo $pa \* 10 / 100 | bc’
else
tax=‘echo $pa \* 5 /100 | bc’
discount=‘echo $pa \* 20 / 100 | bc’ fi
amount=‘expr $pa + $tax - $discount’ echo cash payment =$amount
Sample Run
$sh prg4
enter purchase amount
3000
cash payment =2550
5. Write a shell script to perform an arithmetic operation upon two inputs. The operation should also be input by the user.
$vi prg5
clear
echo “enter a and b”
read a
read b
echo “enter operation to be performed”
read op
case $op in
+) c=‘expr $a + $b’ ;;
-) c=‘expr $a - $b’ ;;
/) c=‘expr $a / $b’ ;;
\*) c=‘expr $a \* $b’ ;;
*) echo “no valid operation specified” ;;
esac
echo Result after performing operation on a and b is echo $c
Sample Run
$sh prg5
enter a and b
4
3
enter operation to be performed
+
Result after performing operation on a and b is
7
$sh prg5
enter a and b
6
5
enter operation to be performed
-
Result after performing operation on a and b is
1
$sh prg5
enter a and b
2
3
enter operation to be performed
*
Result after performing operation on a and b is
6
$sh prg5
enter a and b
4
2
enter operation to be performed
/
Result after performing operation on a and b is
2
$sh prg5
enter a and b
4
5
enter operation to be performed
f
no valid operation specified
6. Write a shell script to find out the length of an input string.
$vi prg6
clear
echo “enter string”
read str
len=‘echo $str | wc –c’
len=‘expr $len – 1’
echo “length of string = $len”
Sample Run
$sh prg6
enter string
unix
length of string = 4
7. Write a shell script to find whether an input year is leap year or not.
$vi prg7
clear
echo “enter year”
read y
k=‘expr $y % 4’
if test $k –eq 0
then
echo “leap year”
else
echo “not a leap year”
fi
Sample Run
$sh prg7
enter year
2008
leap year
$sh prg7
enter year
2009
not a leap year
8. Make a duplicate copy of a specified file through command-line.
$vi prg8
clear
echo file to be copied : $1
echo new file name : $2
if test $# -lt 2 –o $# -gt 2
then
echo invalid
exit
fi
cp $1 $2
echo copy successful
Sample Run
$sh prg8 a1.txt a1.out
file to be copied : a1.txt
new file name : a1.out
copy successful
9. Write a shell script to concatenate two strings input by the user.
$vi prg9
clear
echo “enter two string
read str1
read str2
str3=‘echo $str1 $str2’
echo After concatenate : $str3
Sample Run
$sh prg9
enter two string
Shell
Programming
After concatenate : Shell Programming
10. Write a shell script to concatenate files.
$vi prg10
clear
cat>f1
cat>f2
cat f1 f2 >f3
cat f3
11. Program for command-line parameter & special variable.
$ vi prg11
clear
echo the name of the program is $0
echo the first parameter : $1
echo the second parameter : $2
echo the number of parameters are : $#
echo the parameters are : $*
Sample Run
$sh prg11 a s d f g
the name of the program is prg11
the first parameter : a
the second parameter : s
the number of parameters are : 5
the parameters are : a s d f g
12. Generate a table of an input integer.
$vi prg12
clear
echo “input number :”
read x
echo
for i in 1 2 3 4 5 6 7 8 9 10
do
t=‘expr $x \* $i’
echo $t
i=‘expr $i + 1’
done
Sample Run
$sh prg12
input number
4
4
8
12
16
20
24
28
32
36
40
13. Write a shell script to print all the multiplication tables (up to 10) between two given numbers.
$vi prg13
clear
i=1
j=10
echo enter lower limit
read low
echo enter higher limit
read high
while test $low –le $high
do
echo
echo Table of $low is
echo
while test $i –le $j
do
k=‘expr $low \* $i’
echo $low \* $i = $k
i=‘expr $i + 1’
done
i=1
low=‘expr $low + 1’ done
Sample Run
$sh prg13
enter lower limit
2
enter higher limit
4
Table of 2 is
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
Table of 3 is
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30
Table of 4 is
4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40
14. Write a shell script to find out the ny, where n and y must be input by the user.
$vi prg14
clear
echo “enter a number”
read n
echo “enter the power
read y
i=1
j=$n
while test $i –lt $y
do
j=‘expr $j \* $n’
i=‘expr $i + 1’
done
echo $j
Sample Run
$sh prg14
enter a number
4
enter the power
2
16