"参数太多"在Bash的if语句中出现问题

时间:2017-01-10 15:21:54

标签: bash awk sed

我正在编写脚本以自动执行许可证重新激活。而不是每40天手动执行此操作,我希望脚本能够为我完成。

我已将许可证添加到licenses.txt,该脚本从licenses.txt的第一行构建一个变量,该变量与SOAPLicenseClient命令一起使用。如果命令成功,则它将从文件中删除该行。一旦licenses.txt在许可证上运行不足,它将向日志工具local0发送一条日志消息。

当命令成功时,它将打印:

  

已安装新许可证

当它尝试安装以前已安装的许可证时,它将打印:

  

许可证服务器已返回异常。故障代码:51092故障文本:错误51092,此许可证已在其他设备上激活。请联系技术支持以获取帮助

如果收到此错误,它将删除licenses.txt文件的第一行并重用SOAPLicenseClient命令以尝试新的许可证。

最后,我只需将$ result的输出打印到日志工具0中。

以下是整个脚本:

#VARIABLES
BaseReg=$(head -n1 licenses.txt)
currentlicensecount=$(wc -l < licenses.txt)
licensewarninglimit=2
nolicenses=0

if ! [ -f "/config/licenses.txt" ]
    then 
    /bin/logger -p local0.error "License reactivation failed - Licensefile is MISSING - Please add a Licensefile to /config/licenses.txt"
fi

if [ $currentlicensecount -eq $nolicenses ]
    then
        /bin/logger -p local0.error "No licenses available! - Remaining Licenses: $currentlicensecount - Please add more licenses"
    else 
    result=$(SOAPLicenseClient --basekey "$BaseReg")
        if [ $result -eq "New license installed" ]
            then
            #Removing the recently used BaseRegistration key from the licenes.txt       
            tail -n +2 licenses.txt > licenses.tmp && mv licenses.tmp licenses.txt
            newlicensecount=$(wc -l < licenses.txt)
            /bin/logger -p local0.notice "License reactivation succeded - "$result" - Remaining Licenses: $newlicensecount"
                if [ $newlicensecount -lt $licensewarninglimit ]
                    then
                    /bin/logger -p local0.warning "Current Licensecount is below warning threshold - Remaining Licenses: $newlicensecount - Please add more licenses"
                elif [ $newlicensecount -eq $nolicenses ]
                    then
                    /bin/logger -p local0.error "No licenses available! - Remaining Licenses: $newlicensecount - Please add more licenses"
                fi
        elif [ $result -eq "License server has returned an exception. Fault code: 51092 Fault text: Error 51092, This license has already been activated on a different unit. Please contact technical support for assistance" ]
            then
            #Removing the recently used BaseRegistration key from the licenes.txt       
            tail -n +2 licenses.txt > licenses.tmp && mv licenses.tmp licenses.txt
            NewBaseReg=$(head -n1 licenses.txt)
            newresult=$(SOAPLicenseClient --basekey "$NewBaseReg")
            if [ $newresult -eq "New license installed" ]
                then
                #Removing the recently used BaseRegistration key from the licenes.txt       
                    tail -n +2 licenses.txt > licenses.tmp && mv licenses.tmp licenses.txt
                    newlicensecount=$(wc -l < licenses.txt)
                    /bin/logger -p local0.notice "License reactivation succeded - "$result" - Remaining Licenses: $newlicensecount"
                        if [ $newlicensecount -lt $licensewarninglimit ]
                            then
                            /bin/logger -p local0.warning "Current Licensecount is below warning threshold - Remaining Licenses: $newlicensecount - Please add more licenses"
                        elif [ $newlicensecount -eq $nolicenses ]
                            then
                            /bin/logger -p local0.error "No licenses available! - Remaining Licenses: $newlicensecount - Please add more licenses"
                        fi
        else
        /bin/logger -p local0.error "License reactivation failed - "$result" - Remaining Licenses: $currentlicensecount"
                if [ $currentlicensecount -lt $licensewarninglimit ]
                    then
                    /bin/logger -p local0.warning "Current Licensecount is below warning threshold - Remaining Licenses: $currentlicensecount - Please add more licenses"
                elif [ $currentlicensecount -eq $nolicenses ]
                    then
                    /bin/logger -p local0.error "No licenses available! - Remaining Licenses: $currentlicensecount - Please add more licenses"
                fi

        fi
    fi
fi

运行脚本时,我收到以下错误:

# sh ./license_reactivation.sh
./license_reactivation.sh: line 20: [: too many arguments
./license_reactivation.sh: line 33: [: too many arguments

它抱怨的是:

if [ $result -eq "New license installed" ]

elif [ $result -eq "License server has returned an exception. Fault code: 51092 Fault text: Error 51092, This license has already been activated on a different unit. Please contact technical support for assistance" ]

根据我在这里搜索可以告诉我的是,变量$ result包含太多的参数,因为它包含几个带空格的不同单词。

使用更简单的脚本我回显$ results变量:

脚本:

#VARIABLES
BaseReg=$(head -n1 licenses.txt)
currentlicensecount=$(wc -l < licenses.txt)
licensewarninglimit=2
nolicenses=0

if ! [ -f "/config/licenses.txt" ]
    then 
    /bin/logger -p local0.error "License reactivation failed - Licensefile is MISSING - Please add a Licensefile to /config/licenses.txt"
fi

if [ $currentlicensecount -eq $nolicenses ]
    then
    /bin/logger -p local0.error "No licenses available! - Remaining Licenses: $currentlicensecount - Please add more licenses"
    else 
        result=$(SOAPLicenseClient --basekey "$BaseReg")
        echo $result
fi

结果如下:

# sh ./license_reactivation.sh
License server has returned an exception. Fault code: 51092 Fault text: Error 51092, This license has already been activated on a different unit. Please contact technical support for assistance

我可以从$ results中过滤掉字符串并创建新变量吗?我试过sed,awk和grep,但我做错了什么。

这样做最有效的方法是什么?代码可能不是很漂亮,如果你有改进,请随时说出来。

1 个答案:

答案 0 :(得分:4)

如果变量包含空格,则会在[ ... ]内展开,并且[的参数数量会增加:

three_words='a b c'
[ $three_words = 'a b c' ]

实际上被解释为

[ a b c = 'a b c' ]
# 1 2 3 4 5

我在这里使用了=,因为-eq用来比较数字,而不是字符串。

你知道吗? 5个字!

解决方案?双引号变量:

[ "$three_words" = 'a b c' ]

或者,如果在bash并且不关心其他shell的可移植性,请使用[[,它不需要引用:

[[ $three_words = 'a b c' ]]