Computing.Net > Forums > Unix > Determine a script's execution dir?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Determine a script's execution dir?

Reply to Message Icon

Name: tshea
Date: February 12, 2005 at 07:37:53 Pacific
OS: HP-UX 11
CPU/Ram: PA-RISC / 1GB
Comment:

I have a script that has to record it's actions in a log file. I'd like to ensure that it always writes to a log file that is in the same directory as the script.

I tried to establish the name of the log using the dirname & basename cmds:
LOGFILE=`dirname $0`/`basename $0 .sh`.log
This works as long as the script is run using an absolute path. If I run /scripts/test.sh , my LOGFILE variable is set to /scripts/test.log.

If the script is run using a relative path, cd /scripts
test.sh
the LOGFILE variable gets set to ./test.log. The script changes directories during execution resulting in it writing to test.log in whatever the current working directory is.

I think I need to find a replacement for the dirname command that always returns an absolute path. I have tried a which test.sh command but that also can return a relative path name.



Sponsored Link
Ads by Google

Response Number 1
Name: vgersh99
Date: February 12, 2005 at 09:08:26 Pacific
Reply:

if you're using 'ksh', you can do 'whence $0' at the beginning of the script to give you the path to the script itself. From there you can derive the path AND the script name.


vlad
#include<disclaimer.h>


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Determine a script's execution dir?

line too long in a script www.computing.net/answers/unix/line-too-long-in-a-script/4015.html

executing a script while rlogin www.computing.net/answers/unix/executing-a-script-while-rlogin/7052.html

Using ksh when executing a script www.computing.net/answers/unix/using-ksh-when-executing-a-script/4529.html