[BACK]Return to expr.1 CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / bin / expr

Annotation of src/bin/expr/expr.1, Revision 1.18

1.18    ! wiz         1: .\"    $NetBSD: expr.1,v 1.17 2001/10/18 11:00:03 wiz Exp $
1.8       cgd         2: .\"
1.9       jtc         3: .\" Written by J.T. Conklin <jtc@netbsd.org>.
                      4: .\" Public domain.
1.1       jtc         5: .\"
1.14      jdolecek    6: .Dd September 18, 2000
1.1       jtc         7: .Dt EXPR 1
                      8: .Os
                      9: .Sh NAME
                     10: .Nm expr
                     11: .Nd evaluate expression
                     12: .Sh SYNOPSIS
1.10      enami      13: .Nm
1.1       jtc        14: .Ar expression
                     15: .Sh DESCRIPTION
                     16: The
1.10      enami      17: .Nm
1.16      jdolecek   18: utility evaluates
1.1       jtc        19: .Ar expression
                     20: and writes the result on standard output.
                     21: .Pp
                     22: All operators are separate arguments to the
1.10      enami      23: .Nm
1.1       jtc        24: utility.
                     25: Characters special to the command interpreter must be escaped.
                     26: .Pp
1.6       jtc        27: Operators are listed below in order of increasing precedence.
                     28: Operators with equal precedence are grouped within { } symbols.
1.1       jtc        29: .Bl -tag -width indent
                     30: .It Ar expr1 Li | Ar expr2
1.16      jdolecek   31: Returns the evaluation of
                     32: .Ar expr1
1.1       jtc        33: if it is neither an empty string nor zero;
                     34: otherwise, returns the evaluation of
                     35: .Ar expr2 .
                     36: .It Ar expr1 Li & Ar expr2
                     37: Returns the evaluation of
                     38: .Ar expr1
                     39: if neither expression evaluates to an empty string or zero;
                     40: otherwise, returns zero.
                     41: .It Ar expr1 Li "{=, >, >=, <, <=, !=}" Ar expr2
1.16      jdolecek   42: Returns the results of integer comparison if both arguments are integers;
1.3       jtc        43: otherwise, returns the results of string comparison using the locale-specific
                     44: collation sequence.
1.1       jtc        45: The result of each comparison is 1 if the specified relation is true,
                     46: or 0 if the relation is false.
                     47: .It Ar expr1 Li "{+, -}" Ar expr2
                     48: Returns the results of addition or subtraction of integer-valued arguments.
                     49: .It Ar expr1 Li "{*, /, %}" Ar expr2
                     50: Returns the results of multiplication, integer division, or remainder of integer-valued arguments.
                     51: .It Ar expr1 Li : Ar expr2
1.16      jdolecek   52: The
1.17      wiz        53: .Dq \&:
1.16      jdolecek   54: operator matches
                     55: .Ar expr1
                     56: against
1.1       jtc        57: .Ar expr2 ,
                     58: which must be a regular expression.  The regular expression is anchored
1.16      jdolecek   59: to the beginning of  the string with an implicit
1.5       jtc        60: .Dq ^ .
1.1       jtc        61: .Pp
1.5       jtc        62: If the match succeeds and the pattern contains at least one regular
1.16      jdolecek   63: expression subexpression
                     64: .Dq "\e(...\e)" ,
                     65: the string corresponding to
1.1       jtc        66: .Dq "\e1"
                     67: is returned;
1.16      jdolecek   68: otherwise the matching operator returns the number of characters matched.
1.5       jtc        69: If the match fails and the pattern contains a regular expression subexpression
                     70: the null string is returned;
                     71: otherwise 0.
1.15      jdolecek   72: .It Ar "( " expr Li " )"
                     73: Parentheses are used for grouping in the usual manner.
1.1       jtc        74: .El
                     75: .Pp
1.15      jdolecek   76: Operator precedence (from highest to lowest):
                     77: .Bl -enum -compact -offset indent
                     78: .It
                     79: parentheses
                     80: .It
1.17      wiz        81: .Dq \&:
1.16      jdolecek   82: .It
                     83: .Dq "*" ,
                     84: .Dq "/" ,
                     85: and
                     86: .Dq "%"
                     87: .It
                     88: .Dq "+"
                     89: and
                     90: .Dq "-"
                     91: .It
                     92: compare operators
