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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting while read loop w/ a nested if statement - doesn't treat each entry individually
# 1  
Old 12-11-2007
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  
Old 12-11-2007
Java

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  
Old 12-11-2007
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  
Old 12-11-2007
Question

Quote:
Originally Posted by littlefrog
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  
Old 12-11-2007
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  
Old 12-11-2007
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
# 7  
Old 12-11-2007
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk doesn't understand 'read' statement!!!

While working on awk programming, i found that it doesn't understand 'read' statement. Then what's the use of 'continue' and 'break' statement in awk. For ex: awk '{k=1; while (k<10) {print $0; k++}}' emp.lst Now, please say if I want to put the logic that after priting 1 line, it will ask for... (13 Replies)
Discussion started by: ravisingh
13 Replies

2. Shell Programming and Scripting

How come this if statement doesn't work?

greetings, the following code isn't working as i expect it to. the first dbl brackets do but the second set gets ignored. ie: if i'm on t70c6n229 it echoes "Something" and i expect it not to. what am i missing? if " ]] || " ]]; then echo "Something" fi thanx! (9 Replies)
Discussion started by: crimso
9 Replies

3. UNIX for Dummies Questions & Answers

Statement to find if an entry exists in a file

I need to check if an entry input by the user is in a file. If so, I need to run a command, and if it does not exist then it should output entry does not exist. So I have so far... echo "Enter record:" read record //command || //command Can I use an if statement to do this? (3 Replies)
Discussion started by: itech4814
3 Replies

4. UNIX for Dummies Questions & Answers

Read statement within while read loop

hi, this is my script #!/bin/ksh cat temp_file.dat | while read line do read test if ]; then break else echo "ERROR" fi done when i execute this code , the script does wait for the user input . it directly prints "ERROR" and terminates after the no. of times as there... (3 Replies)
Discussion started by: siva1612
3 Replies

5. Shell Programming and Scripting

Nested While loop doesn't end

Hi, Below is my script in which i am using nested while loop to read two files and move the files to a remote server. My issue is that the 2nd while loop doesn't stop executing and it keeps on executing. Can someone please let me know where i have gone wrong. myFile=$ESER_TEST_FILES ... (2 Replies)
Discussion started by: funonnet
2 Replies

6. Shell Programming and Scripting

Perl nested if statement

I'm just having a bit of trouble running this code. It tells me that there's a syntax error on line 29. Any help appreciated. #!/usr/bin/perl # # Phone Book Application # %phonebook = ( "Wayne", '34687368', "Home", '378643287', "Work", '017374637', "School",... (2 Replies)
Discussion started by: cabaiste
2 Replies

7. Shell Programming and Scripting

If-statement nested in case

I'm trying to write case statements with 'if statements' embedded inside of them. I'm using the korn shell but it's not functioning. If I want to see if a string exists in a file and then perform an action, what would be the best way to do this? For file "asg51fin" to delete a line if a... (1 Reply)
Discussion started by: dazeman27
1 Replies

8. UNIX for Dummies Questions & Answers

Nested If statement within Do / Done

Hi all! I'm really hoping you can help me out here; now i have searched and searched and have at least worked out that you can't have a nested if statement with a 'done' in it (as i have) as you're killing the parent before the child. So here's what i have, and here's hoping someone can help... (2 Replies)
Discussion started by: dalgibbard
2 Replies

9. Shell Programming and Scripting

Nested while read line loop

Hi, Can anyone please help me: i'm trying to read a file with directory-names , then go to that directory and read another (output) file to perform some tasks per line (second read line in the part of script below). The problem is that after the nested while loop has finished, the first while... (7 Replies)
Discussion started by: Rakker
7 Replies

10. Shell Programming and Scripting

nested read

I am hoping someone can help me with this one. I am writing a ksh script on Solaris. I want to read in host names and some other info from a file, do an "rsh host 'shutdown'" (or any uname for now until I get it working), and then be given some options. The problem is I am using while read... (2 Replies)
Discussion started by: TioTony
2 Replies
Login or Register to Ask a Question