199 lines
5.5 KiB
Groff
199 lines
5.5 KiB
Groff
'\"! tbl|mmdoc
|
|
'\"macro stdmacro
|
|
.if n .pH g1.expr @(#)expr 41.8 of 5/26/91
|
|
.\" Copyright 1991 UNIX System Laboratories, Inc.
|
|
.\" Copyright 1989, 1990 AT&T
|
|
.nr X
|
|
.if \nX=0 .ds x} expr 1 "Essential Utilities" "\&"
|
|
.if \nX=1 .ds x} expr 1 "Essential Utilities"
|
|
.if \nX=2 .ds x} expr 1 "" "\&"
|
|
.if \nX=3 .ds x} expr "" "" "\&"
|
|
.TH \*(x}
|
|
.if t .ds ' \h@.05m@\s+4\v@.333m@\'\v@-.333m@\s-4\h@.05m@
|
|
.if n .ds ' '
|
|
.if t .ds ` \h@.05m@\s+4\v@.333m@\`\v@-.333m@\s-4\h@.05m@
|
|
.if n .ds ` `
|
|
.SH NAME
|
|
\f4expr\f1 \- evaluate arguments as an expression
|
|
.SH SYNOPSIS
|
|
\f4expr\f1
|
|
.I arguments
|
|
.SH DESCRIPTION
|
|
The
|
|
.I arguments
|
|
are taken as an expression.
|
|
After evaluation, the result is written on the standard output.
|
|
Terms of the expression must be separated by blanks.
|
|
Characters special to the shell
|
|
must be escaped.
|
|
Note that \f40\fP is returned to indicate a zero value,
|
|
rather than the null string.
|
|
Strings containing blanks or other special characters should be quoted.
|
|
Integer-valued arguments may be preceded by a unary minus sign.
|
|
Internally, integers are treated as 32-bit, 2s complement numbers.
|
|
The length of the expression is limited to 512 bytes.
|
|
\f4expr\fP processes supplementary code set characters
|
|
according to the locale specified in the \f4LC_CTYPE\fP
|
|
environment variable [see \f4LANG\fP on \f4environ\fP(5)].
|
|
In regular expressions, pattern searches are performed
|
|
on characters, not bytes, as described in \f4regcomp\f1(5).
|
|
.PP
|
|
The operators and keywords are listed below. Characters that need to be
|
|
escaped in the shell [see \f4sh\fP(1)] are preceded by \f4\\\fP. The list
|
|
is in order of increasing precedence, with equal precedence operators
|
|
grouped within \f4{\|}\fP symbols.
|
|
.TP .5i
|
|
\f2expr1 \f4\e|\f2 expr2\f1
|
|
returns the evaluation of \f2expr1\fP if it is neither null nor \f40\fP,
|
|
otherwise returns the evaluation of \f2expr2\fP.
|
|
.TP .5i
|
|
\f2expr1 \f4\e&\f2 expr2\f1
|
|
returns the evaluation of \f2expr1\fP
|
|
if neither expression evaluates
|
|
to null or \f40\fP, otherwise returns \f40\fP.
|
|
.sp
|
|
Returns the result of a decimal integer comparison if both arguments
|
|
are integers; otherwise, returns the result of a string comparison using
|
|
the locale-specific collation sequence. The result of each comparison
|
|
will be \f41\fP if the specified relationship is true, or \f40\fP if the
|
|
relationship is false.
|
|
.TP .5i
|
|
\f2expr1 \f1{ \f4=\f1, \f4\e>\f1, \f4\e>=\f1, \f4\e<\f1, \f4\e<=\f1, \f4!=\f1 } \f2expr2\fP
|
|
returns the result of an integer comparison if both arguments are integers,
|
|
otherwise returns the result of a lexical comparison.
|
|
.TP .5i
|
|
\f2expr1 \f1{ \f4+\f1, \f4\- \f1} \f2expr2\fP
|
|
addition or subtraction of decimal integer-valued arguments.
|
|
.TP .5i
|
|
\f2expr1 \f1{ \f4\e\(**\f1, \f4/\f1, \f4% \f1} \f2expr2\fP
|
|
multiplication, division, or remainder of the decimal integer-valued
|
|
arguments.
|
|
.TP .5i
|
|
\f2expr1\f4 : \f2expr2\f1
|
|
The matching operator \f4:\fP compares \f2expr1\fP
|
|
with \f2expr2\fP, which must be a regular expression.
|
|
Regular expression syntax is defined is defined in the
|
|
\f4regcomp(5)\fP man page under the section titled:
|
|
\f2Basic Regular Expression\fP, Normally,
|
|
the matching operator returns the number of bytes matched
|
|
(\f40\fP on failure). Alternatively, if the pattern contains at least
|
|
one regular expression subexpression [\\( . . .\\)], the string
|
|
corresponding to \\1 will be returned.
|
|
.PP
|
|
The use of string arguments \f4length\fP, \f4substr\fP, \f4index\fP or
|
|
\f4match\fP produces unspecified results.
|
|
.SH EXAMPLES
|
|
Add 1 to the shell variable
|
|
\f4a\fP:
|
|
.PP
|
|
.RS
|
|
.ft 4
|
|
a=\*`expr\| $a\| +\| 1\*`
|
|
.ft 1
|
|
.RE
|
|
.ne 5
|
|
.PP
|
|
The following example emulates
|
|
\f4basename\fP(1)\-it returns the last segment of the path name \f4$a\fP.
|
|
For \f4$a\fP equal to either \f4/usr/abc/file\fP or just \f4file\fP, the
|
|
example returns \f4file\fP. (Watch out for \f4/\fP alone as an argument:
|
|
\f4expr\fP takes it as the division operator; see the NOTES below.)
|
|
.PP
|
|
.RS
|
|
.ft 4
|
|
expr $a : \*'.\(**/\e(.\(**\e)\*' \e| $a
|
|
.ft 1
|
|
.RE
|
|
.PP
|
|
Here is a better version of the previous example.
|
|
The addition of the
|
|
\f4//\f1
|
|
characters eliminates any ambiguity about the division operator and
|
|
simplifies the whole expression.
|
|
.PP
|
|
.RS
|
|
.ft 4
|
|
expr //$a : \*'.\(**/\e(.\(**\e)\*'
|
|
.ft 1
|
|
.RE
|
|
.PP
|
|
Return the number of characters in
|
|
\f4$VAR\f1:
|
|
.PP
|
|
.RS
|
|
.ft 4
|
|
expr $VAR : \*'.\(**\*'
|
|
.ft 1
|
|
.RE
|
|
.SH FILES
|
|
.TP
|
|
\f4/usr/lib/locale/\f2locale\f4/LC_MESSAGES/uxcore.abi\f1
|
|
language-specific message file [See \f4LANG\fP on \f4environ\f1 (5).]
|
|
.SH "SEE ALSO"
|
|
\f4regcomp\fP(5), \f4sh\fP(1)
|
|
.SH STDOUT
|
|
\f2expr\fP will evaluate the expression and write the result to standard
|
|
output. The character \f40\fP will be written to indicate a zero value and
|
|
nothing will be written to indicate a null string.
|
|
.SH STDERR
|
|
Used only for diagnostic messages.
|
|
.SH DIAGNOSTICS
|
|
As a side effect of expression evaluation,
|
|
\f4expr\fP
|
|
returns the following exit values:
|
|
.TS
|
|
n l.
|
|
0 if the expression is neither null nor \f40\fP
|
|
1 if the expression is null or \f40\fP
|
|
2 for invalid expressions.
|
|
>2 An error occurred.
|
|
.TE
|
|
.PP
|
|
.PD 0
|
|
.TP 1.75i
|
|
\f4syntax error\fP
|
|
for operator/operand errors
|
|
.TP
|
|
\f4non-numeric argument\fP
|
|
if arithmetic is attempted on such a string
|
|
.PD
|
|
.SH NOTES
|
|
After argument processing by the shell,
|
|
\f4expr\fP
|
|
cannot tell the difference between an operator and an operand
|
|
except by the value.
|
|
If
|
|
\f4$a\f1
|
|
is an
|
|
\f4=\f1,
|
|
the command:
|
|
.PP
|
|
.RS
|
|
.ft 4
|
|
expr $a = \*'=\*'
|
|
.ft 1
|
|
.RE
|
|
.PP
|
|
looks like:
|
|
.PP
|
|
.RS
|
|
.ft 4
|
|
expr = = =
|
|
.ft 1
|
|
.RE
|
|
.PP
|
|
as the arguments are passed to
|
|
\f4expr\fP
|
|
(and they are all taken as the
|
|
\f4=\f1
|
|
operator).
|
|
The following works:
|
|
.PP
|
|
.RS
|
|
.ft 4
|
|
expr X$a = X=
|
|
.ft 1
|
|
.RE
|
|
.\" @(#)expr.1 6.2 of 9/2/83
|
|
.Ee
|