shell脚本参数

使用-z或-n对一个变量判空时, 若直接使用[ -n ARG ]这种形式,当{ARG}中有空格将会报错,

line 27: [: sd: binary operator expected

需要使用

[[ -n ${ARG} ]] 或 [ -n "${ARG}" ]
完整脚本
# Please define the absolute path of tomcat instance
JAVA_HOME=/opt/jdk/latest
BASE_DIR=/home/tomcat
JAVA_OPTS="-server -Xms512m -Xmx512m -XX:MaxNewSize=256m -Djava.awt.headless=true"
EXTRA_OPTS=""
TC_ERROR=0


start() {
  echo -n "[$(date +%Y%m%d-%H%M%S)] Start: "

  pid=`ps -ef|grep $1|grep -v 'grep'|grep 'java'|awk '{print $2}'`
  if [ -z ${pid} ] ; then
    if [ ! -e ${JAVA_HOME}/bin/java ]; then
      echo ${JAVA_HOME} ' is not a valid JAVA_HOME path'
      exit 2
    fi
    echo JAVA_HOME:${JAVA_HOME}, JAR:$1, port:$2
    nohup ${JAVA_HOME}/bin/java ${JAVA_OPTS} -jar $1 --server.port=$2 ${EXTRA_OPTS} >/dev/null 2>&1 &
  else
    echo $1' is running at pid: '${pid}
  fi
}


stop() {
  echo -n "[$(date +%Y%m%d-%H%M%S)] Stop: "
  pid=`ps -ef|grep $1|grep -v 'grep'|grep 'java'|awk '{print $2}'`
  if [ -z $pid ] ; then
    echo $1' is not running'
  else
    echo $1' is running at pid:'${pid}
    kill $pid
    sleep 5
  fi

  pid=`ps -ef|grep $1|grep -v 'grep'|grep 'java'|awk '{print $2}'`
  if [ -z $pid ] ; then
    echo 'Done'
  else
    echo 'Still running, now killing with pid:'${pid}
    ps -ef|grep $1|grep -v 'grep'|grep 'java'|awk '{print $2}'|xargs kill -s 9
  fi
}


restart() {
  stop $1
  start $1 $2
}

deploy() {
  # -f filename - Check for regular file existence not a directory
  [ -f ${BUILD_DIR}/$JAR_FILE ] || exit 1

  stop $1
  df -h
  echo ''

  echo -n "[$(date +%Y%m%d-%H%M%S)] Deploy: "
  if [ -e $1 ]; then
    echo -n "remove $1... "
    rm $1
  fi

  echo -n "copy... "
  cp -f ${BUILD_DIR}/$JAR_FILE ${RUN_DIR}/$JAR_FILE
  echo 'done'

  start $1 $2
}

help_func() {
  echo "Usage:"
  echo "   $0 [-d BASE_DIR] [-j JAVA_HOME] [-f JAR_FILE] [-p PORT] [-m ACT_MODE] [-v VM_OPTION]"
  echo "Description:"
  echo "-d Path of the workspace, optional, default: $BASE_DIR"
  echo "-f The jar file name, e.g. my-project.jar"
  echo "-j Path to JDK home, optional, default: $JAVA_HOME"
  echo "-m Action mode, e.g. start, restart, stop, etc."
  echo "-p Service port, e.g. 8080"
  echo "-e extra jar arguments, e.g. --asp.root_path=/path"
  echo "-v JAVA_OPTS, optional, default: $JAVA_OPTS"
  echo ""
  echo "Examples:"
  echo "   $0 -f asp-commons.jar -p 8761 -m restart"
  exit 0
}

# 如果字符后面没有冒号, 说明该字符的选项不需要值, 如 -i
# 如果字符后面跟着一个冒号, 表明该字符的选项需要一个值, 其参数和值之间需要空格分隔, 如 -j aaa.jar, 冒号和问号不能被用作选项字符
# getopts每次被调用时, 会将下一个选项字符放置到变量中, 通过OPTARG可以取到参数值
# 如果option前面加冒号, 代表忽略错误
while getopts 'd:e:j:f:m:p:v:h' OPT; do
  case $OPT in
    d) BASE_DIR="$OPTARG";;
    f) JAR_FILE="$OPTARG";;
    j) JAVA_HOME="$OPTARG";;
    m) ACT_MODE="$OPTARG";;
    p) PORT="$OPTARG";;
    v) JAVA_OPTS="$OPTARG";;
    e) EXTRA_OPTS="$OPTARG";;
    h) help_func $0;;
    ?) help_func $0;;
    esac
done

shift $(($OPTIND - 1))

# Print helpFunction in case parameters are empty

if [ -z "$ACT_MODE" ]
then
   echo "Parameter -m is missing";
   TC_ERROR=1
fi

if [ -z "$JAR_FILE" ]
then
   echo "Parameter -f is missing";
   TC_ERROR=1
fi

if [ $TC_ERROR -gt 0 ]
then
   help_func
fi

RUN_DIR=${BASE_DIR}/run
BUILD_DIR=${BASE_DIR}/builds
ARCHIVE_DIR=${BASE_DIR}/archives
TMP_DIR=${BASE_DIR}/tmp

# -e Check for file existence regardless of type (node, directory, socket)
#if [ ! -e ${RUN_DIR}/${JAR_FILE} ]; then
#  echo ${RUN_DIR}/${JAR_FILE} 'does not exist'
#  exit 2
#fi

case "$ACT_MODE" in
  start)
      # -e Check for file existence regardless of type (node, directory, socket)
      if [ ! -e ${RUN_DIR}/${JAR_FILE} ]; then
        echo ${RUN_DIR}/${JAR_FILE} 'does not exist'
        exit 2
      fi
      if [ -z "$PORT" ]
      then
         echo "Parameter -p is missing";
         exit 2
      fi
      start ${RUN_DIR}/${JAR_FILE} $PORT
      ;;
  stop)
      stop ${RUN_DIR}/${JAR_FILE}
      ;;
  restart)
      restart ${RUN_DIR}/${JAR_FILE} $PORT
      ;;
  deploy)
      deploy ${RUN_DIR}/${JAR_FILE} $PORT
      ;;
  *)
      echo "Unknown action mode"
      help_func
esac
使用方式

指定jvm参数

./scripts/tc2.sh -f ap-program.jar -m deploy -p 8082 -v "-server -Xms512m -Xmx512m -XX:MaxNewSize=256m -Djava.awt.headless=true -Dspringfox.documentation.enabled=true"

指定jar的启动参数, 对于参数值为空的参数, 两个引号需要转义一下

./scripts/tc2.sh -f ap-website2.jar -m restart -p 8090 -e "--ap.root_path=\"\" --ap.resource_path=/static --server.servlet.context-path=/"