Deal with multi-level of links to a shell script 

*Tags*: jbuilder

Symptom 

can't put a symlink in /opt/bin for jbuilder.

Reason 

jbuilder is a simple shell script. It does not handle the case that it could be multi-level linked.

Solution 

Handle the case that there might be several links before it reach to the finial shell script.

$ diff -wU 1 jbuilder.org jbuilder > /tmp/mll.jb
@@ -6,3 +6,15 @@

-SDIR=`dirname $0`
+PRG=$0
+# Resolve symlinks
+while [ -L "$PRG" ]; do
+    ls=`ls -ld "$PRG"`
+    link=`expr "$ls" : '.*-> \(.*\)$'`
+    if expr "$link" : '/' > /dev/null; then
+    PRG="$link"
+    else
+    PRG="`dirname $PRG`/$link"
+    fi
+done
+
+SDIR=`dirname $PRG`
 cd $SDIR
 SDIR=`pwd`

documented on: 2000.11.13 Mon 21:57:37