本文介绍了"else if"和"else if"之间的区别是什么?和"elif"猛击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下shell脚本,它们应该将一些Java .ear/.war文件简单地转移到JBoss:

I've got the following shell script that's supposed to simply stage a few Java .ear/.war files to JBoss:

SUCCESS=false
DEPLOY_PATH=/apps/jboss/server/default/deploy

E_NOARGS=75
M_USAGE="usage: $0 {rcm|hcm}"
M_MISSING_RCM="missing: rcm.war file not present"
M_MISSING_HCM="missing: hcm.ear or hcm.war file not present"

if [ -z "$1" ]
then
  echo $M_USAGE
  exit $E_NOARGS
else
  M_START="deploying $1 ..."
  M_FINISH="finished deploying $1"
fi

until [ -z "$1" ]
do
  echo $M_START
  case "$1" in
    rcm*)
      # do a hot-deploy of the rcm.war file
      # TODO: test if rcm.war file is present, error out if not
      if [ -e rcm.war ]
      then
        cp -v rcm.war $DEPLOY_PATH/rcm.war
        SUCCESS=true
      else
        echo $M_MISSING_RCM
      fi
      ;;
    hcm*)
      # do a shutdown, deploy hcm.war, and restart jboss
      ps -ef | awk '/jboss/{print $2}' | xargs kill -s KILL

      HCM_DEPLOYED=false

      if [ -e hcm.ear ]
      then
        cp -v hcm.ear $DEPLOY_PATH/hcm.ear
        HCM_DEPLOYED=true
      else
        if [ -e hcm.war ]
        then
          cp -v hcm.war $DEPLOY_PATH/hcm.war
          HCM_DEPLOYED=true
        else
          echo $M_MISSING_HCM
        fi
      fi

      if $HCM_DEPLOYED ;
      then
        # TODO: detect the hostname
        nohup /apps/jboss/bin/run.sh -b <HOSTNAME> & &> /dev/null
        SUCCESS=true
      fi
      ;;
    *)
      echo $M_USAGE
      exit 1
  esac
  shift
done

if $SUCCESS ;
then
  echo $M_FINISH
fi

特别让我困惑的部分是这个:

The section in particular that confuses me is this one:

      if [ -e hcm.ear ]
      then
        cp -v hcm.ear $DEPLOY_PATH/hcm.ear
        HCM_DEPLOYED=true
      else
        if [ -e hcm.war ]
        then
          cp -v hcm.war $DEPLOY_PATH/hcm.war
          HCM_DEPLOYED=true
        else
          echo $M_MISSING_HCM
        fi
      fi

我似乎无法获得 elif [-e hcm.war] 在远程服务器上正常工作.远程服务器在redhat上运行bash 3.2.25(如果有任何区别.)我怀疑我只是缺少一些挑剔的bash shell脚本详细信息.

I can't seem to get elif [ -e hcm.war ] to work correctly on the remote server. The remote server is running bash 3.2.25 on redhat (if that makes any difference.) I suspect I'm just missing some picky bash shell script detail.

有什么建议吗?

推荐答案

您发布的代码似乎可以正常工作.

Your code as posted seems to work.

elif .. fi else之间是有区别的;如果... fi .真正的 elif ... fi 末尾的 fi 比您的代码少

There is a difference between elif .. fi AND else ; if ... fi. A true elif ... fi will have one fewer fi at the end than your code.

您所发布的代码将询问是否存在hcm.ear,然后检查是否存在hcm.war".那是你要的吗?测试的另一个逻辑路径是如果hcm.ear不存在,然后检查是否存在hcm.war."

Your code as posted, asks, "if hcm.ear exists THEN check if there is an hcm.war". Is that what you want? The other logic path to test would be "if hcm.ear doesn't exist THEN check if there an hcm.war."

该替代逻辑路径看起来像

That alternate logic path looks like

  if [ -e hcm.ear ] ; then
    cp -v hcm.ear $DEPLOY_PATH/hcm.ear
    HCM_DEPLOYED=true
  elif [ -e hcm.war ] ; then
      cp -v hcm.war $DEPLOY_PATH/hcm.war
      HCM_DEPLOYED=true
  else
    echo $M_MISSING_HCM
  fi

我希望这会有所帮助.

这篇关于"else if"和"else if"之间的区别是什么?和"elif"猛击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 17:17