Linux with Operating System Concepts



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

some condition 
] && 
some action
• 

some condition 
] || 
some action
In the first case, if the condition is false, the action is not performed. In the second case, 
if the condition is true, the action is not performed. How might we put this to use?
The most common situation is to combine a condition that tests a file to see if it is acces-
sible with an instruction that accesses the file. Consider, for instance, that we expect to find 
the script somescript.sh, which should be a regular file. If the file is found and is regular, 
we want to execute it. We could use an if-then statement of the form
if [ -f somescript.sh ]; then ./somescript.sh; fi
Or we could use the short circuiting approach
[ -f somescript.sh ] && ./somescript.sh
Another example might test a variable for a value. If the variable does not have a value, 
we should not use it.
if [ -r $var ]; then echo Variable var has no value; fi
Can be rewritten as
[ -r $var ] || echo Variable var has no value


Shell Scripting

277
7.6 LOOPS
7.6.1 Conditional Loops
The Bash scripting language has three types of loops: 
conditional
loops, 
counter-con-
trolled
loops, and 
iterator
loops. A conditional loop is a loop controlled by a condition, as 
described in Section 7.4. There are two versions of the conditional loop: a while loop and 
an until loop. They are both 
pre-test
loops meaning that the condition is tested before the 
loop body is executed. The difference between the two is in the semantics of the condition. 
The while loop iterates 
while
the condition is true. The until loop iterates 
until
the condi-
tion becomes true.
The syntax of the two loops is shown below.
while [ condition ]; do
action(s);
done
until [ condition ]; do
action(s);
done
As with other Bash syntax, if you separate the word do from the condition, then the 
semicolon can be omitted. Additionally, if there are multiple actions, the semicolons sepa-
rating them can be omitted if the actions are on separate lines. The conditions must con-
form to the conditions we discussed in Section 7.4.
Let us examine an example illustrating both loops. The following two loops will input 
numbers from the user and sum those values together. The condition to exit the loop occurs 
when a negative number is entered. The while loop’s condition is 
[ $VALUE –ge 0 ]
while the until loop’s condition is the opposite, 
[ $VALUE –lt 0 ]
.
SUM
=
0
read –p "Enter the first number, negative to exit" VALUE
while [ $VALUE –ge 0 ]; do
SUM
=
$((SUM
+
VALUE))
read –p "Enter next number, negative to exit" VALUE
done
SUM
=
0
read –p "Enter the first number, negative to exit" VALUE
until [ $VALUE –lt 0 ]; do
SUM
=
$((SUM
+
VALUE))
read –p "Enter next number, negative to exit" VALUE
done
Conditional loops can be controlled by a number of different types of logic. For instance, 
in the above, the user controls when to exit the loop by entering a negative number. This 


278

Linux with Operating System Concepts
is sometimes referred to as a 
sentinel
loop in that a sentinel value is used as a terminating 
condition. Here, any negative number is the sentinel value. We can also control a condi-
tional loop by asking the user to respond to a prompt of whether to continue or to exit. 
Below is a similar set of code to the two previous examples but requires the extra step of 
asking the user if there are more numbers to input.
SUM
=
0
read –p "Do you have numbers to sum?" ANSWER
while [ $ANSWER 
==
Yes ]; do
read –p "Enter the next number" VALUE
SUM
=
$((SUM
+
VALUE))
read –p "Do you have additional numbers?" ANSWER
done
Another way to control a conditional loop is to base the condition on the result of a compu-
tation. Below is a loop that will compute and output all of the powers of 2 less than 1000. When 
the loop exits, VALUE will be greater than or equal to 1000 (in fact, it will be 1024), which is 
not printed out because it is greater than the largest value that the code is to print out.
VALUE
=
1
while [ $VALUE –lt 1000 ]; do
echo $VALUE
VALUE
=
$((VALUE*2))
done
7.6.2 Counter-Controlled Loops
Another type of loop in Bash is the counter-controlled loop. This loop is a variation of the 
C/Java for loop. In this loop, you specify three pieces of information: a variable 

Download 5,65 Mb.

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