Repository: karaf
Updated Branches:
  refs/heads/master 6b9846657 -> 64eb20d80


Fix problems in Karaf start script

This commit fixes most of the problems pointed out by shellcheck [1] in
the karaf start script (bin/karaf). These include:

 - Properly quote variables to prevent spaces from breaking the script
 - Ensure that KARAF_HOME is set for commands which would otherwise try
   to delete parts of the root file system. E.g:
       rm -rf "$KARAF_HOME/lib"
 - Use only $(..) instead of both $(...) and `...`
 - Require bash since not all commands are posix compliant anyway.

[1] http://shellcheck.net

Signed-off-by: Lars Kiesow <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/db934e7c
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/db934e7c
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/db934e7c

Branch: refs/heads/master
Commit: db934e7c42f3e10ce7e91f597bbd95ca03b9adef
Parents: 47e207c
Author: Lars Kiesow <[email protected]>
Authored: Wed Mar 2 16:34:07 2016 +0100
Committer: Lars Kiesow <[email protected]>
Committed: Wed Mar 30 16:46:26 2016 +0200

----------------------------------------------------------------------
 .../base/src/main/resources/resources/bin/karaf | 124 ++++++++++---------
 1 file changed, 63 insertions(+), 61 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/db934e7c/assemblies/features/base/src/main/resources/resources/bin/karaf
----------------------------------------------------------------------
diff --git a/assemblies/features/base/src/main/resources/resources/bin/karaf 
b/assemblies/features/base/src/main/resources/resources/bin/karaf
index 10f6f9c..ed71e66 100755
--- a/assemblies/features/base/src/main/resources/resources/bin/karaf
+++ b/assemblies/features/base/src/main/resources/resources/bin/karaf
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 #
 #    Licensed to the Apache Software Foundation (ASF) under one or more
 #    contributor license agreements.  See the NOTICE file distributed with
@@ -16,8 +16,8 @@
 #    limitations under the License.
 #
 
-DIRNAME=`dirname "$0"`
-PROGNAME=`basename "$0"`
+DIRNAME=$(dirname "$0")
+PROGNAME=$(basename "$0")
 
 #
 # Sourcing environment settings for karaf similar to tomcats setenv
@@ -62,7 +62,7 @@ detectOS() {
     darwin=false;
     aix=false;
     os400=false;
-    case "`uname`" in
+    case "$(uname)" in
         CYGWIN*)
             cygwin=true
             ;;
@@ -92,7 +92,7 @@ unlimitFD() {
     # Increase the maximum file descriptors if we can
     if [ "$os400" = "false" ] && [ "$cygwin" = "false" ]; then
         if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ]; then
-            MAX_FD_LIMIT=`ulimit -H -n`
+            MAX_FD_LIMIT=$(ulimit -H -n)
             if [ $? -eq 0 ]; then
                 # use the system max
                 MAX_FD="$MAX_FD_LIMIT"
