我什么时候应该使用-eq
vs =
vs==
例如
[[ $num -eq 0 ]]
[[ $num = 'zzz' ]]
Run Code Online (Sandbox Code Playgroud)
我观察到对数字和字符串使用-eq
(和-ne
等)的模式=
。有没有原因,我应该什么时候使用==
我正在检查我的主题的更新脚本
我有 2 个文本文件。第一个称为“current.txt”并包含当前版本。4.1.1
该文本文件中有字符串。
第二个称为“latest.txt”并包含最新版本。4.2
此文本文件中有字符串。
所以这是代码
echo "Checking update";
x=$(cat ./current.txt)
y=$(cat ./latest.txt)
if [ "$x" -eq "$y" ]
then
echo There is version $y update
else
echo Version $x is the latest version
fi
Run Code Online (Sandbox Code Playgroud)
这意味着如果 current.txt 与 latest.txt 不同,那么它会说“有 4.2 版更新”。如果不是,它会说“版本 4.1.1 是最新版本”
但是当我尝试运行它时。我收到这个错误
Checking update
./test.sh: line 4: [: 4.1.1: integer expression expected
Version 4.1.1 is the latest version
Run Code Online (Sandbox Code Playgroud)
那么我做错了什么?