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 (
Do'stlaringiz bilan baham: