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
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.

| improve this answer | |
  • Great... it was simple solution... Thanks bunch Arkadiusz... that worked. – Riz Aug 20 '17 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. – Arkadiusz Drabczyk Aug 20 '17 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.