Am Freitag, 4. November 2011, 23:31:27 schrieb Ralf Jung:
Unfortunately all solutions proposed there have problems with relative symlinks, which readlink does not resolve to absolute paths... (I have a symlink in ~/bin called basexhttp pointing to "basex/bin/basexhttp", but it tries to resolve the relative path from the directory I am in, instead of ~/bin)
The shortest one I could come up with that works for me and does not use readlink -e is based on http://stackoverflow.com/questions/1055671/how-can-i- get-the-behavior-of-gnus-readlink-f-on-a-mac
# find Path to this script pushd . > /dev/null SCRIPT_FILE="${BASH_SOURCE[0]}" cd "$(dirname "$SCRIPT_FILE")" SCRIPT_FILE="$(basename "$SCRIPT_FILE")" # we need to get rid of the path part since we cd'ed # Iterate down a (possible) chain of symlinks while [ -L "$SCRIPT_FILE" ] do SCRIPT_FILE="$(readlink "$SCRIPT_FILE")" cd "$(dirname "$SCRIPT_FILE")" SCRIPT_FILE="$(basename "$SCRIPT_FILE")" done # now we are in the directory we are looking for SCRIPT_DIR="$(pwd -P)" popd > /dev/null
What's wrong with simply using
SOURCE="${BASH_SOURCE[0]}" while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done PWD="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
? It works on my computer, but I'm not Bash profi, so I might be missing something...
Greetings, Dimitar