Linux with Operating System Concepts



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

n
where 
n
is 
an integer. You define the exit status to indicate the error. If you examine the man page of 
various instructions, exit codes are sometimes provided. For instance, useradd has numer-
ous error codes, 0 being success, 1 being an error trying to update the /etc/passwd file, 2 
being invalid command syntax, 3 being an invalid option, and so forth. You can obtain the 
status of a function’s exit with the notation 
$?
.
In addition, functions can also return an integer value. You would use this option of a 
function for one of two reasons. First, you may wish to write a function to compute and 
return a value. For instance, you might write a function, 
maximum
, to return the largest 
value in a list. Second, the function could return a value in response to some situation 
within the function. In this way, the function does not 
have
to return a value but 
may
if the 
situation merits it. Recalling the runIt function, we might want to return the number of 
files passed to the function that did not match the condition, returning nothing if all files 
matched the condition.
To specify a return value, use the instruction 
return 
n
where 
n
is an integer. This is 
similar to 
exit 
n
. In the case of exit, n should be a 0 or a positive number. This is not 
necessarily the case with return. If you use return, then the return value is also stored in 
$? Thus, we can utilize this value after the function terminates. Consider the following 
example script.
#!/bin/bash
maximum() {
if [ $# -eq 0 ]; then exit 9999
else
max
=
$1
shift
for num in $@; do
if [ $num –gt $max ]; then max
=
$num; fi
done
fi
return $max
}
maximum 5 1 2 6 4 0 3
echo $?
This script defines the function maximum and then the first executable instruction is 
the call to maximum. We pass the function the list 5, 1, 2, 6, 4, 0, and 3. In maximum, if no 
parameters are received, it immediately exits with an error code of 9999. Otherwise, it sets 
max to $1 which is 5. The shift instruction shifts all parameters down one position. Now it 
iterates through the rest of this list (1, 2, 6, 4, 0, 3) and for each number, if it is larger than 


Shell Scripting

299
max, it replaces max. Once the for loop terminates, the if statement terminates, and the 
function returns the value in max, which in this case is 6. The last instruction in the script 
outputs $? which is the value provided either by the exit instruction or the return instruc-
tion. In this case, it will be 6, the value from the return.
7.10 C-SHELL SCRIPTING
The Bash scripting language is somewhat based on the syntax of an old language known 
as Algol. Newer features have been added such as the (()) notation to avoid the challenging 
syntax of conditions using [] and to permit easy arithmetic computations like ((i
+
+)) and 
the use of the relational operators like ((i 
>
j)). The C-shell (csh) language, as its name might 
imply, bases its scripting language’s syntax on the C programming language. Here, we 
compare the csh syntax to that of bash. Note that TC-shell (tcsh) will use the same syntax.
We will not cover all of the programming language constructs like we did in the previ-
ous sections of this chapter but instead show comparisons. If you have learned Bash script-
ing, converting to csh scripting should be easy. Alternatively, if you had already known csh 
scripting, learning Bash is also easy. Both scripting languages have many similarities and 
it’s primarily their syntax that differs. There are a few notable exceptions though where a 
related construct is not available in the other scripting language, which will be stated here 
as we provide the comparison.
7.10.1 Variables, Parameters, and Commands
As with bash, csh is case sensitive. Variable names when assigned or used, must match 
the name of the variable as previously assigned or declared, both in spelling and case. 
Similarly, all reserved words are only recognized in lower case.
Variables, as with bash, can only store strings although can also be interpreted as stor-
ing integers. Arrays are also available as with bash. To assign variables, the notation is 
set 

Download 5,65 Mb.

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