2016-01-06 56 views
0

我正在運行一個bash腳本,它通過錯誤日誌查看並生成一個在Mac終端中打印的報告。添加總計到bash腳本的輸出

目前,輸出看起來是這樣的:

This error report is derived from Command Logs from ________ to __________. 

IP addresses: 

109.xx.xxx.xxx 

Error Codes List: 

(code 12) 
(code 30) 

Common Errors - Count: 

Code 2: 
Code 10: 
Code 11: 
Code 12: 237 
Code 14: 
Code 20: 
Code 23: 
Code 30: 5 
Code 35: 
Code 37: 
Code 52: 
/usr/local/bin/backerr: line 45: `$skipping': not a valid identifier 
Total: 242 

Total Files Transferred: 
3558 

Count complete. 

The following are details for each error: 
// all of the 242 above-referenced lines // 

Skipped files: 
// lists all of the lines that have the word 'skipping\ ' // 

正如你所看到的,我沒有正確的標識符:

/usr/local/bin/backerr: line 45: `$skipping': not a valid identifier 

這是我必須檢測與行的代碼'跳過',計算總數,然後打印上面的代碼XX:總數。此外,我希望將「跳過」總數添加到「總計:242」。

count=() 
    total=0 

    while read skipping; do 
     ((count[$skipping]++, total++))  
    done < <(grep 'skipping\ ' $input_variable) 

    for $skipping in $input_variable; do 
    echo "Skipped: ${count[$skipping]}" 
    done 

我預期的輸出 「常見錯誤 - 計數:」 部分應該如下:

Common Errors - Count: 

    Code 2: 
    Code 10: 
    Code 11: 
    Code 12: 237 
    Code 14: 
    Code 20: 
    Code 23: 
    Code 30: 5 
    Code 35: 
    Code 37: 
    Code 52: 
    Skipped: 107 (I made up this number for this question) 
    Total: 349 

我在這裏停留。請幫忙。

作爲變通,而不是使用下面的代碼:

 while read skipping; do 
      ((count[$skipping]++, total++))  
     done < <(grep 'skipping\ ' $input_variable) 

     for $skipping in $input_variable; do 
     echo "Skipped: ${count[$skipping]}" 
     done 

我換成與:

echo "Total Skipped Files: " ; egrep -c 'skipping' $input_variable ; 

但是,這並不能真正產生理想的結果,因爲我最初的設想。

+0

爲什麼'<<'和爲什麼不'<' – SMA

+0

@SMA:[方法取代](HTTPS:// WWW .gnu.org/software/bash/manual/html_node/Process-Substitution.html):處理命令的輸出,如文件。 –

+0

下面的所有的代碼的總數(代碼2的: 代碼10: 代碼11: 代碼12:237 代碼14: 代碼20: 代碼23: 代碼30:5 代碼35: 代碼37: 代碼52 :)'我想有一行說'跳過:'n''其中'n'是總數。然後,我想在底部添加跳過的總數。你明白嗎? – technerdius

回答

2

for循環不應該有索引變量美元符號:

for skipping in $input_variable; do 
    ^^^ 
+0

我這樣做了,但是我得到'/ usr/local/bin/script:第44行:文件名:語法錯誤:無效算術運算符(錯誤標記爲「@filename」' – technerdius

+0

@technerdius腳本中的「filename」在哪裏? – chrisaycock

+0

文件名是$ input_variable – technerdius