我有一个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语句中的表达式:“cannot open 1.0.1e-2+deb7u6:No such file”。它指示if语句的行号,因此它显然将<运算符误解为输入流指令:
"$version" < "1.0.1e-2+deb7u6"

编辑:双方括号([[]])在sh中也会导致错误:[[:未找到],但是bash需要双括号才能正确计算表达式。
如何调整此脚本以使其在sh中工作?

最佳答案

首先,这里没有布尔运算符。布尔运算符(例如:and,or)应该用于布尔值,而不是字符串。
sh中,没有[[,因此您需要好的旧[
你必须逃走:

if [ "$version" \< "1.0.1e-2+deb7u6" ]; then

您也可以使用<"<",只要确保它已转义。

10-04 12:29