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
$sh prg14
enter a number
4
enter the power
2
16
15. Write a shell script to find out the factorial of an input.
$vi prg15
clear
i=1
j=1
echo “enter the number”
read num
while test $i –le $num
do
k=‘expr $i \* $j’
i=‘expr $i + 1’
j=$k
done
echo Factorial of $num is $j
$sh prg15
enter the number
4
Factorial of 4 is 24
16. Write a shell script to generate the series of even number from 0 to n. 0 2 4 n
$vi prg16
clear
echo “enter value of n”
read n
i=0
while test $i –le $n
do
printf “ $i”
i=‘expr $i + 2’
done
echo
$sh prg16
enter value of n
5
0 2 4
17. Write a shell script to check whether an input is a prime or not.
$vi prg17
clear
echo “enter number”
read num
i=2
while test $i –lt $num
do
k=‘expr $num / $i’
if test $k –eq 0
then
echo “number is not prime”
exit
fi
i=‘expr $i + 1’
done
echo “number is prime”
$sh prg17
enter number
4
number is not prime
$sh prg17
enter number
7
number is prime
18. Write a shell script to generate the primes between two given numbers.
$vi prg18
clear
echo “enter two numbers”
read a
echo
if [ $a -eq 0 -a $a -eq 1 ]
then
a=2
fi
read b echo
while test $a -le $b do
i=2
while test $i -lt $a
do
k=‘expr $a % $i’
if test $k -eq 0
then
break
fi
i=‘expr $i + 1’
done
if [ $i -eq $a ]
then
echo $a fi
a=‘expr $a + 1’
done
$sh prg18
enter two numbers
22
2
3
5
7
11
13
17
19
19. Write a shell script to find out the sum of series 1+2+3+………….n, where n is input by the user.
$vi prg19
clear
echo “enter value of n”
read n
i=1
sum=0
while test $i –le $n
do
sum=‘expr $sum + $i’
i=‘expr $i + 1’
done
echo Sum of series is $sum
$sh prg19
enter value of n
12
Sum of series is 78
20. Write a shell script to generate the series 2,4,6,8,…………n, where n must be input by the user.
$vi prg20
clear
echo enter value of n
read n
echo
i=2
while test $i –lt $n
do
printf “ $i, “
i=‘expr $i + 2’
done
printf “ $i”
echo
$sh prg20
enter value of n
21
2, 4, 6, 8, 10, 12, 14, 16, 18, 20
21. Write a shell script to generate the series 1, 5, 2, 10, 3, 15,………….50.
$vi prg21
clear
a=1
b=5
while [ $b –le 50 ]
do
printf “ $a”
printf “, $b”
a=‘expr $a + 1’
b=‘expr $b + 5’
done
echo
$sh prg21
1, 5, 2, 10, 3, 15, 4, 20, 5, 25, 6, 30, 7, 35, 8, 40, 9, 45, 10, 50
22. Write a shell script to generate the series 1+1/2+1/3+……………+1/n.
$vi prg22
clear
echo enter value of n
read n
echo
i=2
printf “1+”
while test $i -lt $n
do
printf “1/$i+”
i=‘expr $i + 1’
done
printf “1/$i”
echo
$sh prg22
enter value of n
12
1+1/2+1/3+1/4+1/5+1/6+1/7+1/8+1/9+1/10+1/11+1/12
23. Write a shell script to generate the series ½+2/3+3/4+……………n-1/n.
$vi prg23
clear
echo enter value of n
read n
echo
b=1
c=2
a=1
n=‘expr $n - 1’
while test $a -lt $n
do
printf $b/$c+
b=‘expr $b + 1’
c=‘expr $c + 1’
a=‘expr $a + 1’
done
printf $b/$c
echo
$sh prg23
enter value of n
12
1/2+2/3+3/4+4/5+5/6+6/7+7/8+8/9+9/10+10/11+11/12
24. Write a shell script to find out the sum of series 12+22+32+……………n2.
$vi prg24
clear
echo “enter value of n”
read n
i=1
sum=0
while test $i –le $n
do
k=‘expr $i \* $i’
sum=‘expr $sum + $k’
i=‘expr $i + 1’
done
echo Sum of series is $sum
$sh prg24
enter value of n
10
Sum of series is 385
44
100 SHELL PROGRAMS IN UNIX
25. The XYZ construction company plans to give a 5% year-end bonus to each of its employees earning Rs. 5,000 or more per year and a fixed bonus of Rs 250 to all other employees. Print the bonus of any employee.
$vi prg25
clear