Bash -eq和==,区别是什么? [英] Bash -eq and ==, what's the diff?

查看:60
本文介绍了Bash -eq和==,区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么有效:

Output=$( tail --lines=1 $fileDiProva )
##[INFO]Output = "OK"

if [[ $Output == $OK ]]; then
    echo "OK"
else
    echo "No Match"
fi

这不是吗?

Output=$( tail --lines=1 $fileDiProva )
##[INFO]Output = "OK"

if [[ $Output -eq $OK ]]; then
    echo "OK"
else
    echo "No Match"
fi

有什么区别??在==和-eq之间?

What's the difference?? between == and -eq?

谢谢!

推荐答案

-eq 是算术测试.

您正在比较字符串.

来自帮助测试:

Other operators:

  arg1 OP arg2   Arithmetic tests.  OP is one of -eq, -ne,
                 -lt, -le, -gt, or -ge.

当您使用 [[[]并使用 -eq 作为运算符时,shell会尝试评估LHS和RHS.以下示例将对其进行解释:

When you use [[ and use -eq as the operator, the shell attempts to evaluate the LHS and RHS. The following example would explain it:

$ foo=something
+ foo=something
$ bar=other
+ bar=other
$ [[ $foo -eq $bar ]] && echo y
+ [[ something -eq other ]]
+ echo y
y
$ something=42
+ something=42
$ [[ $foo -eq $bar ]] && echo y
+ [[ something -eq other ]]
$ other=42
+ other=42
$ [[ $foo -eq $bar ]] && echo y
+ [[ something -eq other ]]
+ echo y
y

这篇关于Bash -eq和==,区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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