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: Chris F.A. Johnson
Subject: Re: basename, dirname why not built-in?
Date: Mon, 18 Feb 2002 16:14:35 GMT

On 15 Feb 2002, Jeff Sheinberg wrote:

> "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?

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?

Since this is a bash newsgroup I thought it appropriate to post
bash-specific functions. They are easily modified to work in any POSIX
shell.

As for the POSIX/SUS specs, dirname was written to conform to the
newest standard exactly (with the spec right in front of me); basename
is close to the spec, but there may be minor discrepancies.

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

-- 
        Chris F.A. Johnson                              address@hidden
        =================================================================
        address@hidden                http://cfaj.freeshell.org
        address@hidden        http://members.rogers.com/c.f.a.johnson



reply via email to

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