Linux with Operating System Concepts



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

Comparison Operator
Meaning

e
File exists

f
File is regular (not a directory or device type file)

d
File is a directory

b
File is a block device

c
File is a character device

p
File is a named pipe

h, 

L
File is a symbolic link

S
File is a domain socket

r, 

w, 

x
File is readable, writable, executable

u, 

g
User ID/Group ID set (the ‘s’ under executable permissions)

O, 

G
You or your group owns the file

N
File has been modified since last read


268

Linux with Operating System Concepts
7.5.3 The If-Then and If-Then-Else Statements
Now that we have viewed the types of comparisons available, we can apply them in selec-
tion statements. The first type of selection statement is the if-then statement, or a one-way 
selection. If the condition is true, the 
then clause
is executed, otherwise the 
then clause
is 
skipped. The syntax for the if-then statement is as follows.
if [ 
condition 
]; then 
action(s)
; fi
The syntax is somewhat bizarre. First, there must be a semicolon after the ] after the 
condition. Second, if there are multiple actions, they must be separated by semicolons. 
Finally, the statement must end with the word 
fi
. However, the use of the semicolons is 
only needed if we place the entire instruction on a single line. If we separate each part onto 
a separate line, the semicolons are not needed. Here are several examples.
• 
if [ –e file1.sh ]; then ./file1.sh; fi
• if the file exists, execute it
• 
if [ $X –gt 0 ]; then COUNT=$((COUNT+1)); fi
• if the value in X is greater than 0 add 1 to COUNT
• 
if [ $NAME == $USER ]; then echo Hello master, I am ready 
for you; fi
• specialized greeting if the value in NAME is equal to the user’s login
• 
if [[ $X –ne 100 && $USER != Frank ]]; then X=0; Y=100; 
NAME=Joe; fi
Let us rewrite the last two of these examples as they might appear in a script file.
if [ $NAME 
==
$USER ]
then
COUNT
=
$((COUNT
+
1))
fi
if [[ $X –ne 100 && $USER !
=
Frank ]]
then
X
=
0
Y
=
100
NAME
=
Joe
fi
Notice that the indentation above is strictly for our own readability and not required 
by the Bash interpreter. The following three sets of code are equivalent as far as the Bash 
interpreter is concerned.


Shell Scripting

269
• 
if [ $X –gt $Y ]; then echo greater; X=0; fi
• 
if [ $X –gt $Y ]
then
echo greater
X=0
fi
• 
if [ $X –gt $Y ]
then
echo greater
X=0
fi
Recall from the last subsection, we noted that using the relational operators (e.g., 
<

>
=

in place of the two-letter abbreviations can yield an improper output. Consider the follow-
ing script.
#!/bin/bash
X
=
0
Y
=
1
if [ $X 
>
$Y ]; then echo greater; fi
if [ $X 
==
0 ]; then echo zero; fi
This script, when run, outputs
greater
zero
There is no logical reason why greater should be output as it is clearly incorrect. For $X, 
we are testing it to 0 using the string comparison 
==
(rather than the numeric compari-
son –eq). However, X does store 0, which can be interpreted as a string or a number, so the 
output of zero does make sense.
In each of the examples covered so far, if the condition is true, the action(s) is performed 
and if not, the statement ends and the interpreter moves on to the next instruction. In 
many situations, we have an alternative action that we would like executed if the condi-
tion is false. For this, we need a two-way selection statement, or an if-then-else
*
statement. 
The difference between the if-then and if-then-else statements is the inclusion of the word 
“else” and an 

Download 5,65 Mb.

Do'stlaringiz bilan baham:
1   ...   98   99   100   101   102   103   104   105   ...   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