bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: basename, dirname why not built-in?


From: Andreas Schwab
Subject: Re: basename, dirname why not built-in?
Date: Mon, 18 Feb 2002 23:35:09 +0100
User-agent: Gnus/5.090005 (Oort Gnus v0.05) Emacs/21.2.50 (ia64-suse-linux)

"Chris F.A. Johnson" <c.f.a.johnson@rogers.com> writes:

|> On 15 Feb 2002, Jeff Sheinberg wrote:
|> 
|> > "Chris F.A. Johnson" <chris@rogers.com> writes:
|> >
|> >  > On 13 Feb 2002, Dan Jacobson wrote:
|> >  >
|> >  > > $ type basename
|> >  > > basename is /bin/basename
|> >  > >
|> >  > > OK, but I've seen a lost of built-ins that do more than basename...
|> >  > > So how can one justify having built-ins for some of the simple stuff
|> >  > > in /bin but not all of them?  Got to make a definition of where to
|> >  > > draw the line.  Lots of them or none of them.
|> >  > >
|> >  > > $ type dirname
|> >  > > dirname is /usr/bin/dirname
|> >  > >
|> >  > > Isn't that one just child's play to implement... wouldn't there be no
|> >  > > better a justification for a built-in than dirname?  [save lots of CPU
|> >  > > time per call.]
|> >
|> > It's not exactly child's play, I suggest you take a look at the
|> > posix or susv2 specs to get it right, but why bother, when the OS
|> > supplied utilities have already done this for you?
|> 
|> Why bother? For the reason cited, of course.
|> 
|> > The book "UNIX for the Impatient" also has a nice description of
|> > the correct algorithms for basename and dirname.
|> 
|> Sounds interesting; I'll have to look for it.
|> 
|> >  >         basename ()
|> >  >         {
|> >  >             local path=$1;
|> >  >             local suffix=$2;
|> >  >             case "$path" in
|> >  >                 */ | "") basename ${path%/} $suffix ;;
|> >  >                 *) path=${path##*/};
|> >  >                    echo ${path%$suffix}
|> >  >                    ;;
|> >  >             esac
|> >  >         }
|> >  >
|> >  >         dirname ()
|> >  >         {
|> >  >             case $1 in
|> >  >                 */) dirname ${1%/}  ;;
|> >  >                 "") echo '/' ;;
|> >  >                 */*) echo "${1%/*}" ;;
|> >  >                 *) echo '.' ;;
|> >  >             esac
|> >  >         }
|> >  >
|> >  >         export -f basename dirname
|> >
|> >     100 jeff ~ $ basename //
|> >     /
|> >
|> >     101 jeff ~ $ sh
|> >
|> >     100 jeff ~ $ . /tmp/dirbase  # source the above function definitions.
|> >
|> >     101 jeff ~ $ basename //
|> >     sh: $2: unbound variable
|> >     --> exit status 1
|> >
|> >     102 jeff ~ $ set +u
|> >
|> >     103 jeff ~ $ basename //
|> >     Segmentation fault (core dumped)
|> >     --> exit status 139
|> 
|> You're not using bash, are you?

Your functions do not have sufficient quotes, causing infinite loops.

|> Both functions could be improved somewhat: I don't like the recursive
|> calls to remove trailing slashes.

What's wrong with loops?

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE GmbH, Deutschherrnstr. 15-19, D-90429 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."



reply via email to

[Prev in Thread] Current Thread [Next in Thread]