Linux with Operating System Concepts



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

com-
mand
which in this case should be one of 
start

stop

restart,
or 
status
. Without 
a command, the script responds with the error message that no command was supplied 
and with any command other than start, stop, restart, or status, the script responds with 
the default usage statement explaining how to use this script.
Note that the action corresponding to restart has two instructions, to call /
sbin/
someservice –k
and then /
sbin/someservice –s
. These could be placed on a 
single line, in which case they would need to be separated by semicolons. The * enumerated 
item is our default case, used to output to the user how to use the instruction. We explore 
this style of code in Chapter 11 when we explore one of the service scripts in detail.


Shell Scripting

275
The following examples demonstrate further use of the wildcards. In the first case, we 
are testing various possible Yes/No answers for the user. In the second case, we are com-
paring a file name against a list of possible extensions to determine how to handle the file 
(in this case, we are attempting to compile the program).
echo –n Do you agree to the licensing terms of the program?
read answer
case $answer in
[yY] | [yY][eE][sS]) ./program ;;
[nN] | [nN][oO] ) echo I cannot run the program ;;
*) echo Invalid response ;;
esac
if [ $# -eq 0 ]; then echo Error, no directory provided
else
for file in $1; do
case $file in
*.c) gcc $file ;;
*.java) javac $file ;;
*.sh)./$file ;;
*.*) echo $file is of an unknown type
*) echo $file has no extension to match
esac
done
fi
Finally, you can alter the case statement’s behavior by using ;& instead of ;; to end an 
action. With ;; the action, after executing, causes the case statement to terminate. Instead, 
if an action executes and ends with the notation ;&, then the case statement continues 
by comparing the remaining lists. Thus, such a case statement could conceivably execute 
multiple actions. In the following code skeleton, any combination of the first three could 
match. The first case represents that the variable has a lower-case letter stored in it. If so, 
it executes its action(s) but continues on to the second case because of the ;& ending the 
instruction. The second case represents that the variable has an upper-case letter stored 
in it. If so, the second set of action execute and again, the instruction continues on. The 
third case represents that the variable has a digit in it. In this situation, if it matches it 
executes the action(s) and then the case statement terminates. If this third case does not 
match, the default executes. Thus, by mixing the ;; and ;&, we can control how far into the 
case statement we continue if we have matches. With only ;; we end the case statement as 
soon as we find a match.
*[a-z]*) . . . ;&
*[A-Z]*) . . . ;&
*[0-9]*) . . . ;;
*) . . .


276

Linux with Operating System Concepts
7.5.6 Conditions outside of Selection Statements
There is one other form of selection statement to explore which is a short-hand notation 
for an if-then statement by using 
short circuiting
, which can be applied by using && or ||. 
With respect to &&, if the first half of the condition is false, there is no need to compute the 
second half because it is immaterial. Similarly for ||, if the first half of the condition is true 
then there is no need to compute the second half. For instance, consider

>
B && B 
>
C
If A is not greater than B, then there is no need to compare B to C because the entire 
expression must be false. Alternatively, in

>
E || F 
>
G
If D is greater than E, there is no need to compare F and G because the entire expression 
is true.
In Linux, we apply this concept to pair up a condition and an executable statement as 
follows.
• 


Download 5,65 Mb.

Do'stlaringiz bilan baham:
1   ...   101   102   103   104   105   106   107   108   ...   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