相关疑难解决方法(0)

unix 测试何时在测试命令中使用 eq vs = vs ==?

我什么时候应该使用-eqvs =vs==

例如

[[ $num -eq 0 ]]

[[ $num = 'zzz' ]]
Run Code Online (Sandbox Code Playgroud)

我观察到对数字和字符串使用-eq(和-ne等)的模式=。有没有原因,我应该什么时候使用==

shell test

26
推荐指数
1
解决办法
2万
查看次数

Bash - 预期的整数表达式

我正在检查我的主题的更新脚本

我有 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)

那么我做错了什么?

string shell-script test numeric-data

15
推荐指数
2
解决办法
7万
查看次数

标签 统计

test ×2

numeric-data ×1

shell ×1

shell-script ×1

string ×1