r6474 - in trunk/BOOK: . general/prog introduction/welcome

Dan Nicholson dbn.lists at gmail.com
Sun Jan 28 09:46:23 PST 2007


On 1/27/07, dj at linuxfromscratch.org <dj at linuxfromscratch.org> wrote:
>  # Adjust PATH
>  pathappend ${JAVA_HOME}/bin PATH
>
> -# Auto Java Classpath Updating
> -# Create symlinks to this directory for auto classpath setting
> +# Auto Java CLASSPATH
> +# Copy jar files to, or create symlinks in this directory
>  AUTO_CLASSPATH_DIR=/usr/lib/classpath
> -if [ -z ${CLASSPATH} ]; then
> +# Check for jar files in AUTO_CLASSPATH_DIR (if empty, don't include)
> +if [ `ls ${AUTO_CLASSPATH_DIR}/*.jar &> /dev/null` ]; then
>    CLASSPATH=.:${AUTO_CLASSPATH_DIR}
>  else
> -  CLASSPATH="${CLASSPATH}:.:${AUTO_CLASSPATH_DIR}"
> +  # Always have the current directory in the CLASSPATH
> +  CLASSPATH=.
>  fi

I don't think this is doing the right thing. In the else, you set
CLASSPATH=., but you also removed the test for empty or null CLASSPATH
(-z). So, potentially, this is nuking any existing settings. It could
be improved by making two tests in the if statement (putting the -z
back in). Really, it's two different tests, though.

You could leverage pathappend to make these things easier.

pathappend . CLASSPATH
if you have jars in $AUTO_CLASSPATH_DIR; then
    pathappend $AUTO_CLASSPATH_DIR CLASSPATH
fi

Another thing ( a nitpick ). The if [ `ls ...` ] is kind of overkill.
You're executing a command and then checking whether the string result
is empty or not. It would be better if you just let `if' decide on the
return status of `ls'. This is how I'd write the test (if you're
curious):

if ls ${AUTO_CLASSPATH_DIR}/*.jar >/dev/null 2>&1; then

> -# Check for empty AUTO_CLASSPATH_DIR
> -ls ${AUTO_CLASSPATH_DIR}/*.jar &> /dev/null &&
> -for i in ${AUTO_CLASSPATH_DIR}/*.jar
> +# Add subdirectories
> +ls -d ${AUTO_CLASSPATH_DIR}/*/ &> /dev/null &&
> +for i in `ls -d ${AUTO_CLASSPATH_DIR}/*/`
>    do CLASSPATH=${CLASSPATH}:"${i}"
>  done
> +
>  export CLASSPATH

Actually, the whole AUTO_CLASSPATHing stuff can be reduced to one loop.

for dir in `find ${AUTO_CLASSPATH_DIR} -type d 2>/dev/null`; do
    pathappend $dir CLASSPATH
done

${AUTO_CLASSPATH_DIR} will always come first in the `find', too. It
would be nice if we'd unset used variables when we're done, too.

unset AUTO_CLASSPATH_DIR i

--
Dan



More information about the blfs-book mailing list