Question

I am trying to write a script to automate my server auditing headaches. I need to check what webserver is running on a server and then find its uptime. But eventhough I am able to find out the webserver I can't compare it with the variable I have saved it to..Here is the part I am having issue with. This is in bash.

Webserver=`curl -Is $(hostname -i) | grep "Server" | awk {'print $2'} | cut -d'/' -f1`

if [ "$Webserver" == "Apache" ]

then
echo "Webserver Apache Uptime: $(/etc/init.d/httpd status | grep "Server uptime")" >> $TEMPFILE

else if [ "$Webserver" == "LiteSpeed" ]

then
echo "Webserver Litespeed: $(head -n4 /tmp/lshttpd/.rtreport | grep UPTIME)" >> $TEMPFILE

else
echo "Unidentified Webserver"

NB: I have the results saved to a temp file in /tmp

No correct solution

OTHER TIPS

Your syntax is wrong. You need elif instead of else. Also you're mssing fi at the end of your if statement. Also, for what it's worth, you should wrap $TEMPFILE in double quotes to prevent word splitting for files that contains spaces.

if [[ "$Webserver" == "Apache" ]]; then
  echo "Webserver Apache Uptime: $(/etc/init.d/httpd status | grep "Server uptime")" >> "$TEMPFILE"
elif [ "$Webserver" == "LiteSpeed" ]; then
  echo "Webserver Litespeed: $(head -n4 /tmp/lshttpd/.rtreport | grep UPTIME)" >> "$TEMPFILE"
  echo "Unidentified Webserver"
fi

If you need some good references these are two of my favorite:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top