Linux with Operating System Concepts



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

filename
word
where 
filename
is the file you are examining and 
word
is the string you are searching for.
7.6.6 The While Read Statement
Earlier, we visited the while loop. A variation of the while loop is known as the 
while 
read
loop. This loop combines the while statement and the read statement. With this ver-
sion of the while statement, there is no condition. Instead, the loop continues to execute 
while there are still data to read. The form of this instruction is
while read 
varlist
; do
loop body
done
The 
varlist
is a list of variable names. The loop expects to input a value for each variable 
in this list for each iteration. When there are no more data received, the loop terminates. 
No more data is either indicated by the user entering 
control
+
d
if the input is coming 
from keyboard, or EOF (end of file) if input is from a file.
The following script, called 
sum
, inputs pairs of numbers multiplying the two of each 
pair together, summing the resulting products into a SUM variable. For instance, if the 
input data is the list: 10 20 30 40 50 60, the script would compute 10 * 20 
+
30 * 40 
+
50 * 
60 
=
4400.
#!/bin/bash
SUM
=
0
while read X Y; do
SUM
=
$((SUM
+
X*Y))


Shell Scripting

285
done
echo $SUM
Now to run this script, you could either invoke it normally, as in .
/sum
, or by redirect-
ing input from an input file as in .
/sum 
<
data.txt
.
There are some challenges in ensuring that this works correctly. You will need a data 
file that contains the pairs of numbers. The format of the data in the file must match the 
read statement. In this case, we are reading X and Y in a single statement, so X and Y must 
appear on the same line in the file. If each pair was on a separate line, the script would 
obtain the datum on the line to store in X and assign 0 for Y since there is no second datum 
on the line. The result would be a sum of 0.
Alternatively, if the user did not redirect input from a file, then input would come from 
the keyboard. However, there is no prompting message so the user may not know what is 
expected of them. Further, the user is not supposed to just enter a single datum but pairs of 
data. In order to end input, the user would have to type 
control 
+
d
.
An alternative to requiring that the user redirect input to the script is to handle the 
redirection within the script itself. This is accomplished with the 
done
statement that ends 
the 
while read
loop. You would specify 
done 
<
inputfile
. Our previous script 
becomes the following.
#!/bin/bash
SUM
=
0
while read X Y; do
SUM
=
$((SUM
+
X*Y))
done 
<
data.txt
echo $SUM
The disadvantage of this approach is that we are hardcoding the input file’s name 
in the script. This removes control from the user who might want to run the script on 
several different input files. One solution to this is to replace 
done 
<
data.txt
in 
the script with 
done 
<
$1
and expect that the user will enter the data file name as the 
first (or only) parameter to the script. For instance, we could invoke the script as .
/sum 
data.txt
.
7.7 ARRAYS
Arrays are data structures that store multiple values. They are a form of container struc-
ture. In most languages, an array must be a 

Download 5,65 Mb.

Do'stlaringiz bilan baham:
1   ...   105   106   107   108   109   110   111   112   ...   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