1

I'm creating a monitoring script that looks at results from multiple hosts in for loop and then echo's OK or NOT OK... If all resulted to OK then I want to combine the message to single OK else NOT OK. Here's what currently works:

Script:

for HOST in $HOST1 $HOST2 $HOST2
do
    HEARTBEAT=$(${LDAPSEARCH} -h ${HOST} -p ${PORT} -D "${USER}" -w $${PASS} -b "" -s base "(objectClass=*)" | grep dn: | sed 's/dn:/dn/g')
    if [ $HEARTBEAT = 'dn' ]; then
        echo "ok"
    else
        echo "NOT OK"
    fi
done

Output:

$OK
$OK
$OK

Else Output:

$NOT OK
$NOT OK
$NOT OK

Desired Output:

$OK
else
$NOT OK

1 Answer 1

3

How about this - a little re-organizing of your script:

#!/usr/bin/env bash

for HOST in $HOST1 $HOST2 $HOST2
do
    HEARTBEAT=$(${LDAPSEARCH} -h ${HOST} -p ${PORT} -D "${USER}" -w $${PASS} -b "" -s base "(objectClass=*)" | grep dn: | sed 's/dn:/dn/g')
    if [ ! $HEARTBEAT = 'dn' ]; then
        echo "NOT OK"
        exit 1
    fi
done

echo "ok"

This script will print a single NOT OK after encountering the first incorrect HOST and will print a single OK if there no incorrect HOSTs.

2
  • Great... it was simple solution... Thanks bunch Arkadiusz... that worked.
    – Riz
    Commented Aug 20, 2017 at 19:13
  • 1
    I'm glad it works for you. If you like this answer please mark it as accepted so that other users know it's solved. Commented Aug 20, 2017 at 19:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .