expr(1): Add sh(1) versions of examples, remove an incorrect example.
The three examples are better done using sh(1) itself these days. The example expr -- "$a" : ".*" is incorrect in the general case, as "$a" may be an operator. MFC after: 2 weeks
This commit is contained in:
parent
f0a7fc8b30
commit
f390d8d927
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=212390
@ -30,7 +30,7 @@
|
|||||||
.\"
|
.\"
|
||||||
.\" $FreeBSD$
|
.\" $FreeBSD$
|
||||||
.\"
|
.\"
|
||||||
.Dd July 12, 2004
|
.Dd September 9, 2010
|
||||||
.Dt EXPR 1
|
.Dt EXPR 1
|
||||||
.Os
|
.Os
|
||||||
.Sh NAME
|
.Sh NAME
|
||||||
@ -217,6 +217,9 @@ command, one might rearrange the expression:
|
|||||||
More generally, parenthesize possibly-negative values:
|
More generally, parenthesize possibly-negative values:
|
||||||
.Dl "a=$(expr \e( $a \e) + 1)"
|
.Dl "a=$(expr \e( $a \e) + 1)"
|
||||||
.It
|
.It
|
||||||
|
With shell arithmetic, no escaping is required:
|
||||||
|
.Dl "a=$((a + 1))"
|
||||||
|
.It
|
||||||
This example prints the filename portion of a pathname stored
|
This example prints the filename portion of a pathname stored
|
||||||
in variable
|
in variable
|
||||||
.Va a .
|
.Va a .
|
||||||
@ -229,6 +232,12 @@ The
|
|||||||
.Li //
|
.Li //
|
||||||
characters resolve this ambiguity.
|
characters resolve this ambiguity.
|
||||||
.Dl "expr \*q//$a\*q \&: '.*/\e(.*\e)'"
|
.Dl "expr \*q//$a\*q \&: '.*/\e(.*\e)'"
|
||||||
|
.It
|
||||||
|
With modern
|
||||||
|
.Xr sh 1
|
||||||
|
syntax,
|
||||||
|
.Dl "\*q${a##*/}\*q"
|
||||||
|
expands to the same value.
|
||||||
.El
|
.El
|
||||||
.Pp
|
.Pp
|
||||||
The following examples output the number of characters in variable
|
The following examples output the number of characters in variable
|
||||||
@ -237,19 +246,21 @@ Again, if
|
|||||||
.Va a
|
.Va a
|
||||||
might begin with a hyphen, it is necessary to prevent it from being
|
might begin with a hyphen, it is necessary to prevent it from being
|
||||||
interpreted as an option to
|
interpreted as an option to
|
||||||
.Nm .
|
.Nm ,
|
||||||
|
and
|
||||||
|
.Va a
|
||||||
|
might be interpreted as an operator.
|
||||||
.Bl -bullet
|
.Bl -bullet
|
||||||
.It
|
.It
|
||||||
If the
|
To deal with all of this, a complicated command
|
||||||
.Nm
|
|
||||||
command conforms to
|
|
||||||
.St -p1003.1-2001 ,
|
|
||||||
this is simple:
|
|
||||||
.Dl "expr -- \*q$a\*q \&: \*q.*\*q"
|
|
||||||
.It
|
|
||||||
For portability to older systems, however, a more complicated command
|
|
||||||
is required:
|
is required:
|
||||||
.Dl "expr \e( \*qX$a\*q \&: \*q.*\*q \e) - 1"
|
.Dl "expr \e( \*qX$a\*q \&: \*q.*\*q \e) - 1"
|
||||||
|
.It
|
||||||
|
With modern
|
||||||
|
.Xr sh 1
|
||||||
|
syntax, this can be done much more easily:
|
||||||
|
.Dl "${#a}"
|
||||||
|
expands to the required number.
|
||||||
.El
|
.El
|
||||||
.Sh SEE ALSO
|
.Sh SEE ALSO
|
||||||
.Xr sh 1 ,
|
.Xr sh 1 ,
|
||||||
|
Loading…
Reference in New Issue
Block a user