Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

determining where a script is being run from

Status
Not open for further replies.

NiceButDim

Programmer
Feb 12, 2002
154
GB
I have a problem; I have a c program that must call a shell script. This script resides in more than once place. Within the script itself I need to be able to determine the directory where it is being run from.

I know that I can use $0 to see the name of the script as used when it was called. However this MAY not always be sufficient as a full pathname is not always used to call the script. Using a combination of $0 and pwd I can construct the path. However I was wondering if anyone knew of a simpler way of determing the path of a running script.

Thanks.

john.
 
I remember having seen a similar question here.
thread822-970932 might give you a few ideas.
Though it seems there isn't what you are looking for ...
 
Not necessarily simpler but this seems to work for Bourne & Korn :

DIRNAME=`dirname $0`
if [ "$DIRNAME" = "." ]; then DIRNAME=${PWD}; fi

I hope that helps.

Mike
 
For me you have to browse $PATH if $0 returns a relative path ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks a lot for the replies guys. They've helped.
hoinz; the thread that you've pointed me towards includes a bit of code along the lines that I thought I would require.
I was concerned that there would be a more straightforward way of doing what I want, that I either didn't know or had forgotten about.
I now know what's required!

Thanks again for everyones help.

john.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top