Linux with Operating System Concepts



Download 5,65 Mb.
Pdf ko'rish
bet104/254
Sana22.07.2022
Hajmi5,65 Mb.
#840170
1   ...   100   101   102   103   104   105   106   107   ...   254
Bog'liq
Linux-with-Operating-System-Concepts-Fox-Richard-CRC-Press-2014

file1 file2 file3 file4 file5 3
where 
file1, file2, and so on are files found in the current directory. Now we are providing the 
script with seven parameters instead of three and so obviously we get the “Illegal input” 
message rather than the result that we were looking for! To avoid this problem, we will use 
‘m’ instead of ‘*.’ This would change the third elif statement into
elif [ $2 
==
"m" ]; then echo $(($1*$3))
7.5.5 Case Statement
The 
case
statement is another form of n-way selection statement which is similar in many 
ways to the if-then-elif-else statement. The instruction compares a value stored in a vari-
able (or the result of an expression) against a series of enumerated lists. Upon finding a 
match, the corresponding action(s) is(are) executed. The case statement (known as a switch 
in C/C
++
/Java) is more restricted in other languages than it is in Bash. For instance, in C/
C
++
/Java, the expression can only be a variable storing an ordinal value.
*
Also, most lan-
guages require that an enumerated list be just that, an enumerated list. Thus, if we wanted 
*
An ordinal value is one that has a specific predecessor and a specific successor found with integers and characters. For 
instance, 5’s predecessor is 4 and its successor is 6 and ‘c’ has a predecessor of ‘b’ and successor of ‘d.’ On the other hand, 
a number like 2.13513 or a string like “Hello” has no specific predecessor or successor.


Shell Scripting

273
to implement the previous grading script using a case statement, in most languages we 
would have to enumerate all of the values between 90 and 100, between 80 and 89, between 
70 and 79, and so forth. The Bash version though gives us a bit of a break in that we can 
employ wildcard characters to reduce the size of an enumerated list.
The Bash form of the case instruction is
case expression in
list1) action(s);;
list2) action(s);;
. . .
listn) action(s);;
esac
Notice the syntax. First, each action ends with two consecutive semicolons. Second, the 
enumerated list ends with the close parenthesis sign. Finally, the entire statement ends with 
an 
esac
statement.
The expression can be a value from a variable, as in 
$X
, the result of an arithmetic opera-
tion such as 
$((X*5))
, or the result from a Linux command. The enumerated lists can be 
one or more literal strings and can combine Bash wild cards. For instance, 
*
by itself would 
match everything and can be used as a default clause in place of the final enumerated list. 
Any of the actions can contain multiple instructions, in which case the instructions are 
separated by semicolons.
Revising our previous calculator program using the case statement yields the following 
script.
#!/bin/bash
if [ $# -ne 3 ]
then echo Illegal input, did not receive 3 parameters
else case $2 in
+
) echo $(($1
+
$3));;
-) echo $(($1-$3));;
m) echo $(($1*$3));;
/) echo $(($1/$3));;
%) echo $(($1%$3));;
*) echo Illegal arithmetic operator;;
esac
fi
We see that the last enumerated list item is 
*)
which is the default clause so that if none 
of the other lists match the value of 
$2
, then this action is performed. Therefore, 
*
is not 
used to indicate multiplication but instead “everything else.”
Aside from the * wildcard, you can also use […] and [!…] to specify a pattern in a 
list that contains any of the enclosed characters or does not contain any of the enclosed 
characters respectively. For instance, [Pp][Cc] would match PC, Pc, pc, pC, and [A-Z]
[!A- Z] would match two characters where the first is an upper-case letter and the second is 


274

Linux with Operating System Concepts
anything other than an upper-case letter. We can also use | to separate full strings such as 
spring|summer|fall|winter.
Let us consider another example. We ask the user for their favorite color and based on 
the response, output a message.
echo What is your favorite color?
read color
case $color in
red) echo Like red wine ;;
blue) echo Do you know there are no blue foods? ;;
green) echo The color of life ;;
purple) echo Mine too! ;;
*) echo That’s nice, my favorite color is purple ;;
esac
We tend to see case statements in use in shell scripts used to control system services, in 
situations like the following. Here, we have a script that will perform an operation as sub-
mitted by the user to start or stop a script.
if [ $# -eq 0 ]; then echo Error, no command supplied
else
case $1 in
start)/sbin/someservice –s ;;
stop)/sbin/someservice –k ;;
restart)/sbin/someservice –k
/sbin/someservice –s ;;
status)/sbin/someservice –d ;;
*) echo Usage:/etc/init.d/someservice [start |
stop | restart | status] ;;
esac
fi
First, we make the assumption that the user has issued the command /
etc/init.d/
someservice
command
where someservice is a service name. Second, we make the 
assumption that the service program is itself stored in /sbin and will receive the option –s 
to start, -k to stop, and –d to display its status. We also assume that the user entered a 

Download 5,65 Mb.

Do'stlaringiz bilan baham:
1   ...   100   101   102   103   104   105   106   107   ...   254




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish