views:

55

answers:

3

Hello, I have some script, that uses files in directories around it. It uses

dirname $0

command. And it should work from any directory from where I will run this script, but when I run symbolic link that points to that script, I get the path of symbolic link like an output of dirname rather than the path of the script itself.
Any one know way to get path of the script?

Thank you for ahead.

+1  A: 

Get the real path to your script

if [ -L $0 ] ; then
    ME=$(readlink $0)
else
    ME=$0
fi
DIR=$(dirname $ME)
jmz
Yes, it greatly works!
rodnower
A: 

if you have realpath installed:

$(dirname $(realpath $0))
knittl
A: 

Unless I misunderstand you, the problem should be the same as the one in: http://stackoverflow.com/questions/284662/how-do-you-normalize-a-file-path-in-bash

An option not mentioned there is the following python one-liner:

python2.6 -c "import os,sys; print os.path.realpath(sys.argv[1])" "$0"

Finally, remember to use double quotes around "$0".

loevborg
The answer of jmz is what I need. I little afraid to use you suggestion, because the version of Python at the end of the command name may be different from server to server, and script will less portable.
rodnower
Oh that was a typo. Any recent version of python (starting at 2.2) should do, so you can just use "python" instead.
loevborg