为什么这两个if和else执行? [英] Why is both the if and else executed?
问题描述
我的code是:
if [ "$server" == "a" ]; then
echo "apache"
fi
if [ "$server" == "t" ]
then
echo "tomcat"
else
echo "Entered"
fi
在 $服务器
的值是 T
。我预计值的tomcat
将在控制台上打印。但进入
印呢!
The value of the $server
is t
. I expect the value tomcat
to be printed on the console. But Entered
is printed too!
我很困惑这个问题。为什么这两个如果
和其他
语句被执行?
I'm confused about this. How come both the if
and else
statements are executed?
推荐答案
如果在preceding if语句计算为false else分支应该永远只能执行。有没有真正的任何例外的规则,我所知道的。有可能是别的东西腥在造成问题的bash的文件怎么回事。
The else branch should only ever execute if the preceding if statement evaluates to false. There's not really any exceptions to that rule that I'm aware of. There's probably something else fishy going on in the bash file that's causing the issue.
确认bash的是什么是执行脚本。 #!/斌/庆典
应该是在第一行。
Make sure bash is what is executing the script. #!/bin/bash
should be on the first line.
这篇关于为什么这两个if和else执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!