Mailing List Archive

Support open source code!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: symlink destination



On Wed, Oct 25, 2000 at 02:51:55PM +0900, Viktor Pavlenko wrote:
> A shell programming question:
> 
> Is there a way to find out the destination of a symbolic link (the
> file it links to) from a sh script? Parsing the output of `ls -l' is a
> possibility but it doesn't feel right.
> 
> Thanks.
> 
> Viktor

Hi Viktor,

No way that I know to do with without a bit of shell programming. Here
is an example of following more than one link to find the real name
of a script. If you are dealing with a random file argument, then
you also need to watchout for circular links...

Tom.

CMD=$0
while [[ -L "$CMD" ]] ; do
        ls=`/bin/ls -ld "$CMD"`
        link=`/usr/bin/expr "$ls" : '.*-> \(.*\)$'`
        if /usr/bin/expr "$link" : '/' > /dev/null; then
                CMD="$link"
        else
                CMD="`/usr/bin/dirname $PRG`/$link"
        fi
done

-- 
Thomas O'Dowd                   Have you had your noop today?
tom@example.com                  http://www.nooper.com


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links