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 but can be remembered as follows:
* / %
+ -
Group all other expressions inside parentheses. Parentheses are required if the expression contains <, >, &, or |.
Operators can be one of the following types:
| Operator | Description |
|---|---|
| = | Assign value. |
| += -= | Reassign after addition/subtraction. |
| *= /= %= | Reassign after multiplication/division/remainder. |
| &= ^= |= | Reassign after bitwise AND/XOR/OR. |
| ++ | Increment. |
| -- | Decrement. |
| Operator | Description |
|---|---|
| * / % | Multiplication; integer division; modulus (remainder) |
| + - | Addition; subtraction |
| 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. |
| Operator | Description |
|---|---|
| == != | Equality; inequality |
| <= >= | Less than or equal to; greater than or equal to |
| < > | Less than; greater than |
Command substitution and filename expansion are performed on file before the test is performed. tcsh permits operators to be combined (e.g., -ef). The following is a list of the valid file inquiry operators:
| Operator | Description |
|---|---|
| -d file | The file is a directory. |
| -e file | The file exists. |
| -f file | The file is a plain file. |
| -o file | The user owns the file. |
| -r file | The user has read permission. |
| -w file | The user has write permission. |
| -x file | The user has execute permission. |
| -z file | The file has 0 size. |
| ! | Reverse the sense of any preceding inquiry. |
Some additional operators specific to tcsh are:
| Operator | Description |
|---|---|
| -b file | The file is a block special file. |
| -c file | The file is a character special 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. |
| -p file | The file is a named pipe (FIFO). |
| -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. |
| -X file | The file is executable and is in the path or is a shell built-in. |
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. |
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. . . |
Copyright © 2001 O'Reilly & Associates. All rights reserved.