2016-09-09 2 views
0

Syntaxfehler für unterstes Shell-Skript erhalten. Unfähig herauszufinden, wo das Problem liegt. Bitte hilf mir Jungs. Danke im Voraus.Syntaxfehler für else im Shell-Skript erhalten

Code:

#!/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" [email protected] 
elif [ "$res" == "Normal" ] 
    echo "Temperature under limit. Ignoring countermeasures!" 
else 
    echo "Unable to determine value" 
fi 

Ausgang:

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' 

Antwort

2

In L 24 ine:

24 elif [ "$res" == "Normal" ] 
25 then   # <------ Missing in your original code 
26  echo "Temperature under limit. Ignoring countermeasures!" 

Verwenden shellcheck.net, für eine solche triviale Fragen debuggen.

1

Sie benötigen einen zusätzlichen then nach dem elif Linie :)

#!/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" [email protected] 
elif [ "$res" == "Normal" ] 
then # <== HERE! 
    echo "Temperature under limit. Ignoring countermeasures!" 
else 
    echo "Unable to determine value" 
fi 
Verwandte Themen