5.5. Expressions

 < Day Day Up > 

Expressions are used in @ (the C shell math operator), 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. It is easiest to just remember the following precedence rules:

  • * / %

  • + -

  • Group all other expressions inside ( )s; parentheses are required if the expression contains <, >, &, or |

5.5.1. Operators

Operators can be one of the following types.

5.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.


5.5.1.2. Arithmetic operators

Operator

Description

* / %

Multiplication; integer division; modulus (remainder).

+ -

Addition; subtraction.


5.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 (short-circuit).

||

Logical OR (short-circuit).

{ 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.


5.5.1.4. Comparison operators

Operator

Description

== !=

Equality; inequality.

<= >=

Less than or equal to; greater than or equal to.

< >

Less than; greater than.

=~

String on left matches a filename pattern on right containing *, ?, or [...].

!~

String on left does not match a filename pattern on right containing *, ?, or [...].


5.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 current user owns the file.

-p file

The file is a named pipe (FIFO).

-r file

The current 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 current user has write permission.

-x file

The current 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 zero size.

!

Reverse the sense of any following inquiry, which may be any of the tests in this table.


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 with 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.


These operators may only be used in multioperator tests, and they must be the last operator in such tests.

5.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 ($file !~ *.[zZ])

If the file doesn't end with .z or .Z ...

if ($argv[1] =~ chap?)

If the first argument is chap followed by a single character ...

if (-f $argv[1])

If the first argument is a plain file ...

if (! -d $tmpdir)

If tmpdir is not a directory ...


     < Day Day Up > 


    Unix in a Nutshell
    Unix in a Nutshell, Fourth Edition
    ISBN: 0596100299
    EAN: 2147483647
    Year: 2005
    Pages: 201

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net