[comp.unix.questions] basename

squires@eecs.nwu.edu (Matthew Squires) (06/07/88)

/ eecs.nwu.edu:comp.unix.questions /
 davidsen@steinmetz.ge.com (William E. Davidsen Jr) 
/  1:15 pm  Jun  6, 1988 /

> In article <1813@stpstn.UUCP> aad@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)...

BASENAME(1)         UNIX Programmer's Manual          BASENAME(1)

NAME
     basename - strip filename affixes

SYNOPSIS
     basename string [ suffix ]

DESCRIPTION
     Basename deletes any prefix ending in `/' and the suffix, if
     present in string, from string, and prints the result on the
     standard output. ...

% basename "foo"
foo
% basename "foo/something"
something
% 
Process shell finished


> 	bill davidsen		(wedu@ge-crd.arpa)
>   {uunet | philabs | seismo}!steinmetz!crdos1!davidsen
> "Stupidity, like virtue, is its own reward" -me

Matthew C. Squires, local GNUisance
squires@eecs.nwu.edu  OR  {ihnp4,oddjob,gargoyle,chinet}!nucsrl!squires

jerryp@cmx.npac.syr.edu (Jerry Peek) (06/26/88)

In article <3680037@eecs.nwu.edu> squires@eecs.nwu.edu (Matthew Squires) writes:
> / eecs.nwu.edu:comp.unix.questions /
>  davidsen@steinmetz.ge.com (William E. Davidsen Jr) 
> /  1:15 pm  Jun  6, 1988 /
> 
> > In article <1813@stpstn.UUCP> aad@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@cmx.npac.syr.edu
  +1 315 423-4120