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

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

$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