2012-12-04 134 views
0

我試圖通過一個腳本來向我發送通知,如果我們的服務器上的負載太高。我找到了一個好的,但是當我運行它時它給了我和錯誤,我看不出爲什麼。Bash腳本錯誤。意外令牌附近的語法錯誤

運行下面的代碼給出了錯誤:

line 13: syntax error near unexpected token `fi'

我想我不得不制定了正確不過。謝謝!

#!/bin/bash 

THR=10 
MAIL="[email protected]" 

VAR=`uptime|awk -F, '{print $4}'|awk '{print $3}'` 
OUT=`echo "$VAR $THR" | awk '{if ($1 > $2) print "yes"; else print "no"}'` 
if [ "$VAR" == "" ] 
then 
    # it's within the first 24 hours of uptime 
    VAR=`uptime|awk -F, '{print $3}'|awk '{print $3}'` 
    OUT=`echo "$VAR $THR" | awk '{if ($1 > $2) print "yes"; else print "no"}'` 
fi 
if [ "$OUT" == "yes" ] 
then 
    echo "The current load $VAR is greater than the threshold $THR" | mail $MAIL 
    -s "Server Load Alert" 
    echo "Alert generated because $VAR is greater than $THR" 
else 
    echo "No alert as $VAR > $THR" 
fi 
echo "load = $VAR" 
+0

用您的shebang行中的標誌-vx執行,並確認錯誤是什麼! '#!/ bin/bash -vx' – Vijay

+0

嗯,空白給了我一個錯誤,所以我刪除了它。現在我得到一個意外的文件結尾 – Adam

+0

嘗試在調試模式下運行它,#/ bin/bash --debug --verbose file.sh –

回答

2

對不起,沒有冒犯,但你的bash風格是可怕的!

這裏有一個更好的版本:

#!/bin/bash 

thr=10 
mail="[email protected]" 

read var _ < /proc/loadavg 

if (($(bc -l <<< "$var>$thr"))); then 
    echo "The current load $var is greater than the threshold $thr" | mail "$mail" -s "Server Load Alert" 
    echo "Alert generated because $var is greater than $thr" 
else 
    echo "No alert as $var <= $thr" 
fi 
echo "load = $var" 

的變化如下:

  • 使用小寫的變量名,如大寫變量名被認爲是不好的bash的做法。
  • 不解析使用數以百萬計的管道,次外層和awk是因爲它是低效的命令uptime的輸出,是直接從文件/proc/loadavg獲得相同的信息,具有read內置。
  • 請勿使用awk來測試不等式,使用bc,它更有效率(並且根本不需要變量$OUT)。
  • 沒有反引號!改用$(...)構造(更易於閱讀,嵌套和更好的bash練習)。

我還沒有測試腳本,只是糾正了你的閱讀。請告訴我它是否適合你。

+0

I got the運行後輸出如下。 'root @ server [〜]#bash /scripts/load_check.sh :command not foundcheck.sh:line 2: :command not foundcheck.sh:line 5: :No such file or directory:line 6:/ proc/loadavg :命令not foundcheck.sh:第7行: /scripts/load_check.sh:第15行:語法錯誤:文件意外結束 – Adam

+0

@AdamG腳本是什麼'load_check.sh'和'foundcheck.sh' ?你能編輯你的OP並將它們包含在那裏嗎? –

+0

load_check.sh是我正在練習的這個腳本。我不知道什麼foundcheck.sh是。我有很多其他腳本在服務器上運行,我不知道爲什麼這個引起這樣的頭痛。 – Adam

0
#!/bin/bash 

THR=10 
MAIL="[email protected]" 

VAR=`uptime|awk -F, '{print $4}'|awk '{print $3}'` 
OUT=`echo "$VAR $THR" | awk '{if ($1 > $2) print "yes"; else print "no"}'` 
if [ "$VAR" == "" ] 
then 
# it's within the first 24 hours of uptime 
VAR=`uptime|awk -F, '{print $3}'|awk '{print $3}'` 
OUT=`echo "$VAR $THR" | awk '{if ($1 > $2) print "yes"; else print "no"}'` 
fi 
if [ "$OUT" == "yes" ] 
then 
echo "The current load $VAR is greater than the threshold $THR" | mail $MAIL -s "Server Load Alert" 
echo "Alert generated because $VAR is greater than $THR" 
else 
echo "No alert as $VAR > $THR" 
fi 
echo "load = $VAR" 

這對我的作品。我改變了,以便「mail $ MAIL」和-s「服務器負載警報」保持在同一行。

+0

我仍然收到意外的文件結尾。我需要在服務器上進行任何更改以使其更寬容嗎? – Adam

+0

執行腳本,但使用#!/ bin/bash -x交換#!/ bin/bash併發布輸出 – Arnestig

+0

運行時我得到了以下輸出。 'root @ server [〜]#bash /scripts/load_check.sh:command not foundcheck.sh:line 2::not foundcheck.sh:line 5:line 22:syntax error:unexpected end of file' – Adam

相關問題