2017-01-10 68 views
0

我正在研究腳本以便自動執行許可證重新激活。我不希望每40天手動完成一次,而是希望腳本能夠爲我做到這一點。在Bash中if語句中「太多參數」問題

我已經爲licenses.txt添加了許可證,並且該腳本從licenses.txt的第一行構建了一個變量,該變量與SOAPLicenseClient命令一起使用。如果命令成功,它將從文件中刪除該行。一旦licenses.txt在許可證上運行不足,它將向日志設施local0發送日誌消息。

當命令是成功的,它會打印:

新安裝許可證

,當它試圖安裝一個已經預先安裝的許可證將打印:

許可證服務器已返回異常。故障代碼:51092故障文本:錯誤51092,此許可證已在其他設備上激活。請聯繫技術支持以獲得幫助

如果收到此錯誤,它將刪除licenses.txt文件的第一行並重新使用SOAPLicenseClient命令以嘗試新許可證。

最後我有一個包羅萬象的,將簡單的打印輸出從$結果記錄到日誌設施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" ] 

從我可以告訴基於這裏搜索是變量$結果包含太多的參數,因爲它包含幾個不同的詞與空間。

使用更簡單的腳本,我回聲出$結果變量:

腳本:

#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 

是否有可能對我來說,過濾掉$結果中的字符串並創建新變量?我試過sed,awk和grep,但是我做錯了什麼。

這樣做的最有效方法是什麼?代碼可能不太好,如果你有改進,可以隨時發言。

+3

這是一個長期的問題。你能把它縮減到基本信息嗎?請參閱創建[mcve]的幫助頁面。 –

+3

另外,請嘗試通過http://www.shellcheck.net/運行您的腳本。 –

+0

@JohnKugelman - Ran通過shellcheck,發現了一些引用和其他幾個問題。糾正了這些問題,但得到了同樣的問題。我相信現在每當它收到「已使用許可證」錯誤時,它可能與我的變量打印出3行相關。我發佈了一個很長的問題,以便你們充分了解我想要做的事情。也許最好是更具體的 –

回答

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和不關心移植到其他貝殼,用[[,它不需要引用:

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

恐怕仍然有錯誤。試過雙括號和雙引號。我會發表我的答覆作爲答案,因爲評論並沒有給你太多補充。 –

+0

@ Philip.J:更新問題可能也是一種方法。 – choroba

+0

啊好想法。從現在開始我會這樣做。謝謝回覆! –