Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!apple!uokmax!occrsh!att!cbnewsm!rsl From: rsl@cbnewsm.att.com (randolph.little) Newsgroups: comp.unix.questions Subject: Re: Which script (was Re: comp.unix.questions) Summary: revised shell script Keywords: shell script, directory Message-ID: <1990Sep10.185245.25531@cbnewsm.att.com> Date: 10 Sep 90 18:52:45 GMT References: <1990Sep7.152354.9439@ecn.purdue.edu> <563@DIALix.UUCP> Organization: AT&T Bell Laboratories Lines: 51 In article <563@DIALix.UUCP>, bernie@DIALix.oz.au (Bernd Felsche) writes: > In article <1990Sep7.152354.9439@ecn.purdue.edu> patkar@helium.ecn.purdue.edu (The Silent Dodo) writes: > >I have a question about shell scripts. How can a shell script > >(sh or csh) find out its own file name? > > The solution is to parse the current PATH if the program name does > not begin with a '/'. On a BSD machine, you can use the 'which' > command (come Sys V have it also), or you can work it out by: > > : > # bourne shell script template > # > > prog=$0 > case $prog in > /*) echo $i # ^^^ Should be $0 > break > ;; # Add test for explicit current directory search: ./*) echo `pwd`/`basename $0` break ;; > *) > OIFS="$IFS" > IFS=":$IFS" > > for i in $PATH > do > if [ -x $i/$prog ] > then > break > fi > done > echo $i/$prog > IFS="$OIFS" > ;; > esac > > Instead of the echo's, you can use a fullname= etc to find what you > need. To get the directory name is left as an exercise :-) > > bernie > > p.s. Send no cash. Just Bullion. The first change corrects a typo, where $i should be $0; while the second change adds another pattern to handle the case where the command is invoked in the working directory by typing ./command (which bypasses $PATH). Randolph S. Little