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: Jeff Sheinberg
Subject: Re: basename, dirname why not built-in?
Date: 15 Feb 2002 15:57:12 -0500
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.1

"Chris F.A. Johnson" <address@hidden> 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?

The book "UNIX for the Impatient" also has a nice description of
the correct algorithms for basename and dirname.

 >      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
 > 
 > -- 
 >      Chris F.A. Johnson                              address@hidden
 >      =================================================================
 >      address@hidden                http://cfaj.freeshell.org
 >      address@hidden        http://members.rogers.com/c.f.a.johnson

    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

HTH,
-- 
Jeff Sheinberg  <address@hidden>



reply via email to

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