Выбрать главу

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

Sample Run

$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

Sample Run

$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”

Sample Run

$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

Sample Run

$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

Sample Run

$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

Sample Run

$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

Sample Run

$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

Sample Run

$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

Sample Run

$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

Sample Run

$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

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

Sample Run

$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

Sample Run

$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”