Question

The following line introduces the local variable PROGUARD_HOME within a shell script:

PROGUARD_HOME=`dirname "$0"`/..

This points to the parent folder of the shell script. The script executes normally. - Then, I created the symlink /usr/local/bin/proguard which refers to ~/bin/proguard4.10/bin/proguard.sh. When I run proguard using the symlink PROGUARD_HOME is no longer resolved correctly. This causes the following error message output by the shell script:

Error: Unable to access jarfile /usr/local/bin/../lib/proguard.jar

How can I rewrite the allocation of the enviroment variable so that it resolves an symbolic link if present? I am aware of a very similar question on resolving symbolic links in shell scripts but still cannot figure out how to combine those solutions with the parent folder approach here.

Was it helpful?

Solution

I think a readlink -f $0 to reveal the target of the shell script itself, then a dirname to strip off the script, then another readlink -f on the product of that should do the trick:

PROGUARD_HOME=$(readlink -f $(dirname $(readlink -f "$0"))/..)

A more step-by-step breakdown:

echo "\$0 is $0"
TRUENAMEOFSCRIPT=$(readlink -f $0)
echo "readlink -f of \$0 reveals $TRUENAMEOFSCRIPT"

DIRNAMEOFSCRIPT=$(dirname $TRUENAMEOFSCRIPT)
echo "The script lives in directory $DIRNAMEOFSCRIPT"

PARENTDIR=$(readlink -f "$DIRNAMEOFSCRIPT"/..)
echo "Its parent dir is $PARENTDIR"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top