[comp.unix.questions] comp.unix.questions

patkar@helium.ecn.purdue.edu (The Silent Dodo) (09/07/90)

I have a question about shell scripts.  How can a shell script
(sh or csh) find out its own file name?  Actually, I need to
know only the directory in which it resides.

My shell scripts need, say, some "sed" script files and it is
always nice to keep them in the same directory as the shell
script.  So when I move the shell scripts to a different
directory or a different machine I have to make changes in the
pathname for the sed scripts.  Is there a better way to do
this?

 -- Anant
(patkar@cn.ecn.purdue.edu)

gwyn@smoke.BRL.MIL (Doug Gwyn) (09/07/90)

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?  Actually, I need to
>know only the directory in which it resides.

Here we go again.  The shell script inode does not reside in any
particular directory nor has it any particular inherent pathname.
Several different directory entries can link to the same inode.
Thus there is no unambiguous answer to your question.

>My shell scripts need, say, some "sed" script files and it is
>always nice to keep them in the same directory as the shell
>script.  So when I move the shell scripts to a different
>directory or a different machine I have to make changes in the
>pathname for the sed scripts.  Is there a better way to do
>this?

Consider using the "here document" feature << to embed the subscripts
in-line.  Otherwise, you pretty much have to make at least one edit
to each script that refers to other scripts when they have been moved.

root@ninja.dell.com (Randy Davis) (09/08/90)

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?  Actually, I need to
|know only the directory in which it resides.

   Hmmmmm..... Off the top of my head, how about this?:

COMMAND=`basename $0`
DIR=`type $COMMAND | sed -e 's:^.* /:/:' -e "s:/$COMMAND$::"`
echo "Parent directory of \"$COMMAND\" is \"$DIR\""

  It works under sh.  Although I am sure there is an "awk" script that might
do it in less lines, I bet this is possibly faster.

Randy Davis					UUCP: rjd@ninja.dell.com

-- 

nwosuck@aix.aix.kingston.ibm.com (Kingsley Nwosu) (10/11/90)

In article <9434@uudell.dell.com>, root@ninja.dell.com (Randy Davis) 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?  Actually, I need to
> |know only the directory in which it resides.
> 
>    Hmmmmm..... Off the top of my head, how about this?:
> 
> COMMAND=`basename $0`
> DIR=`type $COMMAND | sed -e 's:^.* /:/:' -e "s:/$COMMAND$::"`
> echo "Parent directory of \"$COMMAND\" is \"$DIR\""
> 

Why not do:

 echo "Directory name is `dirname $0`"

in sh.


Kingsley Nwosu			   ...uunet!ibmps2!aix!nwosuck
IBM AIX  Dev., Dept. 83HA/572,  |
Neighborhood Rd, Kingston,      |"Advice to those about to get married: Don't!"
NY 12401.			|	 

ramesh@ucc386.UUCP (12/01/90)

just checkin'