1.15      jdolecek   93: .It
                     94: .Dq &
                     95: .It
                     96: .Dq \Z'\*[tty-rn]'|
                     97: .El
1.18    ! wiz        98: .Sh EXIT STATUS
        !            99: The
        !           100: .Nm
        !           101: utility exits with one of the following values:
        !           102: .Bl -tag -width Ds -compact
        !           103: .It 0
        !           104: the expression is neither an empty string nor 0.
        !           105: .It 1
        !           106: the expression is an empty string or 0.
        !           107: .It 2
        !           108: the expression is invalid.
        !           109: .It >2
        !           110: an error occurred (such as memory allocation failure).
        !           111: .El
1.1       jtc       112: .Sh EXAMPLES
                    113: .Bl -enum
1.15      jdolecek  114: .It
1.1       jtc       115: The following example adds one to the variable a.
                    116: .Dl a=`expr $a + 1`
                    117: .It
1.15      jdolecek  118: The following example returns zero, due to deduction having higher precendence
                    119: than '&' operator.
                    120: .Dl expr 1 '&' 1 - 1
                    121: .It
1.1       jtc       122: The following example returns the filename portion of a pathname stored
1.14      jdolecek  123: in variable a.
                    124: .Dl expr "/$a" Li : '.*/\e(.*\e)'
1.1       jtc       125: .It
                    126: The following example returns the number of characters in variable a.
                    127: .Dl expr $a Li : '.*'
                    128: .El
                    129: .Sh STANDARDS
                    130: The
1.10      enami     131: .Nm
1.4       jtc       132: utility conforms to
                    133: .St -p1003.2 .
1.18    ! wiz       134: .Sh AUTHORS
1.14      jdolecek  135: Original implementation was written by
                    136: .An J.T. Conklin Aq jtc@netbsd.org .
                    137: It was rewritten in
                    138: .Nx 1.6
                    139: by
                    140: .An Jaromir Dolecek Aq jdolecek@netbsd.org .
1.15      jdolecek  141: .Sh COMPATIBILITY
1.14      jdolecek  142: This implementation of
                    143: .Nm
                    144: internally uses 64 bit represenation of integers and checks for
1.15      jdolecek  145: over- and underflows. It also treats / (division mark) and
                    146: option '--' correctly depending upon context.
1.14      jdolecek  147: .Pp
                    148: .Nm
                    149: on other systems (including
                    150: .Nx
                    151: up to and including
                    152: .Nx 1.5 )
                    153: might be not so graceful. Arithmetic results might be arbitrarily
                    154: limited on such systems, most commonly to 32 bit quantities.
                    155: This means such
                    156: .Nm
                    157: can only process values between -2147483648 and +2147483647.
                    158: .Pp
                    159: On other systems,
                    160: .Nm
                    161: might also not work correctly for regular expressions where
                    162: either side contains single forward slash, like this:
                    163: .Bd -literal -offset indent
                    164: expr / : '.*/\e(.*\e)'
                    165: .Ed
                    166: .Pp
                    167: If this is the case, you might use // (double forward slash)
                    168: to avoid abiquity with the division operator:
                    169: .Bd -literal -offset indent
                    170: expr "//$a" : '.*/\e(.*\e)'
                    171: .Ed
1.15      jdolecek  172: .Pp
                    173: According to
                    174: .St -p1003.2 ,
                    175: .Nm
                    176: has to recognize special option '--', treat it as an end of command
                    177: line options and ignore it.
                    178: Some
                    179: .Nm
                    180: implementations don't recognize it at all, others
                    181: might ignore it even in cases where doing so results in syntax
                    182: error. There should be same result for both following examples,
                    183: but it might not always be:
                    184: .Bl -enum -compact -offset indent
                    185: .It
                    186: expr -- : .
                    187: .It
                    188: expr -- -- : .
                    189: .El
                    190: Althrough
                    191: .Nx
                    192: .Nm
                    193: handles both cases correctly, you should not depend on this behaviour
                    194: for portability reasons and avoid passing bare '--' as first
                    195: argument.

CVSweb <webmaster@jp.NetBSD.org>