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

enter marks of five subjects

34

42

31

32

23

Percentage is 32

Fail

39. Write a shell script to generate first ‘n’ terms of the following sequence without using multiplication-1 2 4 8 16 32…………n.

$vi prg39

clear

echo enter the value of n

read n

echo

i=1

while test $i –le $n

do

echo $i

i=‘expr $i + $i’

done

Sample Run

$sh p39

enter the value of n

20

1

2

4

8

16

40. Write a shell script to find greatest common divisor (GCD) for two given numbers.

$vi prg40

clear

echo enter numbers a and b

read a b

while [ 1 ] # infinite loop

do

c=‘expr $a % $b’

if [ $c -eq 0 ]

then

echo GCD = $b

exit

fi

a=$b

b=$c

done

Sample Run

$sh prg40

enter numbers a and b 47 3 GCD = 1

41. Write a shell script that takes as command-line input, a number n and a word. It then prints the word n times, one word per line.

$vi prg41

clear

i=1

while [ $i –le  $1 ]

do

echo $2

i=‘expr $i + 1’

done

Sample Run

$sh prg41 5 Hello

Hello

Hello

Hello

Hello

Hello

42. Write a shell script to remove all words that occur more than once in a list.

$vi prg42

clear

echo enter list

cat >file1

echo uniques are :

sort –u file1>file1.out

cat file1.out

Sample Run

$sh prg42

enter list

a

c

a

b

c

Uniques are :

a

b

c

43. Write a shell script to take backup of all c files.

$vi prg43

clear

ls abc >a1

if test – a1

then

mkdir abc

cp *.c /abc

echo backup is done

fi

44. Write a program in UNIX to accept range of months and display calendar within that range.

$vi prg44

clear

echo enter lower limit

read llimit

echo enter upper limit

read ulimit

echo enter year

read y

echo

while test $llimit -le $ulimit

do

cal $llimit $y

llimit=‘expr $llimit + 1’

done

Sample Run

$sh prg44

enter lower limit

2

enter upper limit

3

enter year

2008

February 2008

Su Mo Tu We Th Fr Sa

1  2

3  4  5  6  7  8  9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29

March 2008

Su Mo Tu We Th Fr Sa

1

2  3  4  5  6  7  8

9 10 11 12 13 14 15

16 17 18 19 20 21 22

23 24 25 26 27 28 29

30 31

45. Write a program in UNIX to accept a year and months in that year and display calendar of those months.

$vi prg45

clear

echo enter month value in numeric

read m

echo enter year

read y

echo

for i in $m

do

cal $i $y

done

Sample Run

$sh prg45

enter month value in numeric

1 3 12

enter year

2008

January 2008

Su Mo Tu We Th Fr Sa

1  2  3  4  5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

March 2008

Su Mo Tu We Th Fr

Sa

1

2  3  4  5  6  7  8

9 10 11 12 13 14

15

16 17 18 19 20 21

22

23 24 25 26 27 28

29

30 31

December 2008

Su Mo Tu We Th Fr Sa

1  2  3 4 5  6

7  8  9 10 11 12 13

14 15 16 17 18 19 20

21 22 23 24 25 26 27

28 29 30 31

46. To find out the sum of squares of integers from m to n where m, n are input by user.

$vi prg46

clear

echo enter value of m and n

read m

read n

echo

s=0

while [ $m -le $n ]

do

a=‘expr $m \* $m’

s=‘expr $s + $a’

m=‘expr $m + 1’

done

echo $s

Sample Run

$sh prg46

enter value of m and n

2

5

54

47. To find out the greatest and smallest element of an array.

$ vi prg47

clear

echo Enter size of array

read no

i=0

echo

echo Enter $no elements

while [ $i -lt $no ]

do

read n[$i]

i=‘expr $i + 1’

done

high=${n[0]} low=${n[0]} k=1

while [ $k -lt $no ]

do

if [ $high -lt ${n[$k]} ]

then

high=${n[$k]}

fi

if [ $low -gt ${n[$k]} ]

then

low=${n[$k]}

fi

k=‘expr $k + 1’

done

echo highest=$high

echo lowest=$low

Sample Run

$sh prg47

Enter size of array

5

Enter 5 elements

3

22

1

55

4

highest=55

lowest=1

48. Write a shell script to find out whether a file is writable or not. File name must be input by the user through command-line.

$vi prg48

clear

if test -w $1

then

echo file is writable

else

echo file is not writable

fi

Sample Run

$sh prg48 a1.txt

file is writable

49. Write a program for Bubble sorting.

$vi prg49

clear

echo enter any no

read no

i=0

k=0

while [ $i -lt $no ]

do

read n[$i]

i=‘expr $i + 1’

done

while [ $k -lt $no ]

do

j=0

while test $j -lt $no

do

if test ${n[$k]} -lt ${n[$j]}

then

m=${n[$k]}

n[$k]=${n[$j]}

n[$j]=$m

fi

j=‘expr $j + 1’

done

k=‘expr $k + 1’

done

a=0

echo Array after bubble

sort while test $a -lt $no

do

echo “${n[$a]}” a=‘expr

$a + 1’

done

Sample Run

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

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

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

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

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

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