On Thursday 29 May 2008 14:57, McKown, John wrote:
>I thought that I could get this from $0. But in my tests, if the script
>is on the PATH and I invoke it just with its name, I get just its name
>in return. ...

This is always a tricky problem, because various shells do it differently.
You don't seem to be running bash, because it will report the symlink's name
in $0.  What I usually do is prepend the current directory to a relative
value in $0, which can be done like this in bash:

        if [[ "$0" =~ /^\// ]]
        then    prog="$0"
        else    prog="$PWD/$0"
        fi

Actually, I'm usually trying to write scripts that run in any sh-based shell,
so I just write: prog="$(abspath "$0")" and include an abspath() function I
wrote many years ago that uses sed to convert relative paths to absolute
paths and "normalize" them by cleaning up all "/./" and "/../" path elements.

# Convert the arguments to absolute pathnames and write them to the
# standard output
abspath()
{
for p in "$@"; do echo "$p"; done | \
        sed -n -e '/^[^/]/s!\(.*\)!'$(pwd)'/\1!'\
               -e ':Loop'                      \
               -e 's!//!/!'                    \
               -e 's!/\.$!!'                   \
               -e 's!/[^/]\{1,\}/\.\./!/!'     \
               -e 's!/[^/]\{1,\}/\.\.$!!'      \
               -e 'tLoop'                      \
               -e p
}

Ugly, but it works.  I'm sure it can now be all done better within base using
the regular expression operator.
        - MacK.
-----
Edmund R. MacKenty
Software Architect
Rocket Software, Inc.
Newton, MA USA

----------------------------------------------------------------------
For LINUX-390 subscribe / signoff / archive access instructions,
send email to [EMAIL PROTECTED] with the message: INFO LINUX-390 or visit
http://www.marist.edu/htbin/wlvindex?LINUX-390

Reply via email to