2

I have to check Tomcat 8 is running or not. For this I am using the below script.

#!/bin/bash

statuscode=$(wget --server-response http://localhost:8080 2>&1 | awk '/^  HTTP/{print $2}')

if [ $statuscode -eq 200 ]
then
    echo "TOMCAT OK"
    exit 0
else
    echo "TOMCAT CRITICAL"
    exit 2
fi

When I run this script on CentOS 7.

If Tomcat 8 is running then script is running without any error.

If Tomcat 8 is stopped then script is running with following error

line 5: [: -eq: unary operator expected

How can I fix this issue?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3441151
  • 1,880
  • 6
  • 35
  • 79
  • 1
    This is because `$statuscode` get populated with an empty string or something strange. Try to debug by `echo`ing the variable before the `if` condition. – fedorqui Aug 09 '16 at 10:45
  • @fedorqui yes when tomcat stopped $statuscode is empty. How can i handle this? – user3441151 Aug 09 '16 at 10:47
  • you can add an empty variable check before. – Thirupathi Thangavel Aug 09 '16 at 10:51
  • If you were quoting correctly (`[ "$statuscode" -eq 200 ]`), you'd be getting a more useful error; it's the lack of quotes that causes the shell to string-split your variable into an unknown number of arguments (in this case 0) before passing those arguments to the `[` command (aka test). – Charles Duffy Aug 09 '16 at 13:02
  • The canonical may be *["unary operator expected" error in Bash if condition](https://stackoverflow.com/questions/13617843/)* – Peter Mortensen Oct 05 '21 at 16:56

2 Answers2

2

Check if the variable is not empty before comparing it against the expected output.

#!/bin/bash

statuscode=$(wget --server-response http://localhost:8080 2>&1 | awk '/^  HTTP/{print $2}')

if [ -n "$statuscode" ] && [ $statuscode -eq 200 ]
then
    echo "TOMCAT OK"
    exit 0
else
    echo "TOMCAT CRITICAL"
    exit 2
fi
Thirupathi Thangavel
  • 2,418
  • 3
  • 29
  • 49
  • If my tomcat 8 service is running then "wget --server-response http://localhost:8080 2>&1 | awk '/^ HTTP/{print $2}'" would give me always 200 or something else value? – user3441151 Aug 09 '16 at 11:21
  • There should be better way of checking if a service is running or not. Like a `systemctl` command – Thirupathi Thangavel Aug 09 '16 at 11:32
0

Try this:

If statuscode is empty, then throw -eq: unary operator expected.

#!/bin/bash
{
statuscode=$(wget --server-response http://localhost:80 2>&1 | awk '/^  HTTP/{print $2}')

if [ -z "$statuscode" ]
then
echo "TOMCAT CRITICAL";
exit 2;
else
  if [ $statuscode -eq 200 ]
    then
     echo "TOMCAT OK";
     exit 0;
  fi
fi
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mustafa DOGRU
  • 3,994
  • 1
  • 16
  • 24
  • Re *"then throw -eq: unary operator expected."*: Where is that happening? Can you elaborate? E.g, what do you mean by ***"throw"***? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/38848796/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Oct 05 '21 at 17:00