【发布时间】:2014-11-11 04:54:42
【问题描述】:
我有一个 shell 脚本来检查 Debian 安装是否存在 heartbleed 漏洞。它首先获取任何已安装的面向 SSL 的软件包的版本号列表:
$: dpkg -l | grep ssl | grep amd64 | awk '{print $3}'
1.0.1e-2+deb7u3
1.0.1e-2+deb7u3
1.0.1e-2+deb7u3
这用于for 循环以确定版本号是否在具有 1.0.1e-2+deb7u6 或更高版本的固定版本之前:
#!/bin/sh
heartbleed_is_fixed() {
#
# For each package that uses SSL
for version in $(dpkg -l | grep ssl | grep amd64 | awk '{print $3}'); do
#
# The heartbleed bug was fixed in OpenSSL 1.0.1e2+deb7u6
# Check each package and if package version is less
if [[ "$version" < "1.0.1e-2+deb7u6" ]]; then
#
# return false, heartbleed is not fixed
return 1
fi
done
#
# If we got to this point then the heartbleed bug has been fixed
return 0
}
这个在 bash 中运行良好,但是这个脚本需要在 sh 中运行。在 sh 中运行时,它偶然发现了 if 语句中的表达式:“无法打开 1.0.1e-2+deb7u6:没有这样的文件”。它指示 if 语句的行号,因此显然将 < 运算符误解为输入流指令:
"$version" < "1.0.1e-2+deb7u6"
编辑:双方括号([[ 和 ]])也会在 sh: "[[: not found" 中导致错误,但 bash 需要双括号才能正确计算表达式。
如何调整此脚本以使其在 sh 中工作?
【问题讨论】:
-
尝试使用
-lt作为“小于”运算符而不是<。现在,<被解释为 I/O 重定向。 -
@rje 是个好主意,但
-lt是数字表达式的布尔运算符,而不是字符串;它返回“非法数字:1.0.1e-2+deb7u3” -
啊,是的,当然,我错了。你想做一个字符串比较。对不起:)
标签: linux string bash shell sh