为什么这两个if和else执行? [英] Why is both the if and else executed?

查看:154
本文介绍了为什么这两个if和else执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