The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Nested loop not running using cronjob bihani4u Shell Programming and Scripting 9 09-26-2007 12:19 PM
Variable in While Loop Nested If geass Shell Programming and Scripting 6 03-26-2007 07:09 PM
Nested while read line loop Rakker Shell Programming and Scripting 7 06-24-2005 08:42 AM
nested loop chinog Shell Programming and Scripting 5 04-20-2005 11:45 AM
nested read TioTony Shell Programming and Scripting 2 03-05-2004 03:11 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-11-2007
littlefrog littlefrog is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 12
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`" u@mail.com 2>&1
fi
done <ldaplist


Thanks!
  #2 (permalink)  
Old 12-11-2007
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
  
 

Join Date: Nov 2007
Location: New Zealand
Posts: 922
Post

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:

Code:
#!/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
  #3 (permalink)  
Old 12-11-2007
littlefrog littlefrog is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 12
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
  #4 (permalink)  
Old 12-11-2007
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
  
 

Join Date: Nov 2007
Location: New Zealand
Posts: 922
Question

Quote:
Originally Posted by littlefrog View Post
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.
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?
  #5 (permalink)  
Old 12-11-2007
littlefrog littlefrog is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 12
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.
  #6 (permalink)  
Old 12-11-2007
littlefrog littlefrog is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 12
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:40 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0