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

$sh prg49

enter any no

5

6

4

1

9

7

Array after bubble sort

1

4

6

7

9

50. Write a shell script to find out what type of character you have entered such as capital letter, small letter, digit, special symbol and whether you entered more than one character.

$vi prg50

clear

echo enter character

read char

case $char in

[A-Z]) echo you entered a capital letter ;;

[a-z]) echo you entered a small letter ;;

[0-9]) echo you entered a digit ;;

?) echo you entered a special symbol ;;

*) echo you entered more than one character ;; esac

Sample Run

$sh prg50

enter character

a

you entered a small letter

enter character

1

you entered a digit

enter character

#

you entered a special symbol

enter character

asd123

you entered more than one character

enter character

A

you entered a capital letter

51. Write a script that has this output:

Give me a U!

U

Give me a N!

N

Give me a I!

I

Give me a X!

X

$vi prg51

clear

for i in U N I X

do

echo Give me a $i!

echo $i done

Sample Run

$sh prg51

Give me a U!

U

Give me a N!

N

Give me a I!

I

Give me a X!

X

52. Rewrite the Q. 51 so that it uses command-line input to provide the spell out letters.

$sh prg52

clear for i

do

echo Give me a $i!

echo $i

done

Sample Run

sh prg52 B O O K

Give me a B!

B

Give me a O!

O

Give me a O!

O

Give me a K!

K

53. Write a shell script that presents a multiple-choice question, gets the user’s answer, and reports back whether the answer is right, wrong, or not one of the choices.

$vi prg53

clear

echo UNIX is

echo a\) a Turkish Assistant Manager\’s club

echo b\) a United Nations organization

echo c\) a computer operating system

echo d\) all of the above

read answer

case $answer in

a) echo Wrong — the answer is c ;;

b) echo Wrong — the answer is c ;;

d) echo Wrong — the answer is c ;;

c) echo Right ;;

*) echo Not one of the choices ;;

esac

Sample Run

$sh prg53

UNIX is

a) a Turkish Assistant Manager’s club

b) a United Nations organization

c) a computer operating system

d) all of the above a Wrong — the answer is c)

$sh prg53

UNIX is

a) a Turkish Assistant Manager’s club

b) a United Nations organization

c) a computer operating system

d) all of the above c

Right

54. Write a shell script which accepts the word oak as an answer regardless of whether upper-case or lower-case letters are used anywhere in the word.

$sh prg54

clear

echo What kind of tree bears acorns\?

read response

case $response in

Oo) echo $response is correct ;;

Aa) echo $response is correct ;;

Kk) echo $response is correct ;;

*) echo sorry, that is wrong

esac

Sample Run

$sh prg54

What kind of tree bears acorns?

Aa

Aa is correct

$sh prg54

What kind of tree bears acorns?

AA

sorry, that is wrong

55. Write a shell script that takes a login name (say X) as a command-line argument and reports to you when that person has logged in or not. If the user wants to send a greeting to that person (X) redirection can be used to his or her terminal. (Such a script would be run in background.)

In case admin is not login, it repeatedly says “admin is not logged in” press ctrl+c

$vi prg55

clear

until who | grep $1 > /dev/null

do

echo sleep now

echo admin is not logged in

echo press ctrl+c

sleep 300

done

set ‘who | grep $1’ echo $1 has logged in on $2 echo hi, $1 > /dev/$2

Sample Run

$sh prg55 tomcat

here tomcat is the user name who is to be searched for being log in. If tomcat is not log in then the set command returns error.

tomcat has logged in on tty1

[tomcat@localhost ~]$ hi, tomcat

This output is displayed on tomcat’s terminal

$sh prg55 admin

sleep now

admin is not logged in

press ctrl+c

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

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

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

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

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

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