Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ expr(1) — AIX PS/2 1.2.1

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

ed, red

sh, Rsh



EXPR(1,C)                   AIX Commands Reference                    EXPR(1,C)



-------------------------------------------------------------------------------
expr



PURPOSE

Evaluates arguments as an expression.

SYNTAX


expr -- expression --|


DESCRIPTION

The expr command reads an expression, evaluates it, and writes the result to
standard output.  Within expression, you must separate each term with blanks,
precede characters special to the shell with a \( backslash), and quote strings
containing blanks or other special characters.  Note that expr returns 0 rather
than the null string, to indicate a zero value.  Integers may be preceded by a
unary minus sign.  Internally, integers are treated as 32-bit, two's-complement
numbers.

The operators and keywords are described in the following listing.  Characters
that need to be escaped are preceded by a \ (backslash).  The list is in order
of increasing precedence, with equal precedence operators grouped within {}
(braces).

\c \)
          Used to override the normal precedence of the operators.

expression1  \|  expression2
          Returns expression1 if it is neither null nor 0; otherwise, it
          returns expression2.

expression1  \"&"  expression2
          Returns expression1 if neither expression1 nor expression2 is null or
          0; otherwise, it returns 0.

expression1  "{  =,  \>,  \>=,  \<,  \<=,  !=  }"  expression2
          Returns the result of an integer comparison if both expressions are
          integers; otherwise, returns the result of a string comparison.

expression1  "{+,  - }"  expression2
          Adds or subtracts integer-valued arguments.

expression1  "{  \*,  /,   %  }"  expression2
          Multiplies, divides, or provides the remainder from the division of
          integer-valued arguments.




Processed November 8, 1990         EXPR(1,C)                                  1





EXPR(1,C)                   AIX Commands Reference                    EXPR(1,C)



expression1  :  expression2
          Compares expression1 with expression2, which must be a pattern;
          pattern syntax is the same as that of the ed command (see page
          ed-1), except that all patterns are anchored, so ^ (which
          anchors a pattern to the beginning of a line), is not a special
          character in this context.

          Normally, the matching operator returns the number of characters
          matched.  Alternatively, you can use the \(...\) symbols in
          expression2 to return a portion of expression1.  In an expression
          such as [a-z], the minus means "through" according to the current
          collating sequence.  A collating sequence may define equivalence
          classes for use in character ranges.  See the "Overview of
          International Character Support" in Managing the AIX Operating System
          for more information on collating sequences and equivalence classes.

The expr command returns the following exit values:

0  The expression is neither null nor 0.

1  The expression is null or 0.

2  The expression is invalid.

Note:  After parameter processing by the shell, the expr command cannot
       distinguish between an operator and an operand except by the value.
       Thus, if "$a" is "=", the command:

         expr $a = '='

       looks like:

         expr = = =

       after the shell passes the arguments to the expr command, and they all
       are taken as the = operator.  The following works:

         expr X$a = X=

EXAMPLES

  1. To modify a shell variable:

      COUNT=`expr $COUNT + 1`

    This command adds "1" to the shell variable "COUNT".  The expr command is
    enclosed in grave accents, which causes the shell to substitute the
    standard output from expr into the "COUNT=" command.  For more details, see
    "Command Substitution."

  2. To find the length of a shell variable:




Processed November 8, 1990         EXPR(1,C)                                  2





EXPR(1,C)                   AIX Commands Reference                    EXPR(1,C)



      LENGTH=`expr $STR : ".*"`

    This command sets "LENGTH" to the value given by the ":" (colon) operator.
    The pattern ".*" matches any string from beginning to end, so the colon
    operator gives the length of "STR" as the number of characters matched.
    Note that ".*" must be in quotes to prevent the shell from treating the "*"
    as a pattern-matching character.  The quotes themselves are not part of the
    pattern.

    If "STR" is set to the null string, the error message "expr: syntax error"
    is displayed because the shell does not normally pass null strings to
    commands.  In other words, the expr command sees only

      : .*

    (The shell also removes the quotation marks.)  This does not work because
    the colon operator requires two values.  This problem can be fixed by
    enclosing the shell variable in double quotation marks:

      LENGTH=`expr "$STR" : ".*"`

    Now if "STR" is null, "LENGTH" is set to zero.  Enclosing shell variables
    in double quotes is recommended in general.  However, do not enclose shell
    variables in single quotes.  See page sh-5 for details about quoting.

  3. To use part of a string:

      FLAG=`expr "$FLAG" : "-*\(.*\)"`

    This command removes leading minus signs, if any, from the shell variable
    "FLAG".  The colon operator gives the part of "FLAG" matched by the part of
    the pattern enclosed in \( \).  If you omit the \( \), the colon operator
    gives the number of characters matched.

    If "FLAG" is set to - (minus), a syntax error message is displayed because
    the shell substitutes the value of "FLAG" before running the expr command.
    The expr command does not know that the minus is the value of a variable.
    It can only see:

      - : -*\(.*\)

    and it interprets the first minus sign as the subtraction operator.  We can
    fix this problem by using:

      FLAG=`expr "x$FLAG" : "x-*\(.*\)"`

  4. To use the expr command in an if statement:

      if expr "$ANSWER" : "[yY]" >/dev/null
      then
         # ANSWER begins with "y" or "Y"
      fi



Processed November 8, 1990         EXPR(1,C)                                  3





EXPR(1,C)                   AIX Commands Reference                    EXPR(1,C)




    If "ANSWER" begins with "y" or "Y", the then part of the if statement is
    performed.  If the match succeeds, the result of the expression is "1" and
    expr returns an exit value of "0", which is recognized as the logical value
    TRUE by the if statement.  If the match fails, the result is "0" and the
    exit value is "1" (FALSE).

    Redirecting the standard output of the expr command to the /dev/null
    special file discards the result of the expression.  If you do not redirect
    the result, it is written to the standard output, which is usually your
    work station display.

  5. Consider the following expression:

      expr "$STR" = "="

    If "STR" has the value "=" (equal sign), after the shell processes this
    command expr sees the expression:

      = = =

    The expr command interprets this as three "=" operators in a row and
    displays a syntax error message.  This error happens whenever the value of
    a shell variable is the same as one of the expr operators.  You can avoid
    this problem by doing the following:

      expr "x$STR" = "x="

RELATED INFORMATION

See the following commands:  "ed, red" and "sh, Rsh."

See the "Introduction to International Character Support" in Managing the AIX
Operating System.





















Processed November 8, 1990         EXPR(1,C)                                  4



Typewritten Software • bear@typewritten.org • Edmonds, WA 98026