an expression is a group of words (numbers, strings, and operators separated by spaces

Upload: raradewrer

Post on 30-May-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 An Expression is a Group of Words (Numbers, Strings, And Operators Separated by Spaces

    1/24

    xpressions: Arithmetic, Logic Operations, and DecisionsAn expression is a group of words (numbers, strings, and operators separated by spaces) which the Cshell replaces by a single number, called its value. The act of replacing an expression by its value is akind of substitution called evaluation, and forms the basis for all arithmetic, logic operations, anddecisions. Loosely speaking, the term "expression" applies to any construct whatever in the C shell, buthere it has a specific sense.

    Expressions are only evaluated, hence useful, in conjunction with four built-in commands:

    if (expression) thenwhile (expression)@ name = (expression)exit (expression)

    The if and while statements use the value of expression to determine whether to execute the nextcommand or group of commands. In particular, when the C shell produces a value of zero (false), thenext group of commands is not done; otherwise it is done. The @ command (yes, it is actually acommand), not to be confused with the "at" command or the old default line-kill character, stores thevalue of expression in the variable name. Except for providing the only way to store the results of anarithmetic or logic computation, it is identical to the "set" command. The exit command quits thecurrent shell (e.g., in a script) and sets the command completion status (which other programssometimes want to know) with the value of expression.

    The command forms above do not all require parentheses, but including them always is probably agood practice. Moreover, the words inside an expression generally require spaces between each other.These words ultimately consist of strings (such as filenames), numbers (such as 3.1415), and operators(such as +, -, ). Usually, however, you have entered some of the words as references to variablevalues, and so the shell will provide the actual values during variable substitution just prior toevaluating the expression.

    The C shell has two main kinds of expressions: arithmetic and logic. Of logic expressions it has threekinds: boolean, file enquiry, and command status enquiry. Each category is described below.

    Arithmetic ExpressionsHere evaluation simply means doing the arithmetic indicated, and the value that replaces the expressionis just the result. You may use any of the operators and precedences valid in the C programminglanguage (without tremendously reliable results), and octal numbers must begin with a 0. The onlynumbers recognized by the shell are integers.

    In the following examples, suppose we have issued "% set y = 8 ; set num = (4 168 10 8808)":

    EXPRESSION VALUE EXPRESSION VALUE(2 + 2) 4 (5 - 27 + 7) -15(3 + $y + $num[3]) 21 ($num[4] - 5) 8803(5 * 5) 25 (24 / $y - 6) -3

    % @ x = (354 - 128 + 52 * 5 / 3)% echo Result is $xResult is 174

  • 8/9/2019 An Expression is a Group of Words (Numbers, Strings, And Operators Separated by Spaces

    2/24

    % set y = (354 - 128 + 52 / 3)% echo Result is $yResult is 354 - 128 + 52 / 3

    Logic ExpressionsEvaluation means asking the question "Is it true that expression?", where expression is a statement or

    assertion phrased in C Shell terms. The value replacing it is 1 if the statement is true, 0 if false. Thereare three kinds of logic expressions: boolean, file enquiry, and command status enquiry. Any nonzerovalued expression is taken as true, whatever you may have intended (e.g., (3 + 2) is both 5 and true).Boolean ExpressionsThese expressions, named after the English mathematician George Boole, make assertions about therelative sizes of two numbers. There are 8 kinds of assertions,

    == literal equals != literal not-equals=~ regular expr. equals !~ regular expr. not equals> greater than < less than>= > or = 2) 1 (-5 < $num[3]) 1(234 -8 && 3 > 1) 1(1) 1 (2 + (5 == 5)) 3(prog.c == *.c) 0 (prog.c =~ *.c) 1

    % set x = 340% if ($x > 200) echo You cannot afford it.You cannot afford it.

    File Enquiry ExpressionsThese have the form (-L filename), where the `L' specifies one of 8 different assertions about filename.You may employ these to have the shell determine whether or not permission is granted for a certainfile, whether it is a directory, etc. The possible choices for `L' are:

    r read access o ownershipw write access z zero sizex execute access f plain filee existence d directory

  • 8/9/2019 An Expression is a Group of Words (Numbers, Strings, And Operators Separated by Spaces

    3/24

    Here are some examples.

    % if (! -z /usr/spool/mail/jak) echo You have mail.You have mail.% if (! -z netcopy.out) echo ready% if (! -e stuff) echo Not executable

    Command Status ExpressionsThese have the form ( { command } ), where the shell executes command during evaluation, and usesthe completion status as the value of the variable. Note the spaces between command and the {}'s.Unlike command substitution, nothing special happens to the output of command. In fact, frequentlyyou do not need output anyway since the expression relies upon the return status. In the examplesbelow the -s option suppresses any printed output in both cases.

    For instance, to take a certain action in case two files differ,

    if ( { cmp -s file1 file2 } ) then ...

    or to leave a script and let the calling process know of a match for a string in a file,

    exit ( { grep -s string file } )

    Comments to [email protected] 1998-2010 UC Re

  • 8/9/2019 An Expression is a Group of Words (Numbers, Strings, And Operators Separated by Spaces

    4/24

  • 8/9/2019 An Expression is a Group of Words (Numbers, Strings, And Operators Separated by Spaces

    5/24

  • 8/9/2019 An Expression is a Group of Words (Numbers, Strings, And Operators Separated by Spaces

    6/24

    Arya

  • 8/9/2019 An Expression is a Group of Words (Numbers, Strings, And Operators Separated by Spaces

    7/24

    leader

  • 8/9/2019 An Expression is a Group of Words (Numbers, Strings, And Operators Separated by Spaces

    8/24

  • 8/9/2019 An Expression is a Group of Words (Numbers, Strings, And Operators Separated by Spaces

    9/24

  • 8/9/2019 An Expression is a Group of Words (Numbers, Strings, And Operators Separated by Spaces

    10/24

  • 8/9/2019 An Expression is a Group of Words (Numbers, Strings, And Operators Separated by Spaces

    11/24

  • 8/9/2019 An Expression is a Group of Words (Numbers, Strings, And Operators Separated by Spaces

    12/24

    go wherever

  • 8/9/2019 An Expression is a Group of Words (Numbers, Strings, And Operators Separated by Spaces

    13/24

    computer workers

    along CikarangBekasi depok

    tangerang arya10000000000000000000 workers following

    the movement ofivory palm harbor of

    Jakarta Cikarangbekasi tangerang

  • 8/9/2019 An Expression is a Group of Words (Numbers, Strings, And Operators Separated by Spaces

    14/24

    follow a leader arya

    arya

  • 8/9/2019 An Expression is a Group of Words (Numbers, Strings, And Operators Separated by Spaces

    15/24

    go wherever

    computer workersalong CikarangBekasi depoktangerang arya100000000000000000

  • 8/9/2019 An Expression is a Group of Words (Numbers, Strings, And Operators Separated by Spaces

    16/24

    00 workers following

    the movement ofivory palm harbor of

    Jakarta Cikarangbekasi tangerangfollow a leader arya

    arya

  • 8/9/2019 An Expression is a Group of Words (Numbers, Strings, And Operators Separated by Spaces

    17/24

  • 8/9/2019 An Expression is a Group of Words (Numbers, Strings, And Operators Separated by Spaces

    18/24

    go wherever

  • 8/9/2019 An Expression is a Group of Words (Numbers, Strings, And Operators Separated by Spaces

    19/24

    computer workers

    along CikarangBekasi depok

    tangerang arya10000000000000000000 workers following

    the movement ofivory palm harbor of

    Jakarta Cikarangbekasi tangerang

  • 8/9/2019 An Expression is a Group of Words (Numbers, Strings, And Operators Separated by Spaces

    20/24

    follow a leader arya

    arya

  • 8/9/2019 An Expression is a Group of Words (Numbers, Strings, And Operators Separated by Spaces

    21/24

  • 8/9/2019 An Expression is a Group of Words (Numbers, Strings, And Operators Separated by Spaces

    22/24

  • 8/9/2019 An Expression is a Group of Words (Numbers, Strings, And Operators Separated by Spaces

    23/24

    arya go wherever

    computer workersalong Cikarang

    Bekasi depoktangerang arya10000000000000000000 workers followingthe movement of

    ivory palm harbor ofJakarta Cikarang

  • 8/9/2019 An Expression is a Group of Words (Numbers, Strings, And Operators Separated by Spaces

    24/24

    bekasi tangerang

    follow a leader aryaarya