while read loop w/ a nested if statement - doesn't treat each entry individually

Hi -

Trying to take a list of ldap suffixes in a file, run an ldapsearch command on them, then run a grep command to see if it's a match, if not, then flag that and send an email alert.

The list file (ldaplist) would look like -

***********
o=company a
o=company b
***********

** Note there are spaces in that file that need to be preserved. I was trying to use a for loop and someone suggested a while read loop in order to prevent the space from being treated as a line break.

The problem w/ the script i created below is that it runs and only sends one email w/ the info from both entries in ldaplist. It's like my nested 'if' loop is not the right loop to use. I made it so that both entries should have 0 return codes and no luck. Runs once and that's it. Any suggestions to a better structure to this? I would try to use a for i in cat /ldaplist - do .... - but that fails because it doesn't treat the space in the ldaplist (ie: o=company a) as a space - even with " ".

while read i

    do 

ldapsearch -h server1 -b "$i" objectclass=* ibm-replicationState |grep ibm-replicationState=ready > /dev/null 2>&1

    if [ $? -ne 0 ]; then 

   mailx -s "LDAP sync alert on \`hostname\`" [email]u@mail.com[/email] 2>&1 

fi
done <ldaplist

Thanks!

Your script looks like it would send one email per ldap suffix that has a problem - and that's the issue right?
I think you are wanting one email containing a list of all the failing suffixes?

In which case, try this:

#!/bin/sh
errors=""
while read i
do
  ldapsearch -h server1 -b "$i" objectclass=* ibm-replicationState |grep ibm-replicationState=ready > /dev/null 2>&1 || errors="${errors}
${i}"
done <ldaplist
if [ -n "$errors" ]
then
  echo $errors | mailx -s "LDAP sync alert on `hostname`" u@mail.com 2>&1
fi

(Untested)
BTW, The linefeed between errors} and ${i}" is not accidental, you want that in there

Right now - rather than sending one email per suffix - it sends one email and in the body of the email is the output for each suffix.

Thanks

I'm probably just being dense but I still don't understand what you are saying...
Are you saying that your current script (if statement within the while loop) is sending only one email even if there's a bunch of failing ldap suffix lookups?

What behaviour do you actually want (ie how many emails and what info in each message):

  • When there's no suffixes that fail to look up?
  • When just one fails?
  • When more than one fails?

sorry - just having a difficult time explaining this mess....

What i would like is:

For each suffix in the ldaplist file -

To run an ldap search command

And then grep for 'ibm-replicationState=ready '

If there is a match - do nothing, if not, then send an email.

What I'm getting is only one email, and in the body of the email are the other suffixes in my ldaplist file.

For instance here is what the body of the email would look like

-----
o=group b
o=group c
-----

So it looks like it runs - but gets triggered on just one of the list items. In this case - o=company a. When i switch the script to do an echo 'echo $i' rather than the mailx statement i get

--------
ibm-replicationState=ready
o=company a
ibm-replicationState=ready
o=company b
---------

I can skip all of the loops - just thought it would be cool to figur it out.

One other note -

I took out the '> /dev/null 2>&1' from the ldap search command - and i only see one instance of 'ibm-replcationState=ready' returned to my screen. -

So it looks like for some reason, it's only run that part of the loop once?? - Stange

You can't just do "mailx". mailx wants to read an email. so you need to do something like:
echo fumble | mailx

As it is now, mailx is reading from stdin and thus sucking up the rest of the file from your < ldaplist.

Many thanks - the mailx was the issue - switched the script around and seems to now work as hoped!