Operating System - HP-UX
1830802 Members
4792 Online
110016 Solutions
New Discussion

Obtaining "real" directory from a symbolic link

 
Danny Fang
Frequent Advisor

Obtaining "real" directory from a symbolic link

Hi,

I'm attempting to obtain the path of the script that I'm running with the script below. However,if the path obtained is only a symbolic link, then I'm unable to obtain the "real" path using my script below.

May I know how should I modify my script in order to obtain the "pointed to" or "real" absolute path from the sym-link?

#!/bin/ksh
srcDir=`dirname $0`
if [ "$srcDir" = "." ] ; then
srcDir=`pwd`
fi

e.g:
Sym-link:
/home/cs/test -> /usr/sbin/realScript.ksh

My script returns:
/home/cs/test

Could anyone help me out?

Thanks in advance.

2 REPLIES 2
Alex Lavrov.
Honored Contributor

Re: Obtaining "real" directory from a symbolic link

use:

pwd -P

for Korn shell

(http://osr600doc.sco.com/en/FD_files/nav_symlinks.html)
I don't give a damn for a man that can only spell a word one way. (M. Twain)
Muthukumar_5
Honored Contributor

Re: Obtaining "real" directory from a symbolic link

You can try with file -h also as,

#!/bin/ksh
file -h $0 | grep -q link
if [[ $? -eq 0 ]]
then
file=$(file -h $0 | awk '{ print $NF}');
srcDir=`dirname $file`
else
srcDir=`dirname $0`
if [ "$srcDir" = "." ] ; then
srcDir=`pwd`
fi
fi
echo $srcDir;
exit 0
## END ##

hth.
Easy to suggest when don't know about the problem!