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

34. Write a shell script to print Fibonacci series.

$vi prg34

clear

echo enter the last element of series

read n

echo

a=0

b=1

echo $a

echo $b

i=1

while test $i –lt $n

do

c=‘expr $a + $b’

if test $c –gt $n

then

exit

fi

echo $c

a=$b

b=$c

i=‘expr $i + 1’

done

Sample Run

$sh prg34

enter value of series

5

0

1

1

2

3

5

35. Write a shell script to translate the contents of a file into UPPER CASE, where file name is entered through command line.

$vi prg35

clear

if test $# -eq 0

then

echo “No argument”

exit

fi

while test $# -gt 0

do

if test –s $1

then

if test –f $1

then

cat $1 | tr a-z A-Z >$1.up

cat $1.up

fi

else

echo $1 is not a file

fi

shift

done

echo Translation successful

Sample Run

$sh prg35 file.txt

WELCOME

HELLO

Translation successful

In file.txt, welcome and hello are written in small letters. After running this program, welcome and hello are converted in capital letters and saved in 1.up file

36. Write a shell script to perform following tasks-

(

a

)

Display the present working directory.

(

b

)

Clear the screen.

(

c

)

Display the current date.

(

d

)

Make a directory with its sub-directory d1.

(

e

)

Change the directory to the directory having sub directory d1.

(

f

)

Create two files (say file1 & file2) within this.

(

g

)

Provide appropriate security options to these files.

(

h

)

List the contents of directory.

$vi prg36

(a) Pwd

(b) clear

(c) date

(d) mkdir d

cd d

mkdir d1

(e) cd d1

(f) touch file1 file2

(g) chmod 644 file1 file2

(h) Is

37. The marks obtained by a student in five different subjects are input through the keyboard. The student gets a division as per the following rules. (Using else’s clause).

if percentage greater than or equal to 60 get First division

if percentage greater than or equal to 50 or less than 60 get Second division

if percentage greater than or equal to 40 or less than 50 get Third division

if percentage less than 40 Fail

$vi prg37

clear

echo enter marks of five subjects (out of 100 each)

read m1

read m2

read m3

read m4

read m5

per=‘echo \( $m1 + $m2 + $m3 + $m4 + $m5 \) /5 | bc’

echo

echo Percentage is $per

if [ $per –ge 60 ]

then

echo First division

else

if [ $per –ge 50 –a -$per –lt 60 ]

then

echo Second division

else

if [ $per –ge 40 –a $per –lt 50 ]

then

echo Third division

else

echo Fail

fi

fi

fi

Sample Run

$sh prg37

enter marks of five subjects

44

67

80

90

67

Percentage is 69

First division

$sh prg37

enter marks of five subjects

56

54

53

51

60

Percentage is 54

Second division

$sh prg37

enter marks of five subjects

46

54

41

42

46

Percentage is 45

Third division

$sh prg37

enter marks of five subjects

34

42

31

32

23

Percentage is 32

Fail

38. The marks obtained by a student in two different subjects are input through the keyboard. The student gets a division as per the following rules. (Using elif clause).

if percentage greater than or equal to 60 get First division

if percentage greater than or equal to 50 or less than 60 get Second division

if percentage greater than or equal to 40 or less than 50 get Third division

if percentage less than 40 Fail

$vi prg38

clear

echo enter marks of five subjects

read m1

read m2

read m3

read m4

read m5

per=‘echo \( $m1 + $m2 + $m3 + $m4 + $m5 \) /5 | bc’

echo

echo Percentage is $per

if [ $per –ge 60 ]

then

echo First division

elif [ $per –ge 50 –a -$per –lt 60 ]

then

echo Second division

elif [ $per –ge 40 –a $per –lt 50 ]

then

echo Third division

else

echo Fail

fi

Sample Run

$sh prg38

enter marks of five subjects

44

67

80

90

67

Percentage is 69

First division

$sh prg38

enter marks of five subjects

56

54

53

51

60

Percentage is 54

Second division

$sh prg38

enter marks of five subjects

46

54

41

42

46

Percentage is 45

Third division

$sh prg38

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

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

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

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

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

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