Bash if statement equal output from last command

bashscripting

I am trying to equal something from last command with bash if statement:

#!/bin/bash

monit status

if [ "status" != "error" ]; then
        echo -e "hostname\ttest\t0\t0" | /usr/sbin/send_nsca -H hostname -c /etc/send_nsca.cfg
        exit 1;
fi

Even if the monit status gives out status = online with all services it runs the echo command. I can not figure out how to make the if statement match the status of monit status output.

Best Answer

You are comparing the static strings status vs. error.

There are several ways to go about this. For capturing the output of the command in a variable, use

STATUS=`monit status`

or

STATUS=$(monit status)

For a simple case as your's, I would go for a simple

if monit status | grep -q error ; then
    ...
fi