Re: Soft link pathname conversion for Linux and Unix

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

 



On Sat, 2007-04-28 at 22:05 +0000, Kevin Kofler wrote:
> John Horne <john.horne <at> plymouth.ac.uk> writes:
> > Thanks for the reply, but sorry this isn't going to work. As said, this
> > must work under Unix, and Solaris, for example, uses a standard Bourne
> > shell, not bash. Constructs such as 'symval=$(' won't work. (I can't
> > change shell either before anyone asks )
> 
> No backticks either???
> 
Yes, backticks do work, and this led me to what I think is a solution.
The following script seems to do what I want, and I have given it a
quick test under Solaris 10, NetBSD, Ubuntu and FC6. (I know Solaris
doesn't have readlink, but as already suggested, the 'ls' and 'awk'
commands can get around that. On my Solaris 10 system I have readlink
installed from source, so the script works.)

I should have added that I am only dealing with files, so we don't need
to worry about directories. The perl idea was a possibility, but some
systems, my NetBSD one as far as I remember, doesn't install perl by
default.

============================================================
#! /bin/sh


full_pathname() {
        LINKNAME=$1
        TARGET=$2

        # We must first get the full pathname to the linkname directory.
        if [ -z "`echo \"${LINKNAME}\" | grep '/'`" ]; then
                # If the link name is just a filename
                LINKDIR=`pwd`
        else
                LINKDIR=`echo "${LINKNAME}" | sed -e 's:/[^/]*$::'`
                test -n "${LINKDIR}" && LINKDIR=`cd ${LINKDIR}; pwd`
        fi

        # Now test the target.
        if [ -z "`echo \"${TARGET}\" | grep '/'`" ]; then
                # If the target is just a filename do nothing
                :
        elif [ -z "`echo \"${TARGET}\" | grep '^/'`" ]; then
                # If the target doesn't begin with a '/' then prepend
the
                # linkname directory and find the full pathname of that.
                TARGETDIR=`echo "${TARGET}" | sed -e 's:/[^/]*$::'`
                TARGETDIR="${LINKDIR}/${TARGETDIR}"
                LINKDIR=`cd ${TARGETDIR}; pwd`
                TARGET=`echo "${TARGET}" | sed -e 's:^.*/\([^/]*\)$:
\1:'`
        else
                # The target begins with a '/', so extract the directory
                # and get the full pathname of that.
                LINKDIR=`echo "${TARGET}" | sed -e 's:/[^/]*$::'`
                test -n "${LINKDIR}" && LINKDIR=`cd ${LINKDIR}; pwd`
                TARGET=`echo "${TARGET}" | sed -e 's:^.*/\([^/]*\)$:
\1:'`
        fi

        echo "${LINKDIR}/${TARGET}"

        return
}


for FILE in /bin/* /usr/bin/* /sbin/* /usr/sbin/*; do
        test ! -f "${FILE}" -o ! -h "${FILE}" && continue

        XX=`readlink ${FILE}`
        FP=`full_pathname ${FILE} ${XX}`

        echo "Found link ${FILE} pointing to ${FP}"
done

exit
============================================================


Many thanks for the suggestions,

John.

-- 
---------------------------------------------------------------
John Horne, University of Plymouth, UK  Tel: +44 (0)1752 233914
E-mail: John.Horne@xxxxxxxxxxxxxx       Fax: +44 (0)1752 233839


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux