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 a script know its directory location ?

Status
Not open for further replies.

AmitVJ

Programmer
Mar 11, 2002
2
US
Hi,

I have scripts 'a' and 'b' in a directory, say, /home/myhome. Now /home/myhome is not in the $PATH, so i have to invoke the script with the full path. script 'a' calls script 'b'. I can call it as /home/myhome/b inside 'a'. But tomorrow if i move the whole setup to, say, /home/yourhome, i will have to change the script 'a'. Is their any command, system utility or code by which inside 'a' i can know what its parent directory is and then using that parent directory i can invoke script 'b' from inside 'a'.

Hope i have made the question clear. Please let me know if you need further details / clarifications.

Thanks,
Amit.
 
No, I don't think that helps him.

None of that tells him where the script is; it tells him where HE is when he invokes the script.


I think he wants "dirname $0" instead. Tony Lawrence
SCO Unix/Linux Resources tony@pcunix.com
 
create a VAR in the users .profile

a=/your/full/path/of/the/script;export a

then just use $a/scriptname to execute.

You then only need to change one thing if you move your scripts.

P.S. you can also use ./ in front of the script to execute it in it's current directory if the directory is not in the PATH

Mike

--
| Mike Nixon
| Unix Admin
| ----------------------------
 
Hi,

I guess 'pcunix' has understood my requirement more appropriately. There is one small issue though. In my initial mail i have said that the script directory will not be in the path. in that case, the user will be running the script 'a' with full path (absolute or relative) and i will get what i want by `dirname $0`. But, just in case if the script is in $PATH, then the user may invoke it by the script name alone. Here the `dirname $0` doesn't give me what i want. Is there any generalization to this so that i dont have to bother whether the script 'a' is in user's $PATH or not and whether the user executes the script by full path or just the name. I should be able to find what its location is inside the script. Is there are a way?

And yes, thank you all for taking time in answering my query.

Thanks,
Amit.
 
But If you can't find the script in the first place, how can you expect the script to know where it is? Or am I getting the wrong end of the stick? --
| Mike Nixon
| Unix Admin
| ----------------------------
 
#!/usr/bin/ksh

SCRIPT_NAME=$0
echo "SCRIPT NAME: $SCRIPT_NAME"
ABSOLUTE_LOC=`which $SCRIPT_NAME`
echo "ABSOLUTE LOC: $ABSOLUTE_LOC"

This should work if the full path is given:
/usr/bin/ls

or
if the script dir is in the path

it won't work if your user does
something like . script or ./script

Robert Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
[lightsaber]
 
This may work. Let me know.

Robert

# cat script_a
#!/usr/bin/ksh

echo "Doing some stuff..."

SCRIPT_NAME=$0
echo "SCRIPT NAME: $SCRIPT_NAME"
COMMAND=`echo $SCRIPT_NAME|awk -F"/" '{print $NF}'`

FIRST_CHAR=`echo $SCRIPT_NAME|cut -c1`
echo "FIRST CHAR: $FIRST_CHAR"

if [ "$FIRST_CHAR" = "." ]
then
SCRIPT_DIR=$PWD
else
SCRIPT_DIR=`echo $SCRIPT_NAME|awk -F "/$COMMAND" '{print $1}'`
fi

echo "Executing ${SCRIPT_DIR}/script_b..."
${SCRIPT_DIR}/script_b

# cat script_b
#!/usr/bin/ksh

echo "Doing some more stuff..."

Test 1
# ./script_a
Doing some stuff...
SCRIPT NAME: ./script_a
FIRST CHAR: .
Executing /home/u273258/scripts/script_b...
Doing some more stuff...

Test 2
# cd /tmp
root@awhq6394 [/tmp]
# /home/u273258/scripts/script_a
Doing some stuff...
SCRIPT NAME: /home/u273258/scripts/script_a
FIRST CHAR: /
Executing /home/u273258/scripts/script_b...
Doing some more stuff...
Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
[lightsaber]
 
> But, just in case if the script is in $PATH, then the user
> may invoke it by the script name alone. Here the `dirname $
> 0` doesn't give me what i want.

Huh? "dirname $0" gives you the same results whether the command is in the PATH or is invoked specifically with an absolute PATH- at least it does in shells I've used.

???? Tony Lawrence
SCO Unix/Linux Resources tony@pcunix.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top