@@ -101,7 +101,7 @@ unlimitFD() {
             fi
         fi
         if [ "$MAX_FD" != 'unlimited' ]; then
-            ulimit -n $MAX_FD > /dev/null
+            ulimit -n "$MAX_FD" > /dev/null
             if [ $? -ne 0 ]; then
                 warn "Could not set maximum file descriptor limit: $MAX_FD"
             fi
@@ -111,10 +111,10 @@ unlimitFD() {
 
 locateHome() {
     if [ "x$KARAF_HOME" = "x" ]; then
-      # In POSIX shells, CDPATH may cause cd to write to stdout
-      (unset CDPATH) >/dev/null 2>&1 && unset CDPATH
-      # KARAF_HOME is not provided, fall back to default
-      KARAF_HOME=`cd "$DIRNAME/.."; pwd`
+        # In POSIX shells, CDPATH may cause cd to write to stdout
+        (unset CDPATH) >/dev/null 2>&1 && unset CDPATH
+        # KARAF_HOME is not provided, fall back to default
+        KARAF_HOME=$(cd "$DIRNAME/.."; pwd)
     fi
 
     if [ ! -d "$KARAF_HOME" ]; then
@@ -158,7 +158,7 @@ setupNativePath() {
 
     # For Cygwin, set PATH from LD_LIBRARY_PATH
     if $cygwin; then
-        LD_LIBRARY_PATH=`cygpath --path --windows "$LD_LIBRARY_PATH"`
+        LD_LIBRARY_PATH=$(cygpath --path --windows "$LD_LIBRARY_PATH")
         PATH="$PATH;$LD_LIBRARY_PATH"
         export PATH
     fi
@@ -168,18 +168,18 @@ setupNativePath() {
 pathCanonical() {
     local_dst="${1}"
     while [ -h "${local_dst}" ] ; do
-        ls=`ls -ld "${local_dst}"`
-        link=`expr "$ls" : '.*-> \(.*\)$'`
+        ls=$(ls -ld "${local_dst}")
+        link=$(expr "$ls" : '.*-> \(.*\)$')
         if expr "$link" : '/.*' > /dev/null; then
             local_dst="$link"
         else
-            local_dst="`dirname "${local_dst}"`/$link"
+            local_dst="$(dirname "${local_dst}")/$link"
         fi
     done
-    local_bas=`basename "${local_dst}"`
-    local_dir=`dirname "${local_dst}"`
+    local_bas=$(basename "${local_dst}")
+    local_dir=$(dirname "${local_dst}")
     if [ "$local_bas" != "$local_dir" ]; then
-      local_dst="`pathCanonical "$local_dir"`/$local_bas"
+        local_dst="$(pathCanonical "$local_dir")/$local_bas"
     fi
     echo "${local_dst}" | sed -e 's#//#/#g' -e 's#/./#/#g' -e 
's#/[^/]*/../#/#g'
 }
@@ -187,15 +187,15 @@ pathCanonical() {
 locateJava() {
     # Setup the Java Virtual Machine
     if $cygwin ; then
-        [ -n "$JAVA" ] && JAVA=`cygpath --unix "$JAVA"`
-        [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
+        [ -n "$JAVA" ] && JAVA=$(cygpath --unix "$JAVA")
+        [ -n "$JAVA_HOME" ] && JAVA_HOME=$(cygpath --unix "$JAVA_HOME")
     fi
 
     if [ "x$JAVA_HOME" = "x" ] && [ "$darwin" = "true" ]; then
         JAVA_HOME="$(/usr/libexec/java_home -v 1.8)"
     fi
     if [ "x$JAVA" = "x" ] && [ -r /etc/gentoo-release ] ; then
-        JAVA_HOME=`java-config --jre-home`
+        JAVA_HOME=$(java-config --jre-home)
     fi
     if [ "x$JAVA" = "x" ]; then
         if [ "x$JAVA_HOME" != "x" ]; then
@@ -205,41 +205,41 @@ locateJava() {
             JAVA="$JAVA_HOME/bin/java"
         else
             warn "JAVA_HOME not set; results may vary"
-            JAVA=`type java`
-            JAVA=`expr "$JAVA" : '.* \(/.*\)$'`
+            JAVA=$(type java)
+            JAVA=$(expr "$JAVA" : '.* \(/.*\)$')
             if [ "x$JAVA" = "x" ]; then
                 die "java command not found"
             fi
         fi
     fi
     if [ "x$JAVA_HOME" = "x" ]; then
-        JAVA_HOME="$(dirname $(dirname $(pathCanonical "$JAVA")))"
+        JAVA_HOME="$(dirname "$(dirname "$(pathCanonical "$JAVA")")")"
     fi
 }
 
 detectJVM() {
-   #echo "`$JAVA -version`"
-   # This service should call `java -version`,
-   # read stdout, and look for hints
-   if $JAVA -version 2>&1 | grep "^IBM" ; then
-       JVM_VENDOR="IBM"
-   # on OS/400, java -version does not contain IBM explicitly
-   elif $os400; then
-       JVM_VENDOR="IBM"
-   else
-       JVM_VENDOR="SUN"
-   fi
-   # echo "JVM vendor is $JVM_VENDOR"
+    #echo "$($JAVA -version)"
+    # This service should call $(java -version),
+    # read stdout, and look for hints
+    if $JAVA -version 2>&1 | grep "^IBM" ; then
+        JVM_VENDOR="IBM"
+    # on OS/400, java -version does not contain IBM explicitly
+    elif $os400; then
+        JVM_VENDOR="IBM"
+    else
+        JVM_VENDOR="SUN"
+    fi
+    # echo "JVM vendor is $JVM_VENDOR"
 }
 
 checkJvmVersion() {
-   # echo "`$JAVA -version`"
-   VERSION=`$JAVA -version 2>&1 | egrep '"([0-9].[0-9]\..*[0-9]).*"' | awk 
'{print substr($3,2,length($3)-2)}' | awk '{print substr($1, 3, 3)}' | sed -e 
's;\.;;g'`
-   # echo $VERSION
-   if [ "$VERSION" -lt "60" ]; then
-       echo "JVM must be greater than 1.6"
-       exit 1;
-   fi
+    # echo "$($JAVA -version)"
+    VERSION=$($JAVA -version 2>&1 | egrep '"([0-9].[0-9]\..*[0-9]).*"' | awk 
'{print substr($3,2,length($3)-2)}' | awk '{print substr($1, 3, 3)}' | sed -e 
's;\.;;g')
+    # echo $VERSION
+    if [ "$VERSION" -lt "60" ]; then
+        echo "JVM must be greater than 1.6"
+        exit 1;
+    fi
 }
 
 setupDebugOptions() {
@@ -280,7 +280,7 @@ setupDefaults() {
     #Set the JVM_VENDOR specific JVM flags
     if [ "$JVM_VENDOR" = "SUN" ]; then
         # permgen was removed in Java 8
-        VERSION=`$JAVA -version 2>&1 | egrep '"([0-9].[0-9]\..*[0-9]).*"' | 
awk '{print substr($3,2,length($3)-2)}' | awk '{print substr($1, 3, 3)}' | sed 
-e 's;\.;;g'`
+        VERSION=$($JAVA -version 2>&1 | egrep '"([0-9].[0-9]\..*[0-9]).*"' | 
awk '{print substr($3,2,length($3)-2)}' | awk '{print substr($1, 3, 3)}' | sed 
-e 's;\.;;g')
         if [ "$VERSION" -lt "80" ]; then
             # Check some easily accessible MIN/MAX params for JVM mem usage
             if [ "x$JAVA_PERM_MEM" != "x" ]; then
@@ -326,11 +326,13 @@ setupDefaults() {
 
 checkRootInstance() {
    ROOT_INSTANCE_RUNNING=false
-   if [ -f "${KARAF_HOME}/instances/instance.properties" ]; then
-      ROOT_INSTANCE_PID=`sed -n -e '/item.0.pid/ s/.*\= *//p' 
"${KARAF_HOME}/instances/instance.properties"`
-      ROOT_INSTANCE_NAME=`sed -n -e '/item.0.name/ s/.*\= *//p' 
"${KARAF_HOME}/instances/instance.properties"`
+   if [ -f "${KARAF_HOME}/instances/instance.properties" ];
+   then
+      ROOT_INSTANCE_PID=$(sed -n -e '/item.0.pid/ s/.*\= *//p' 
"${KARAF_HOME}/instances/instance.properties")
+      ROOT_INSTANCE_NAME=$(sed -n -e '/item.0.name/ s/.*\= *//p' 
"${KARAF_HOME}/instances/instance.properties")
       if [ "$ROOT_INSTANCE_PID" -ne "0" ]; then
-          if [ `ps -p "$ROOT_INSTANCE_PID" 2> /dev/null | grep -c 
"$ROOT_INSTANCE_PID" 2> /dev/null` -eq '1' ]; then
+          if ps p "$ROOT_INSTANCE_PID" > /dev/null
+          then
               ROOT_INSTANCE_RUNNING=true
           fi
       fi
@@ -364,7 +366,7 @@ init() {
 
     # Determine the JVM vendor
     detectJVM
-    
+
     # Determine the JVM version >= 1.6
     checkJvmVersion
 
@@ -386,7 +388,7 @@ run() {
     while [ "$1" != "" ]; do
         case $1 in
             'clean')
-                rm -Rf "$KARAF_DATA"/{.[^.],.??*,*}
+                rm -rf "${KARAF_DATA:?}"/{.[^.],.??*,*}
                 shift
                 ;;
             'debug')
@@ -438,16 +440,16 @@ run() {
     
JAVA_ENDORSED_DIRS="${JAVA_HOME}/jre/lib/endorsed:${JAVA_HOME}/lib/endorsed:${KARAF_HOME}/lib/endorsed"
     
JAVA_EXT_DIRS="${JAVA_HOME}/jre/lib/ext:${JAVA_HOME}/lib/ext:${KARAF_HOME}/lib/ext"
     if $cygwin; then
-        KARAF_HOME=`cygpath --path --windows "$KARAF_HOME"`
-        KARAF_BASE=`cygpath --path --windows "$KARAF_BASE"`
-        KARAF_DATA=`cygpath --path --windows "$KARAF_DATA"`
-        KARAF_ETC=`cygpath --path --windows "$KARAF_ETC"`
+        KARAF_HOME=$(cygpath --path --windows "$KARAF_HOME")
+        KARAF_BASE=$(cygpath --path --windows "$KARAF_BASE")
+        KARAF_DATA=$(cygpath --path --windows "$KARAF_DATA")
+        KARAF_ETC=$(cygpath --path --windows "$KARAF_ETC")
         if [ ! -z "$CLASSPATH" ]; then
-            CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
+            CLASSPATH=$(cygpath --path --windows "$CLASSPATH")
         fi
-        JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
-        JAVA_ENDORSED_DIRS=`cygpath --path --windows "$JAVA_ENDORSED_DIRS"`
-        JAVA_EXT_DIRS=`cygpath --path --windows "$JAVA_EXT_DIRS"`
+        JAVA_HOME=$(cygpath --path --windows "$JAVA_HOME")
+        JAVA_ENDORSED_DIRS=$(cygpath --path --windows "$JAVA_ENDORSED_DIRS")
+        JAVA_EXT_DIRS=$(cygpath --path --windows "$JAVA_EXT_DIRS")
     fi
     cd "$KARAF_BASE"
 
@@ -458,13 +460,13 @@ run() {
     while true; do
         # When users want to update the lib version of, they just need to 
create
         # a lib.next directory and on the new restart, it will replace the 
current lib directory.
-        if [ -d "${KARAF_HOME}/lib.next" ] ; then
+        if [ -d "${KARAF_HOME:?}/lib.next" ] ; then
             echo "Updating libs..."
-            rm -rf "${KARAF_HOME}/lib"
-            mv -f "${KARAF_HOME}/lib.next" "${KARAF_HOME}/lib"
+            rm -rf "${KARAF_HOME:?}/lib"
+            mv -f "${KARAF_HOME:?}/lib.next" "${KARAF_HOME}/lib"
         fi
 
-        # Ensure the log directory exists 
+        # Ensure the log directory exists
         # We may need to have a place to redirect stdout/stderr
         if [ ! -d "$KARAF_DATA/log" ]; then
             mkdir -p "$KARAF_DATA/log"

Reply via email to