8.5. Expressions
Expressions
are used in @, if, and while
statements to perform arithmetic, string comparisons, file testing,
and so on. exit and set also specify expressions, as can the
tcsh built-in command filetest. Expressions are formed by combining
variables and constants with operators that resemble those in the C
programming language. Operator precedence is the same as
in C and can be remembered as follows:
-
* / %
-
+ -
Group all other expressions inside parentheses. Parentheses are
required if the expression contains <, >, &, or |.
8.5.1. Operators
Operators
can be one of the following types.
8.5.1.1. Assignment operators
Operator
|
Description
|
=
|
Assign value.
|
+= -=
|
Reassign after addition/subtraction.
|
*= /= %=
|
Reassign after multiplication/division/remainder.
|
&= ^= |=
|
Reassign after bitwise AND/XOR/OR.
|
++
|
Increment.
|
--
|
Decrement.
|
8.5.1.2. Arithmetic operators
Operator
|
Description
|
* / %
|
Multiplication; integer division; modulus (remainder).
|
+ -
|
Addition; subtraction.
|
8.5.1.3. Bitwise and logical operators
Operator
|
Description
|
~
|
Binary inversion (one's complement).
|
!
|
Logical negation.
|
<< >>
|
Bitwise left shift; bitwise right shift.
|
&
|
Bitwise AND.
|
^
|
Bitwise exclusive OR.
|
|
|
Bitwise OR.
|
&&
|
Logical AND.
|
||
|
Logical OR.
|
{ command
}
|
Return 1 if command is successful, 0 otherwise. Note that this is the
opposite of command's normal
return code. The $status variable
may be more practical.
|
8.5.1.4. Comparison operators
Operator
|
Description
|
= = !=
|
Equality; inequality.
|
<= >=
|
Less than or equal to; greater than or equal to.
|
< >
|
Less than; greater than.
|
8.5.1.5. File inquiry operators
Command substitution and filename
expansion are performed on file before the test
is performed. Operators can be combined (e.g., -ef). The following is a list of the valid
file inquiry operators:
Operator
|
Description
|
-b file
|
The file is a block special file.
|
-c file
|
The file is a character special file.
|
-d file
|
The file is a directory.
|
-e file
|
The file exists.
|
-f file
|
The file is a plain file.
|
-g file
|
The file's set-group-ID bit is set.
|
-k file
|
The file's sticky bit is set.
|
-l file
|
The file is a symbolic link.
|
-L file
|
Apply any remaining operators to symbolic link, not the file it
points to.
|
-o file
|
The user owns the file.
|
-p file
|
The file is a named pipe (FIFO).
|
-r file
|
The user has read permission.
|
-s file
|
The file has nonzero size.
|
-S file
|
The file is a socket special file.
|
-t file
|
file is a digit and is an open file descriptor
for a terminal device.
|
-u file
|
The file's set-user-ID bit is set.
|
-w file
|
The user has write permission.
|
-x file
|
The user has execute permission.
|
-X file
|
The file is executable and is in the path, or is a shell built-in.
|
-z file
|
The file has 0 size.
|
!
|
Reverse the sense of any preceding inquiry.
|
Finally, tcsh provides the following
operators, which return other kinds of information:
Operator
|
Description
|
-A[:] file
|
Last time file was accessed, as the number of seconds since the
epoch. With a colon (:), the result is in timestamp format.
|
-C[:] file
|
Last time inode was modified. With a colon (:), the result is in
timestamp format.
|
-D file
|
Device number.
|
-F file
|
Composite file identifier, in the form
device:inode.
|
-G[:] file
|
Numeric group ID for the file. With a colon (:), the result is the
group name if known, otherwise the numeric group ID.
|
-I file
|
Inode number.
|
-L file
|
The name of the file pointed to by symbolic link
file.
|
-M[:] file
|
Last time file was modified. With a colon (:), the result is in
timestamp format.
|
-N file
|
Number of hard links.
|
-P[:] file
|
Permissions in octal, without leading 0. With a colon (:), the result
includes a leading 0.
|
-Pmode[:] file
|
Equivalent to -P
file ANDed to mode. With a
colon (:), the result includes a leading 0.
|
-U[:] file
|
Numeric user ID of the file's owner. With a colon
(:), the result is the username if known, otherwise the numeric user
ID.
|
-Z file
|
The file's size, in bytes.
|
8.5.2. Examples
The following examples show @
commands and assume n = 4:
Expression
|
Value of $x
|
@ x = ($n > 10 || $n < 5)
|
1
|
@ x = ($n >= 0 && $n <
3)
|
0
|
@ x = ($n << 2)
|
16
|
@ x = ($n >> 2)
|
1
|
@ x = $n % 2
|
0
|
@ x = $n % 3
|
1
|
The following examples show the first line of if or while
statements:
Expression
|
Meaning
|
while ($#argv != 0)
|
While there are arguments . . .
|
if ($today[1] = = "Fri")
|
If the first word is "Fri". . .
|
if (-f $argv[1])
|
If the first argument is a plain file. . .
|
if (! -d $tmpdir)
|
If tmpdir is not a directory. .
.
|
| | | 8.4. Variables | | 8.6. Command History |
Copyright © 2003 O'Reilly & Associates. All rights reserved.
|
|