Linux with Operating System Concepts



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

VARIABLE
=
VALUE
. As with bash, the right hand side can be a literal value, a variable 
preceded by a $, or the result of an arithmetic operation. The csh language also utilizes the 
various shortcut operators (e.g., 
++
and 
+
=
).
There are four significant differences here between csh and bash. First, we precede the 
assignment statement with the command 
set
. Second, if the VALUE contains an arithme-
tic operation where we want to treat the values as numbers, we use the symbol @ preceding 
the assignment statement in place of the word set. The third difference is that we can insert 
spaces around the equal sign as well as around the arithmetic operators if the instruction 
uses @. Fourth, spaces can exist before and/or after the equal sign.
Here are some examples.
• 
set X = 5
X is treated as a string
• 
set X = 5
• X is treated as a string


300

Linux with Operating System Concepts
• 
set Y = Hello
• 
set Z = "$X $Y"
• Z takes on the value “5 Hello”
• 
set Z = $X $Y
• although this does not cause an error, the lack of quote marks leaves Z storing 
only 5 rather than “5 Hello”
• 
@ Z = $X + 1
• Z is now 6
• 
@ Z++
• Z is now 7
• 
@ Z+=5
• Z is now 12
• 
set Z = $X + 1
• this yields an error, set: Variable name must begin with a letter. because the 
csh interpreter is confused by attempting string concatenation without quote 
marks
• 
set Z = "$X + 1"
• Z stores “5 
+
1” literally
There are three ways to unset a variable. As with bash, you can use the 
unset
command. 
You can also accomplish this by either specifying 
set 
VARIABLE
or 
set 
VARIABLE
=
‘’. 
A variation of the set command is 
setenv
. This is used to assign a variable a value and 
export it. Thus, only use setenv when defining environment variables.
Parameters are referenced the same as with bash, using $1, $2, and so on. $# is the 
number of parameters passed, $0 is the name of the script, and $$ is the PID of the script 
process. You can also obtain the values of the arguments through the notation $argv[1], 
$argv[2], and so forth with $argv[*] being the entire list of parameters. There is no $argv 
equivalent for $0 or $#. The variable $
<
stores the most recently entered input. If the user 
has entered multiple data on a single line, the entire list is stored in $
<
.
7.10.2 Input and Output
The echo statement is the same as we saw with bash, with the same options and with quote 
marks serving the same purposes. Input however differs as there is no explicit input state-
ment. Instead, you must use $
<
. Reference to $
<
causes the interpreter to pause execution 
until the user presses enter. The following illustrates an example of this form of input.


Shell Scripting

301
#!/bin/csh
echo Enter your name
set name 
=
$
<
echo Nice to meet you $name
You do not need the variable name in the above example. Omitting the set statement, we 
could instead replace 
$name
with 
$
<
in the second echo statement.
7.10.3 Control Statements
Conditions appear in single parens. Comparisons between values are made using only the 
relational operators (
<
,
<=

>
,
>=

==
, !
=
)
. String comparisons, as with bash, are lim-
ited to equal to and not equal to (
=
=


=
)
. File tests are available using the same condition 
codes (e.g., -e for file exists, -f for file exists and is regular, -r for readable). To test a variable 
to see if it is initialized, use 
$?VAR
as in 
$?x
to see if x has a value. Compound conditions 
do not require any special syntax, unlike with bash where 
[[]]
are used. Boolean opera-
tors of AND, OR, and NOT are the same as in bash (
&&

||

!
).
There are four forms of if statements. The first is much like the C if statement. It is a one-
way statement in which the then clause must be self-contained all on one line. The syntax is
if (

Download 5,65 Mb.

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