$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
$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
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
$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
$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
$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