获取以下shell脚本的语法错误。无法弄清问题所在。请帮帮我们。在此先感谢。
代码:
#!/bin/bash
# Check if package sensors (for Ubuntu) is installed, if not install it
dpkg -l | grep -i sensors > /dev/null
if [ "$?" == "0" ]; then
echo "Package sensors already installed on system. Analyzing temperature!"
else
sudo apt-get install sensors && echo "Package sensors now installed"
fi
# Set threshold temperature
threshold="+80.0°C"
# Check if temp has reached threshold, trigger mail
tempnow=$(sensors | sed -n '20p' | awk '{print $NF}')
res=`echo "$tempnow $threshold" | awk '{ if($1 > $2) print "Exceeds"; else print "Normal" }'`
if [ "$res" == "Exceeds" ]
then
echo "Temperature exceeds threshold. Triggering mail to system owners..."
mail -s "CPU temperature too high on system" abc@xyz.com
elif [ "$res" == "Normal" ]
echo "Temperature under limit. Ignoring countermeasures!"
else
echo "Unable to determine value"
fi
输出:
Package sensors already installed on system. Analyzing temperature!
/home/luckee/scripts/cputemp.sh: line 26: syntax error near unexpected token `else'
/home/luckee/scripts/cputemp.sh: line 26: `else'
答案 0 :(得分:2)
第24行:
24 elif [ "$res" == "Normal" ]
25 then # <------ Missing in your original code
26 echo "Temperature under limit. Ignoring countermeasures!"
使用shellcheck.net
来调试此类微不足道的问题。
答案 1 :(得分:1)
then
行之后需要额外的elif
:
#!/bin/bash
# Check if package sensors (for Ubuntu) is installed, if not install it
dpkg -l | grep -i sensors > /dev/null
if [ "$?" == "0" ]; then
echo "Package sensors already installed on system. Analyzing temperature!"
else
sudo apt-get install sensors && echo "Package sensors now installed"
fi
# Set threshold temperature
threshold="+80.0°C"
# Check if temp has reached threshold, trigger mail
tempnow=$(sensors | sed -n '20p' | awk '{print $NF}')
res=`echo "$tempnow $threshold" | awk '{ if($1 > $2) print "Exceeds"; else print "Normal" }'`
if [ "$res" == "Exceeds" ]
then
echo "Temperature exceeds threshold. Triggering mail to system owners..."
mail -s "CPU temperature too high on system" abc@xyz.com
elif [ "$res" == "Normal" ]
then # <== HERE!
echo "Temperature under limit. Ignoring countermeasures!"
else
echo "Unable to determine value"
fi