(resent with correct recipien ts)
Hi,
this will break if the link is absolute, since it will still prepend the $(dirname "$FILE") to the value we got from readlink. So if I call /home/r/bin/basexhttp, which is a link to /home/r/bin/basex/bin/basexhttp, it will set FILE to /home/r/bin//home/r/bin/basex/bin/basexhttp. Also, I wonder why you are splitting $SOURCE into basename and dirname, just to concat it again?
Kind regards, Ralf
On Sunday 06 November 2011 21:22:57 you wrote:
Hi Ralf,
I modified your script a little, and came with the following shorter solution:
FILE="${BASH_SOURCE[0]}" while [ -h "$FILE" ] ; do SOURCE="$(readlink "$FILE")" FILE="$(dirname "$FILE")/$(dirname "$SOURCE")/$(basename "$SOURCE")" done BASEX_DIR="$( cd -P "$(dirname $FILE)" && pwd )"
Could you give it a try and tell me if you see any problems with it?
Many thanks in advance!
Dimitar
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
Kind regards, Ralf
On Friday 04 November 2011 23:15:44 Dimitar Popov wrote:
Am Freitag, 4. November 2011, 16:48:10 schrieb Ralf Jung:
there was some shell syntax to do that, but I do not remember it currently.
Again the holy StackOverflow with the help of it's helper St. Google:
http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what- directory-its-stored-in
Cheers, Dimitar