Linux with Operating System Concepts



Download 5,65 Mb.
Pdf ko'rish
bet116/254
Sana22.07.2022
Hajmi5,65 Mb.
#840170
1   ...   112   113   114   115   116   117   118   119   ...   254
Bog'liq
Linux-with-Operating-System-Concepts-Fox-Richard-CRC-Press-2014

condition

action(s)
Notice the omission of the word 
then
. You are unable to use this form of statement if 
you have an else clause or if the actions are placed on separate lines. An optional 
endif
statement may end the line if desired.
There is a second one-way statement which is the if-then statement similar to what we 
saw in bash earlier. The syntax here is
if (
condition
) then
then-clause action(s)
endif
In this case, the then-clause must appear on a separate line from the word then. If there 
are multiple instructions in the then-clause, they can be on one or multiple lines but mul-
tiple instructions on one line must be separated by semicolons. The word 
endif
must 
occur on a line by itself and is required.
The two-way statement is an if-then-else statement. This is the same syntax as the if-
then statement with the exception that after the then-clause and before the 
endif
, an 
else-clause appears. The else clause is the word 
else
followed by one or more statements. 
Oddly enough, the word else and its statements do not have to occur on separate lines and 
can in fact occur on the same line as part of or all of the then-clause. The entire construct 
ends with an endif statement.
There are two forms of n-way selection statements, the nested if-then-else and the switch. 
The nested if-then-else is very much like bash’ if-then-elif-fi but in this case, the 
elif
is 


302

Linux with Operating System Concepts
replaced by the two words 
else if
. Below is an example that compares the bash example 
(left) to the csh example (right).
if [ $x –gt $ y ]; then 
if ($x 
>
$y) then
z
=
$((x
+
y)) 
@ z 
=
$x 
+
$y
elif [ $x –gt $z ]; then 
else if ($x 
>
$z) then
y
=
$((x
+
z)) 
@ y 
=
$x 
+
$z
else x
=
$((y
+
z)) 
else @ x 
=
$y 
+
$z
fi endif
The switch statement is similar in syntax to the switch statement from C. The format is
switch (test)
case pattern1:
action(s)
breaksw
case pattern2:
action(s)
breaksw
case pattern3:
action(s)
breaksw
. . .
default:
action(s)
endsw
The test is usually a variable but could also be an instruction which returns a value. The 
case portions can have multiple patterns which would appear on separate lines as
case pattern1a:
case pattern1b:
case pattern1c:
action(s)
Each pattern is a string where wildcards of 
*

?

|,
and 
[]
are permitted as in 
[Yy]
or 
y*
, which might be used to express different forms of the answer “yes.” The default is 
optional but allows you to specify a default case much like using 
*)
in bash’ case statement. 
The 
breaksw
statements are also optional but the way the switch statement is written, 
without them, the switch statement continues to compare the test value to the patterns even 
if a previous pattern matched. Omitting 
breaksw
is like using 
;&
in bash’ case statement.
There are two types of loops available for csh scripting
foreach
and 
while
. The while 
loop is the same as bash’ except for the syntax. The foreach loop is the same as bash’ itera-
tor for loop. There is no equivalent of a counting loop in csh but can be simulated through 
either the while or the foreach. The syntax for each type of loop is given below.


Shell Scripting

303
while (

Download 5,65 Mb.

Do'stlaringiz bilan baham:
1   ...   112   113   114   115   116   117   118   119   ...   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