Читаем 100 Shell Programs in Unix полностью

while test $ch = ‘y’

do

echo a. Factorial

echo b. Prime or not

echo c. Odd or even

echo d. Exit

echo Enter choice

read ch

case $ch in

a) echo Enter number

read num

i=1

j=1

while test $i -le $num

do

k=‘expr $i \* $j’

i=‘expr $i + 1’

j=$k

done

echo Factorial of $num is $j ;;

b) 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

break

fi

i=‘expr $i + 1’

done

if test $i -eq $num

then

echo number is prime ;;

fi ;;

c) echo enter number

read num

y=‘expr $num % 2’ if test $y -eq 0

then

echo number is even

else

echo number is odd

fi ;;

d) exit ;;

*) echo wrong choice ;;

esac

echo Do you want to continue press y/n

read $ch

done

Sample Run

$sh prg75

a. Factorial

b. Prime or not

c. Odd or even

d. Exit

Enter choice

a

Enter number

4

Factorial of 4 is 24

Do you want to continue press y/n

y

a. Factorial

b. Prime or not

c. Odd or even

d. Exit

Enter choice

b

Enter number 6

number is not prime

Do you want to continue press y/n

y

a. Factorial

b. Prime or not

c. Odd or even

d. Exit

Enter choice

b

Enter number 7

number is prime

Do you want to continue press y/n y

a. Factorial

b. Prime or not

c. Odd or even

d. Exit

Enter choice

c

enter number 5 number is odd

Do you want to continue press y

a. Factorial

b. Prime or not

c. Odd or even

d. Exit

Enter choice

c

enter number 12

number is even

Do you want to continue press y

a. Factorial

b. Prime or not

c. Odd or even

d. Exit

Enter choice

2

wrong choice

Do you want to continue press y

a. Factorial

b. Prime or not

c. Odd or even

d. Exit

Enter choice

d

y/n

y/n

y/n

76. Program for printing user id of user whose uid >50.

$vi prg76

clear

cat /etc/passwd | cut -f3 -d ‘:’>aa

uid=50

while [ $uid -le 65535 ]

# 65535 is last user id

do

grep $uid aa>>bb

uid=‘expr $uid +1’

done

sort bb #

order by first digit

Sample Run

$sh prg76

500

501

502

51

65534

68

69

74

77

81

99

77. Program for swapping of two numbers.

$vi prg77

clear

echo Enter the first number

read a

echo Enter the second number

read b

c=$a

a=$b

b=$c

echo After swapping

echo first number is $a

echo  second number is $b

Sample Run

$sh prg77

Enter the first number

5

Enter the second number

6

After swapping

first number is 6

second number is 5

78. Write a Program to check whether a number given by the user is zero, positive or negative.

$vi prg78

clear

echo Enter the Number

read x

if [ $x -gt 0 ]

then

echo x is Positive

elif  [  $x -eq 0 ]

then

echo x is a Zero

else

echo x is Negative

fi

Sample Run

$sh prg78

Enter the Number

2

x is Positive

$sh prg78

Enter the Number

0

x is a Zero

$sh prg78

Enter the Number

-3

x is Negative

79. Program for checking the login id & password.

$vi prg79

clear

echo Enter the login id

read login

echo Enter the password

read password

if [ $login = root ]

then

if [ $password = redhat ]

then

echo You entered the correct login name and password

fi

else

echo login failed

fi

Sample Run

$sh prg79

Enter the login id

root

Enter the password

unix

login failed

$sh prg79

Enter the login id

root

Enter the password

redhat

You entered the correct login name and password

$sh prg79

Enter the login id

unix

Enter the password

redhat

login failed

80. Program to find the sum of numbers entered through command-line.

$vi prg80

clear

sum=0

for i in $*

do

sum=‘expr $sum + $i’

done

echo The sum of the given numbers is $sum

Sample Run

$sh prg80 30 40

The sum of the given numbers is 70

81. The length & breadth of a rectangle and radius of a circle are input through the keyboard. Write a program to calculate the area & perimeter of the rectangle, and the area & circumference of the circle.

$vi prg81

clear

echo Enter length, breadth and radius

read length breadth radius

areaR=‘expr $length \* $breadth’

perimeterR=‘expr 2 \* \( $length + $breadth \)’

areaC=‘echo 3.14 \* $radius \* $radius |bc’

‘$areaR ‘$perimeterR ‘$areaC ‘$cirC

cirC=‘echo 2\* 3.14 \* $radius |bc’

echo ‘Area of rectangle         = ‘$areaR

echo ‘Perimeter of rectangle    = ‘$perimeterR

echo ‘Area of circle            = ‘$areaC

echo ‘Circumference of circle   = ‘$cirC

Sample Run

$sh prg81

Enter length, breadth and radius

20 5 5

Area of rectangle       = 100

Perimeter of rectangle  = 50

Area of circle          = 78.50

Circumference of circle = 31.40

82. If a five digit number is input through the keyboard, write a program to calculate the sum of its digits.

$vi prg82

clear

echo Enter any five digit number

read num

d1=‘expr $num % 10’

num=‘expr $num / 10’

Перейти на страницу:

Похожие книги

Компьютерные сети. 6-е изд.
Компьютерные сети. 6-е изд.

Перед вами шестое издание самой авторитетной книги по современным сетевым технологиям, написанное признанным экспертом Эндрю Таненбаумом в соавторстве со специалистом компании Google Дэвидом Уэзероллом и профессором Чикагского университета Ником Фимстером. Первая версия этого классического труда появилась на свет в далеком 1980 году, и с тех пор каждое издание книги неизменно становилось бестселлером. В книге последовательно изложены основные концепции, определяющие современное состояние компьютерных сетей и тенденции их развития. Авторы подробно объясняют устройство и принципы работы аппаратного и программного обеспечения, рассматривают все аспекты и уровни организации сетей — от физического до прикладного. Изложение теоретических принципов дополняется яркими, показательными примерами функционирования интернета и компьютерных сетей различного типа. Большое внимание уделяется сетевой безопасности. Шестое издание полностью переработано с учетом изменений, произошедших в сфере сетевых технологий за последние годы, и, в частности, освещает такие технологии, как DOCSIS, 4G и 5G, беспроводные сети стандарта 802.11ax, 100-гигабитные сети Ethernet, интернет вещей, современные транспортные протоколы CUBIC TCP, QUIC и BBR, программно-конфигурируемые сети и многое другое.

Дэвид Уэзеролл , Ник Фимстер , Эндрю Таненбаум

Учебные пособия, самоучители