Linux with Operating System Concepts



Download 5,65 Mb.
Pdf ko'rish
bet97/254
Sana22.07.2022
Hajmi5,65 Mb.
#840170
1   ...   93   94   95   96   97   98   99   100   ...   254
Bog'liq
Linux-with-Operating-System-Concepts-Fox-Richard-CRC-Press-2014

Hello $NAME, todays date is 
`
date
`. Additionally, by using the 
single quote marks, we would not be able to place the single quote after the s in today’s.


Shell Scripting

253
Let us consider another example of executing a command on the right hand side of 
an assignment statement. What will happen when executing an ls command? The com-
mand 
LIST 
=
$(ls)
would result in LIST storing the list of all files and subdirectories in 
the current directory. However, unlike the actual list produced by ls, each of these items, 
listed as a string, would be separated by spaces on a single line and not on separate lines. 
Similarly, 
LIST 
=
$(ls 

l)
would cause LIST to store the long listing of the items in the 
directory where each portion of the long listing is separated by a single space. This might 
look something like:
total 44 -rwxr


r


. 1 foxr foxr 448 Sep 6 10:20
addresses.txt -rwxr


r


. 1 foxr foxr 253 Aug 6 08:29
computers.txt -rw-rw-r


. 1 foxr foxr 235 Sep 11 14:23
courses.dat . . .
which is a total jumble of information. If the item enclosed inside 
` `
or $() is not a Linux 
command, an error will arise.
In addition to applying commands, we can also take advantage of Bash’s filename 
expansion using wildcards. The instruction 
LIST 
=
*.txt
would store in the variable 
LIST the list of all filenames with the .txt extension. The instruction 
LIST 
=
file?.txt
would store in the variable LIST the list of all filenames that start with file followed by one 
character and end with the .txt extension, for instance file1.txt and file2.txt but not file.txt 
or file21.txt.
We can use various operations on the right hand side of our assignment statement. In 
general, we think of two types of operations, string operations and arithmetic operations. 
The most commonly used string operation is string concatenation, that is, the conjoining 
of multiple strings to create one larger string. Concatenation is accomplished merely by 
listing each string on the right hand side of the assignment statement. If the strings are 
to be separated by blank spaces, then the entire sequence must be enclosed within quote 
marks. The items concatenated can be any combination of variables, literal characters, and 
Linux operations embedded in ` ` or $() notation. We saw several examples earlier such as
• 
NAME="$FIRST_NAME $LAST_NAME"
• 
NAME=$FIRST_NAME$LAST_NAME
• 
LIST=$(ls)
We examine some other string operations later in this section.
You need to make sure that any command placed inside ` ` or $() notation is a legal 
command which the user who is running the script has access to. For instance,
Greeting 
=
"Hello $NAME, today is $(dat)"
yields an error of dat: command not found.


254

Linux with Operating System Concepts
If you do choose to declare a variable, the statement is 
declare [options] name[
=
value]
. This notation indicates that options and the initial value are optional. Options 
are denoted using 
+
to turn an attribute of the variable off and 
− 
to turn it on. The attri-
butes are a (array), f (function), i (integer), r (read-only), and x (exported). For instance, 
declare –rx ARCH 
=
386
will create a read-only variable, ARCH, with the value 386 
and export it beyond the current shell. You cannot use 
+
r as by default, a variable is not 
read-only.
7.3.4 Arithmetic Operations in Assignment Statements
For arithmetic operations, we have to denote that the values stored in variables are to be 
interpreted as numbers and not strings. We annotate such operations using one of two 
notations. First, we can precede the assignment statement with the word let, as in 
let 

=

+
1
. Second, we embed the arithmetic operation inside the notation $(()), as in 

=
$((n 
+
1))
. Notice in both cases, n, a variable on the right hand side of the assignment 
statement, does not have a $ immediately precede it. You are allowed to include the $ before 
n in either case but it is not necessary. You can also use the prefix or postfix increment/
decrement operators 
++
and -- as found inC, Java, and similar languages. The syntax in 
this case is ((var
++
)), ((
++
var)), ((var --)), and (( --var)). The variable must currently be stor-
ing a numeric value. If it is storing a string, the variable’s string value is lost and replaced 
by 1 (for 
++
) or 

1 (for --).
The Bash interpreter can perform many arithmetic operations on integer values. It can-
not however perform arithmetic operations on fractional values (any value with a decimal 
point). The operators available are shown in Table 7.1.
Let us consider some examples.
• 
N
=
1

N
=
$((N+1))
• N is now 2
TABLE 7.1 
Bash Arithmetic Operators

Download 5,65 Mb.

Do'stlaringiz bilan baham:
1   ...   93   94   95   96   97   98   99   100   ...   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