Shell programming 101: Is this an expr(1) bug ?

2003-02-18 Thread Poul-Henning Kamp


Running:

#!/bin/sh
set -ex

for p in ad2 ad0 ad1
do
a0=`expr $p : '^ad\([0-9]\)$'`
done

I get:

syv# sh _
+ expr ad2 : ^ad\([0-9]\)$
+ a0=2
+ expr ad0 : ^ad\([0-9]\)$
+ a0=0
syv# echo $?
1
syv# 

That looks like a bug to me...

-- 
Poul-Henning Kamp   | UNIX since Zilog Zeus 3.20
[EMAIL PROTECTED] | TCP/IP since RFC 956
FreeBSD committer   | BSD since 4.3-tahoe
Never attribute to malice what can adequately be explained by incompetence.

To Unsubscribe: send mail to [EMAIL PROTECTED]
with unsubscribe freebsd-current in the body of the message



Re: Shell programming 101: Is this an expr(1) bug ?

2003-02-18 Thread Brandon S. Allbery
On Tue, 2003-02-18 at 06:39, Poul-Henning Kamp wrote:
   + expr ad0 : ^ad\([0-9]\)$
   + a0=0
   syv# echo $?
   1
   syv# 
 
 That looks like a bug to me...

hilfy:202 Z$ /bin/expr ad0 : '^ad\([0-9]\)$'
0
zsh: exit 1 /bin/expr ad0 : '^ad\([0-9]\)$'

(Solaris 8 box)

The Solaris manpage claims:

EXIT STATUS
 As a side effect of expression evaluation, expr returns  the
 following exit values:
(...)
 1 if the expression is either NULL or 0

So it looks like correct behavior, if slightly odd in this particular
context.

-- 
brandon s allbery [openafs/solaris/japh/freebsd] [EMAIL PROTECTED]
system administrator [linux/heimdal/too many hats] [EMAIL PROTECTED]
electrical and computer engineering  KF8NH
carnegie mellon university  [better check the oblivious first -ke6sls]


To Unsubscribe: send mail to [EMAIL PROTECTED]
with unsubscribe freebsd-current in the body of the message



Re: Shell programming 101: Is this an expr(1) bug ?

2003-02-18 Thread Enache Adrian
On Tue, Feb 18, 2003 at 12:39:22PM +0100, Poul-Henning Kamp wrote:
 
 
 Running:
 
   #!/bin/sh
   set -ex
 
   for p in ad2 ad0 ad1
   do
   a0=`expr $p : '^ad\([0-9]\)$'`
   done
 
 I get:
 
   syv# sh _
   + expr ad2 : ^ad\([0-9]\)$
   + a0=2
   + expr ad0 : ^ad\([0-9]\)$
   + a0=0
   syv# echo $?
   1
   syv# 
 
 That looks like a bug to me...

Confusing but documented behaviour:

1. expr ad0 : ad\([0-9]\) = expr 0

man expr
If the match succeeds and the pattern contains at least one regu-
lar expression subexpression ``\(...\)'', the string correspond-
ing to ``\1'' is returned; otherwise the matching operator

2. `expr 0` = exit status = 1

man expr
 The expr utility exits with one of the following values:
  0   the expression is neither an empty string nor 0.
  1   the expression is an empty string or 0.

This behaviour is the same on linux, *BSD and probably other
systems too.

Regards
Adi

To Unsubscribe: send mail to [EMAIL PROTECTED]
with unsubscribe freebsd-current in the body of the message



Re: Shell programming 101: Is this an expr(1) bug ?

2003-02-18 Thread Chet Ramey
 Running:
 
   #!/bin/sh
   set -ex
 
   for p in ad2 ad0 ad1
   do
   a0=`expr $p : '^ad\([0-9]\)$'`
   done
 
 I get:
 
   syv# sh _
   + expr ad2 : ^ad\([0-9]\)$
   + a0=2
   + expr ad0 : ^ad\([0-9]\)$
   + a0=0
   syv# echo $?
   1

The `set -e' says to exit the shell if a simple command fails.  POSIX.2
says that the exit status of an assignment statement without an accompanying
command is either 0 or the exit status of the last command substitution
performed while expanding the rhs.  `expr' returns 1 when the result is
`0'.  Thus the assignment statement fails and the shell exits.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
( ``Discere est Dolere'' -- chet )

Chet Ramey, ITS, CWRU[EMAIL PROTECTED]http://cnswww.cns.cwru.edu/~chet/

To Unsubscribe: send mail to [EMAIL PROTECTED]
with unsubscribe freebsd-current in the body of the message