Operating System - HP-UX
1748040 Members
4844 Online
108757 Solutions
New Discussion юеВ

How do I script the directory in which this script is located?

 
Stuart Abramson
Trusted Contributor

How do I script the directory in which this script is located?

We have scripts that we move around from machine to machine.

They can be in:
/usr/local/bin/
/opt/local/bin/
/usr/contrib/bin

How can I script definitively where they are from within the script. You can't just go $PWD, because if that will give you where you are sitting right now. You can't go "whence" because that may turn up more than one of them. Also, the location may be some other place not in your path.

I think that you can use some combination of "basename" or "dirname" or something like that.
10 REPLIES 10
Tom Danzig
Honored Contributor

Re: How do I script the directory in which this script is located?

in script:

which $0
Tom Danzig
Honored Contributor

Re: How do I script the directory in which this script is located?

EDIT: If you only want the directory, use:

dirname `which $0`
Stuart Abramson
Trusted Contributor

Re: How do I script the directory in which this script is located?

I think that "which" only works for c shell users. It takes the path/environment from .cshrc.

Since I'm a ksh user, it doesn't work for me. It says "no xxxx in and lists my path (correctly - but it can't find xxxx which is right there in the path.)
Patrick Wallek
Honored Contributor

Re: How do I script the directory in which this script is located?

which SHOULD work for any shell in HP-UX. A better option may actually be to use whence.

# whence xxxx
Tom Danzig
Honored Contributor

Re: How do I script the directory in which this script is located?

/usr/bin/which should work for any shell. whence is actually a shell built-in and would not be available in csh.
c_51
Trusted Contributor

Re: How do I script the directory in which this script is located?

i've always used:
program="${0:##*/}"
path="${0:%/*}"

for posix type shells
c_51
Trusted Contributor

Re: How do I script the directory in which this script is located?

I guess i left out a bit,

path="${0:%/*}"
#
# find where I am, absolute path to this directory
#
oldDir=$(pwd)
cd $path
mydir=$(pwd)
cd $oldDir
Bill Hassell
Honored Contributor

Re: How do I script the directory in which this script is located?

This is going to be tricky because $0 is the name used to start the script. The basename will be the name of the script but unless the user uses a full pathname, $0 will report just what was used to start it. It might be: scriptname ./scriptname ../scriptname bin/scriptname, etc.

You'll need to test the dirname results. If it starts with /, then you have a full pathname and you know where it came from. If dirname is empty or contains relative locations, then the location can be determined with the construct:

MYNAME=${0##*/}
MYDIR=$(dirname $0)
if [ $(echo $MYDIR | cut -c 1) != / ]
then
MYFULLPATH=$(whence ./$MYDIR/$MYNAME)
else
MYFULLPATH=$MYDIR/$MYNAME
fi
echo $MYFULLPATH

In this snippet of code, we use the shell to perform basename, but use dirname rather than ${0:%/*}. The reason is that if the script is started by the simple name, the shell construct returns the basename rather than . so we use dirname to make sure it is really a valid directory reference.

The way it works is to put ./ in front of whatever dirname is found in $0. Then run the ./DIRNAME/SCRIPTNAME through whence and it returns a useable full pathname. Note that you may see an extra ./ in $MYFULLPATH but it is still a valid directory reference.

This sample has been tested with:

/tmp/myscript
myscript
./myscript
../myscript
../bin/myscript
./././myscript

and so on. It should identify the full pathname of the script regardless of how it was invoked.


Bill Hassell, sysadmin
Dietmar Konermann
Honored Contributor

Re: How do I script the directory in which this script is located?

Had that problem some time ago and just digged this from one of my scripts:

MYNAME=$(cd $(dirname $0); pwd -P)/${0##*/}

Best regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)