bash 中的“else if”和“elif”有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7126752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between "else if" and "elif" in bash?
提问by emptyset
I've got the following shell script that's supposed to simply stage a few Java .ear/.war files to JBoss:
我有以下 shell 脚本,它应该只是将一些 Java .ear/.war 文件暂存到 JBoss:
SUCCESS=false
DEPLOY_PATH=/apps/jboss/server/default/deploy
E_NOARGS=75
M_USAGE="usage: 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
{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 "" ]
then
echo $M_USAGE
exit $E_NOARGS
else
M_START="deploying ..."
M_FINISH="finished deploying "
fi
until [ -z "" ]
do
echo $M_START
case "" 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 }' | 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
elif [ -e hcm.war ] ; then
cp -v hcm.war $DEPLOY_PATH/hcm.war
HCM_DEPLOYED=true
else
echo $M_MISSING_HCM
fi
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.
我似乎无法elif [ -e hcm.war ]
在远程服务器上正常工作。远程服务器在 redhat 上运行 bash 3.2.25(如果这有什么不同。)我怀疑我只是缺少一些挑剔的 bash shell 脚本细节。
Any suggestions?
有什么建议?
回答by shellter
Your code as posted seems to work.
您发布的代码似乎有效。
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.
elif .. fi
AND之间存在差异else ; if ... fi
。真正的末尾将比您的代码elif ... fi
少一个fi
。
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."
您发布的代码询问,“如果 hcm.ear 存在,则检查是否存在 hcm.war”。那是你要的吗?要测试的另一个逻辑路径是“如果 hcm.ear 不存在,则检查是否存在 hcm.war”。
That alternate logic path looks like
那个替代逻辑路径看起来像
HCM_DEPLOYED=true
cp -v hcm.ear "${DEPLOY_PATH}/" || cp -v hcm.war "${DEPLOY_PATH}/" || HCM_DEPLOYED=false
if [ ! ${HCM_DEPLOYED} ]; then
echo "${M_MISSING_HCM}"
else
# TODO: detect the hostname
...
I hope this helps.
我希望这有帮助。
回答by Paul Hargreaves
This isn't a direct answer to the question (elif vs else) but I would refactor like so:
这不是问题的直接答案(elif vs else),但我会像这样重构:
##代码##I.e. always try the copies since you always want to do them, if one fails try the next, etc.
即总是尝试副本,因为您总是想这样做,如果一个失败尝试下一个,等等。
As an aside, you always want to wrap paths and strings in quotes. Otherwise a path containing spaces will cause your app to break.
顺便说一句,您总是希望将路径和字符串用引号括起来。否则包含空格的路径将导致您的应用程序中断。