2012-12-04 135 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

相关问题