linux - Bash:为什么我的 If 语句总是评估为真?

标签 linux bash if-statement

我正在尝试编写在大多数语言中看起来很简单的 if 语句,但是在 bash 中这似乎根本不起作用。

当我运行脚本时,它总是进入第一个 if 语句。谁能帮助我解决我做错了什么?

PERC=.5
    if [  "$PERC" > "1.00" ]
            then
                    echo "Entered first statement"
            else
                    if [ "$PERC" < "1.00" ]
                            then
                                    echo "Entered second statement"
                    fi
    fi

感谢您的帮助。

最佳答案

><比较字符串,而不是数字(并且必须在单个 [...] 中用反斜杠或引号引起来)。使用 -gt , -lt等比较数字,或使用算术条件:

if (( a < b || b <= c )) ; then

但是请注意,bash 仅处理整数运算。要比较 float ,您可以使用 bc :

if [[ 1 == $( bc <<< '1.5 < 1.00' ) ]] ; then

关于linux - Bash:为什么我的 If 语句总是评估为真?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32521381/

相关文章:

bash - 预期的音频采样率与实际不匹配?

html - 在 HTML [REGEX, RUBY] 中从 &lt;!--[if gte mso 9]> 移除到 &lt;![endif]-->

Javascript条件

javascript - 如何跳出 while 循环和 if 语句?

linux - 如何向公众发布我的 DNS 服务器以便可以访问我的域名

c - 无法写入共享内存段

linux - (( '127.0.0.1' , 8080)) 已经关闭

linux - Linux 上 UDP 套接字上的 send、sendto 和 sendmsg 可能的返回值是什么?

linux - While 在 bash 脚本中执行循环和变量?

linux - 在 linux 中更改以前的命令