One issue that I always come across when creating a shell script is referencing files dynamically based on the location of the shell script. A hack I often used was to pass the script’s path to the script on execution because the readlink command was not available to me. However recently I have revisited this issue and have found a solution for obtaining the absolute path of the executing shell script without readlink. For your convenience both scripts are found below.

Absolute Path using readlink

#!/bin/sh
PATH= $(dirname $(readlink -f $0))
echo ${PATH}

Absolute Path without using readlink

#!/bin/sh
ORIGINAL_PATH=`pwd`
cd "`dirname ${0}`"
 
PATH=`pwd`
cd "${ORIGINAL_PATH}"
 
echo ${PATH}
, , , , ,

One of the main maintenance issues with running a Java application from a shell script is keeping your classpath up-to-date with the new and updated jars. To make things more difficult you can’t specify a folder as the classpath as it doesn’t include the jars in it only .class files. However through the use of a basic for loop we are able to dynamically generate the classpath.

CP=
for i in `ls ./lib/*.jar`
do
  CP=${CP}:${i}
done
, , , , , ,