Annotation of 43BSDTahoe/new/help/src/cshell/expressions, revision 1.1

1.1     ! root        1: .TI CSHELL/EXPRESSIONS
        !             2: Expressions:  Arithmetic, Logic Operations, and Decisions [DRAFT]
        !             3: 
        !             4: 
        !             5: An expression is a group of words (numbers, strings, and
        !             6: operators separated by spaces) which the C shell
        !             7: replaces by a single number, called its value.
        !             8: The act of replacing an expression by its value
        !             9: is a kind of substitution called evaluation, and forms the
        !            10: basis for all arithmetic, logic operations, and decisions.
        !            11: Loosely speaking, the term "expression" applies to any construct
        !            12: whatever in the C shell, but here it has a specific sense.
        !            13: 
        !            14: Expressions are only evaluated, hence useful,
        !            15: in conjunction with four built-in commands:
        !            16: .IP
        !            17: .nf
        !            18: \fBif (\fIexpression\fP) then
        !            19: \fBwhile (\fIexpression\fP)
        !            20: \fB@ \fIname\fP = (\fIexpression\fP)
        !            21: exit (\fIexpression\fP)\fR
        !            22: .fi
        !            23: .LP
        !            24: The if and while statements use the value of \fIexpression\fP
        !            25: to determine whether to execute the next command or group of commands.
        !            26: In particular, when the C shell produces a value of zero (false),
        !            27: the next group of commands is not done; otherwise it is done.
        !            28: The \fB@\fP command (yes, it is actually a command),
        !            29: not to be confused with the "at" command or the old default
        !            30: line-kill character, stores the value of \fIexpression\fP
        !            31: in the variable \fIname\fP.
        !            32: Except for providing the only way to store the results of an
        !            33: arithmetic or logic computation, it is identical to the "set" command.
        !            34: The exit command quits the current shell (e.g., in a script)
        !            35: and sets the command completion status (which other programs
        !            36: sometimes want to know) with the value of \fIexpression\fP.
        !            37: 
        !            38: The command forms above do not all require parentheses,
        !            39: but including them always is probably a good practice.
        !            40: Moreover, the words inside an expression generally require spaces
        !            41: between each other.
        !            42: These words ultimately consist of strings (such as filenames),
        !            43: numbers (such as 3.1415), and operators (such as +, \-, <, and >).
        !            44: Usually, however, you have entered some of the words as
        !            45: references to variable values, and so the shell will provide
        !            46: the actual values during variable substitution just prior
        !            47: to evaluating the expression.
        !            48: 
        !            49: The C shell has two main kinds of expressions:  arithmetic and logic.
        !            50: Of logic expressions it has three kinds:  boolean, file enquiry,
        !            51: and command status enquiry.
        !            52: Each category is described below.
        !            53: 
        !            54: Arithmetic Expressions
        !            55: 
        !            56: Here evaluation simply means doing the arithmetic indicated,
        !            57: and the value that replaces the expression is just the result.
        !            58: You may use any of the operators and precedences valid in the
        !            59: C programming language (without tremendously reliable results),
        !            60: and octal numbers must begin with a 0.
        !            61: The only numbers recognized by the shell are integers.
        !            62: 
        !            63: In the following examples, suppose
        !            64: we have issued "% set y = 8 ; set num = (4 168 10 8808)":
        !            65: 
        !            66: .nf
        !            67:     EXPRESSION          VALUE           EXPRESSION              VALUE
        !            68:     (2 + 2)             4               (5 - 27 + 7)            -15
        !            69:     (3 + $y + $num[3])  21              ($num[4] - 5)           8803
        !            70:     (5 * 5)             25              (24 / $y - 6)           -3
        !            71: 
        !            72: % @ x = (354 - 128 + 52 * 5 / 3)
        !            73: % echo Result is $x
        !            74: Result is 174
        !            75: % set y = (354 - 128 + 52 / 3)
        !            76: % echo Result is $y
        !            77: Result is 354 - 128 + 52 / 3
        !            78: .fi
        !            79: 
        !            80: 
        !            81: Logic Expressions
        !            82: 
        !            83: Evaluation means asking the question "Is it true that \fIexpression\fP?",
        !            84: where \fIexpression\fP is a statement or assertion phrased in C Shell terms.
        !            85: The value replacing it is 1 if the statement is true, 0 if false.
        !            86: There are three kinds of logic expressions:  boolean, file enquiry,
        !            87: and command status enquiry.
        !            88: Any nonzero valued expression is taken as true, whatever you
        !            89: may have intended (e.g., (3 + 2) is both 5 and true).
        !            90: 
        !            91: Boolean Expressions
        !            92: 
        !            93: These expressions, named after the English mathematician George Boole,
        !            94: make assertions about the relative sizes of two numbers.  There are
        !            95: 8 kinds of assertions,
        !            96: 
        !            97: .nf
        !            98:     ==    literal equals             !=    literal not-equals
        !            99:     =~    regular expr. equals       !~    regular expr. not equals
        !           100:     >     greater than               <     less than
        !           101:     >=    > or =                     <=    < or =,
        !           102: 
        !           103: 2 ways to combine two assertions,
        !           104: 
        !           105:     &&    and                        ||    or
        !           106: 
        !           107: and one way to negate an assertion
        !           108: 
        !           109:     !     not
        !           110: 
        !           111: Here are some examples:
        !           112: 
        !           113:     EXPRESSION          VALUE           EXPRESSION           VALUE
        !           114:     (3 > 2)             1               (-5 < $num[3])       1
        !           115:     (234 <= 234)        1               (234 <= 233)         0
        !           116:     (2 == $y || 3 > 2)  1               (1 > -8 && 3 > 1)    1
        !           117:     (1)                 1               (2 + (5 == 5))       3
        !           118:     (prog.c == *.c)     0               (prog.c =~ *.c)      1
        !           119: 
        !           120: % set x = 340
        !           121: % if ($x > 200)   echo You cannot afford it.
        !           122: You cannot afford it.
        !           123: .fi
        !           124: 
        !           125: File Enquiry Expressions
        !           126: 
        !           127: These have the form (-L \fIfilename\fP), where the `L' specifies one of
        !           128: 8 different assertions about \fIfilename\fP.
        !           129: You may employ these to have the shell determine whether or not
        !           130: permission is granted for a certain file, whether it is a directory, etc.
        !           131: The possible choices for `L' are:
        !           132: 
        !           133: .nf
        !           134:         r       read access             o       ownership
        !           135:         w       write access            z       zero size
        !           136:         x       execute access          f       plain file
        !           137:         e       existence               d       directory
        !           138: 
        !           139: Here are some examples.
        !           140: 
        !           141: % if (! -z /usr/spool/mail/jak)  echo You have mail.
        !           142: You have mail.
        !           143: % if (! -z netcopy.out)  echo ready
        !           144: % if (! -e stuff)   echo Not executable
        !           145: .fi
        !           146: 
        !           147: Command Status Expressions
        !           148: 
        !           149: These have the form ( { \fIcommand\fP } ), where the shell
        !           150: executes \fIcommand\fP during evaluation, and uses the
        !           151: completion status as the value of the variable.
        !           152: Note the spaces between \fIcommand\fP and the {}'s.
        !           153: Unlike command substitution, nothing special happens to
        !           154: the output of \fIcommand\fP.
        !           155: In fact, frequently you do not need output anyway since
        !           156: the expression relies upon the return status.
        !           157: In the examples below the \-s option
        !           158: suppresses any printed output in both cases.
        !           159: 
        !           160: For instance, to take a certain action in case two files differ,
        !           161: 
        !           162: if ( { cmp -s file1 file2 } ) then ...
        !           163: 
        !           164: or to leave a script and let the calling process know
        !           165: of a match for a string in a file,
        !           166: 
        !           167: exit ( { grep -s string file } )
        !           168: 
        !           169: 
        !           170: jak

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.