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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I obtain and change to wd of running script

Status
Not open for further replies.

MCubitt

Programmer
Mar 14, 2002
1,081
GB
Is there a way for a script to identify it's location?

Eg
If I am in /home/oracle
and do
/home/oracle/scripts/test.unix

I would want the script to recognise /home/oracle/scripts/ as the new wd, not my wd of /home/oracle

Is this possible, please?




Applications Support
UK
 
Have you tried dirname $0 ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
It filled the value with "dirname" !
I am using bash...



Applications Support
UK
 
echo $(dirname $0)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
echo "$(dirname $(whence $0) )"

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks all, it was resolved by:
ScriptDirectory=`dirname $0`

So this worked:
Code:
# Get path of script
ScriptName=`basename $0`
CurrentDirectory=`pwd`
ScriptDirectory=`dirname $0`
case "${ScriptDirectory}" in /*) ;;
  .) ScriptDirectory="$CurrentDirectory" ;;
  *) ScriptDirectory="$CurrentDirectory"/"$ScriptDirectory" ;;
esac






Applications Support
UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top