Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 18 Feb 2003 18:01:20 +0200
From:      Enache Adrian <enache@rdslink.ro>
To:        Poul-Henning Kamp <phk@phk.freebsd.dk>
Cc:        current@freebsd.org
Subject:   Re: Shell programming 101:  Is this an expr(1) bug ?
Message-ID:  <20030218160120.GA859@ratsnest.hole>
In-Reply-To: <57374.1045568362@critter.freebsd.dk>
References:  <57374.1045568362@critter.freebsd.dk>

next in thread | previous in thread | raw e-mail | index | archive | help
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 majordomo@FreeBSD.org
with "unsubscribe freebsd-current" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030218160120.GA859>