Path: utzoo!utgpu!water!watmath!clyde!att!pacbell!ames!nrl-cmf!mailrus!tut.cis.ohio-state.edu!bloom-beacon!mit-eddie!uw-beaver!cornell!batcomputer!itsgw!nyser!cmx!jerryp From: jerryp@cmx.npac.syr.edu (Jerry Peek) Newsgroups: comp.unix.questions Subject: Re: basename(1) (Was Re:argv[0] in shellscript?) Message-ID: <538@cmx.npac.syr.edu> Date: 26 Jun 88 12:40:43 GMT References: <3680037@eecs.nwu.edu> Reply-To: jerryp@cmx.npac.syr.edu (Jerry Peek) Organization: Northeast Parallel Architectures Center, Syracuse NY Lines: 42 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