2015-10-08 15:15:08 +00:00
|
|
|
.Dd 2015-10-08
|
2015-01-31 13:20:02 +00:00
|
|
|
.Dt EXPR 1
|
2015-01-31 19:37:03 +00:00
|
|
|
.Os sbase
|
2014-09-29 13:22:59 +00:00
|
|
|
.Sh NAME
|
|
|
|
.Nm expr
|
|
|
|
.Nd evaluate expression
|
|
|
|
.Sh SYNOPSIS
|
2015-01-30 20:17:58 +00:00
|
|
|
.Nm
|
2014-09-29 13:22:59 +00:00
|
|
|
.Ar expression
|
|
|
|
.Sh DESCRIPTION
|
|
|
|
.Nm
|
2015-01-25 15:49:46 +00:00
|
|
|
evaluates
|
2014-09-29 13:22:59 +00:00
|
|
|
.Ar expression
|
2015-01-25 15:49:46 +00:00
|
|
|
and writes the result to stdout.
|
2014-09-29 13:22:59 +00:00
|
|
|
.Pp
|
2015-01-25 15:49:46 +00:00
|
|
|
There are two elemental expressions,
|
|
|
|
.Sy integer
|
|
|
|
and
|
|
|
|
.Sy string.
|
|
|
|
Let
|
|
|
|
.Sy expr
|
|
|
|
be a non-elemental expression and
|
|
|
|
.Sy expr1 ,
|
|
|
|
.Sy expr2
|
2020-03-01 22:37:31 +00:00
|
|
|
arbitrary expressions.
|
|
|
|
Then
|
2015-01-25 15:49:46 +00:00
|
|
|
.Sy expr
|
|
|
|
has the recursive form
|
|
|
|
.Sy expr = [(] expr1 operand expr2 [)].
|
2014-09-29 13:22:59 +00:00
|
|
|
.Pp
|
2015-01-25 15:49:46 +00:00
|
|
|
With
|
|
|
|
.Sy operand
|
2015-02-01 20:28:17 +00:00
|
|
|
being in order of increasing precedence:
|
2015-01-25 15:49:46 +00:00
|
|
|
.Bl -tag -width Ds
|
|
|
|
.It |
|
|
|
|
Evaluate to
|
|
|
|
.Sy expr1
|
|
|
|
if it is neither an empty string nor 0; otherwise evaluate to
|
|
|
|
.Sy expr2 .
|
|
|
|
.It &
|
|
|
|
Evaluate to
|
|
|
|
.Sy expr1
|
|
|
|
if
|
|
|
|
.Sy expr1
|
|
|
|
and
|
|
|
|
.Sy expr2
|
|
|
|
are neither empty strings nor 0; otherwise evaluate to 0.
|
|
|
|
.It = > >= < <= !=
|
|
|
|
If
|
|
|
|
.Sy expr1
|
|
|
|
and
|
|
|
|
.Sy expr2
|
|
|
|
are integers, evaluate to 1 if the relation is true and 0 if it is false.
|
|
|
|
If
|
|
|
|
.Sy expr1
|
|
|
|
and
|
|
|
|
.Sy expr2
|
|
|
|
are strings, apply the relation to the return value of
|
|
|
|
.Xr strcmp 3 .
|
|
|
|
.It + -
|
|
|
|
If
|
|
|
|
.Sy expr1
|
|
|
|
and
|
|
|
|
.Sy expr2
|
|
|
|
are integers, evaluate to their sum or subtraction.
|
|
|
|
.It * / %
|
|
|
|
If
|
|
|
|
.Sy expr1
|
|
|
|
and
|
|
|
|
.Sy expr2
|
2015-02-01 20:28:17 +00:00
|
|
|
are integers, evaluate to their multiplication, division or remainder.
|
2015-01-25 15:49:46 +00:00
|
|
|
.It :
|
|
|
|
Evaluate to the number of characters matched in
|
|
|
|
.Sy expr1
|
|
|
|
against
|
|
|
|
.Sy expr2 . expr2
|
|
|
|
is anchored with an implicit '^'.
|
2014-09-29 13:22:59 +00:00
|
|
|
.Pp
|
2015-01-25 15:49:46 +00:00
|
|
|
You can't directly match the empty string, since zero matched characters
|
2020-03-01 22:37:31 +00:00
|
|
|
resolve equally to a failed match.
|
|
|
|
To work around this limitation, use "expr X'' : 'X$' instead of "expr ''
|
|
|
|
: '$'"
|
2014-09-29 13:22:59 +00:00
|
|
|
.El
|
|
|
|
.Sh EXIT STATUS
|
2015-01-25 15:49:46 +00:00
|
|
|
.Bl -tag -width Ds
|
2014-09-29 13:22:59 +00:00
|
|
|
.It 0
|
2015-01-25 15:49:46 +00:00
|
|
|
.Ar expression
|
2015-02-09 23:34:24 +00:00
|
|
|
is neither an empty string nor 0.
|
2014-09-29 13:22:59 +00:00
|
|
|
.It 1
|
2015-01-25 15:49:46 +00:00
|
|
|
.Ar expression
|
2015-02-09 23:34:24 +00:00
|
|
|
is an empty string or 0.
|
2014-09-29 13:22:59 +00:00
|
|
|
.It 2
|
2015-01-25 15:49:46 +00:00
|
|
|
.Ar expression
|
2015-02-09 23:34:24 +00:00
|
|
|
is invalid.
|
2015-01-25 15:49:46 +00:00
|
|
|
.It > 2
|
2015-02-09 23:34:24 +00:00
|
|
|
An error occurred.
|
2014-09-29 13:22:59 +00:00
|
|
|
.El
|
|
|
|
.Sh SEE ALSO
|
2015-01-25 15:49:46 +00:00
|
|
|
.Xr test 1
|
2014-09-29 13:22:59 +00:00
|
|
|
.Sh STANDARDS
|
|
|
|
The
|
|
|
|
.Nm
|
|
|
|
utility is compliant with the
|
2015-03-27 16:22:48 +00:00
|
|
|
.St -p1003.1-2013
|
2014-09-29 13:22:59 +00:00
|
|
|
specification.
|