basename(1) (Was Re:argv[0] in shellscript?)

Jerry Peek jerryp at cmx.npac.syr.edu
Sun Jun 26 22:40:43 AEST 1988


In article <3680037 at eecs.nwu.edu> squires at eecs.nwu.edu (Matthew Squires) writes:
> / eecs.nwu.edu:comp.unix.questions /
>  davidsen at steinmetz.ge.com (William E. Davidsen Jr) 
> /  1:15 pm  Jun  6, 1988 /
> 
> > In article <1813 at stpstn.UUCP> aad at stpstn.UUCP (Anthony A. Datri) writes:
> > | 
> > | I want to write a script that will have multiple links to it, and be
> > | able to tell what name it was invoked with.  Ideas?
> > 
> >   How about $0? That's the name of the called program.  Watch out if you
> > have a full pathname (ie.  $0 = foo/something). ...
> 
> Then perhaps you could use basename(1)...

But using basename means that the shell has to start another process.
I saw another article where the person mentioned using shell wildcards
to get around the full-pathname problem.  That works great, and it doesn't
start a child process.

Here are two examples of a program with four links (names): ll, lf, lg, and
lr.  The left-hand column shows the sh version; the right-hand shows how to
do it in csh.  Since I put a * before each matching pattern, it always works:

#! /bin/sh                          #! /bin/csh -f
case "$0" in                        switch ($0)
*ll) ls -l $* ;;                    case *ll:
*lf) ls -F $* ;;                        ls -l $*; breaksw
*lg) ls -lg $* ;;                   case *lf:
*lr) ls -lR $* ;;                       ls -F $*; breaksw
*) echo "$0: Wrong name!" 1>&2      case *lg:
   exit 1                               ls -lg $*; breaksw
   ;;                               case *lr:
esac                                    ls -lR $*; breaksw
                                    default:
                                        echoerr "${0}: Wrong name\!"
                                        breaksw
                                    endsw

--Jerry Peek, Northeast Parallel Architectures Center, Syracuse, NY
  jerryp at cmx.npac.syr.edu
  +1 315 423-4120



More information about the Comp.unix.questions mailing list