Bash exit codes

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
User avatar
WillemDH
Posts: 2320
Joined: Wed Mar 20, 2013 5:49 am
Location: Ghent
Contact:

Bash exit codes

Post by WillemDH »

Hello,

I'm trying to get some logging done in a bash script that will update my nsclient.ini, but I'm having issues with the exitcode given by the check_nrpe.

This is the code:

Code: Select all

#!/bin/bash

HOSTNAME=$1
LOGFILE=/var/log/update_nscp_ini_legacy.log

NOW=$(date '+%Y-%m-%d -- %H:%M')
echo "$NOW : Nsclient legacy ini update started on $HOSTNAME." >> /var/log/update_nscp_ini_legacy.log
/usr/local/nagios/libexec/check_nrpe -H wingatewayserver -t 300 -c update_nscp_ini_legacy -a $HOSTNAME
EXITCODE=$?
echo "$NOW : Exit code $?" >> /var/log/update_nscp_ini_legacy.log

NOW=$(date '+%Y-%m-%d -- %H:%M')

if [$EXITCODE -eq 0]
then
        echo "$NOW : Nsclient legacy ini update succeeded on $HOSTNAME, with exit code $EXITCODE" >> /var/log/update_nscp_ini_legacy$
else
        echo "$NOW : NSClient legacy ini update failed on $HOSTNAME, with exit code $EXITCODE" >> /var/log/update_nscp_ini_legacy.log
fi

exit 0
The log file shows however:

Code: Select all

2014-04-30 -- 14:01 : Nsclient legacy ini update started on servername.
2014-04-30 -- 14:01 : Exit code 0
2014-04-30 -- 14:01 : NSClient legacy ini update failed on servername, with exit code 0
Although I'm 100 % sure the nsclient.ini file was updated and the $EXITCODE is 0, so somehow the if statement is nto working as expected. Any tips?

Willem
Nagios XI 5.8.1
https://outsideit.net
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

Re: Bash exit codes

Post by scottwilkerson »

Spaces matter a lot in bash tests .... change this

Code: Select all

if [$EXITCODE -eq 0]
to this

Code: Select all

if [ $EXITCODE -eq 0 ]
Former Nagios employee
Creator:
Human Design Website
Get Your Human Design Chart
User avatar
WillemDH
Posts: 2320
Joined: Wed Mar 20, 2013 5:49 am
Location: Ghent
Contact:

Re: Bash exit codes

Post by WillemDH »

Thanks. Spaces. Need to remember that. :) Thread can be closed.
Nagios XI 5.8.1
https://outsideit.net
Locked