Download System Administration

Transcript
Chapter 15. Shell Scripts
For reasons of consistency with C, the second form is usually preferred. The example above
tests whether the file filename exists and is not a directory. The following options are
possible:
r
Read access
w
Write access
x
Executable
e
Existence
o
Ownership
z
Zero length
f
Plain file, not directory
d
Directory
Numeric comparison operators are also available:
==
Equal to
!=
Not equal to
>
Greater than
>=
Greater than or equal to
<
Less than
<=
Less than or equal to
For example:
( $num == 3 )
Also, string comparison is possible (do not put the pattern in quotation marks):
==
Equal to
!=
Not equal to
=~
Matches (with wildcard characters)
!~
Does not match (with wildcard characters)
For example:
( $str == xyz )
True if $str is xyz
( $str =~ [abc]* )
True if $str starts with a, b, or c
( $str !~ *.c )
False if $str ends with .c
These logical functions can be combined with logical operators:
&&
Logical AND
||
Logical OR
For example:
(( $num == 5 ) && ( -f filename ))
The entire expression must be enclosed in parentheses, and again parentheses can be used
to group such functions. The logical AND has precedence over the logical OR (and is
therefore evaluated first), but for clarity it is recommended to always use parentheses.
Conditional Execution
Conditional execution is accomplished with the if statement:
if ( <logical_function> ) then
<commands>
else
208
System Administration
01-999166-00 A0800