Operating System - HP-UX
1821244 Members
2958 Online
109632 Solutions
New Discussion юеВ

Shell script - while do loop

 
SOLVED
Go to solution
Chuck J
Valued Contributor

Shell script - while do loop

Hi

I want to use a while loop to read from a file. I would have a file called "notify", which would contain a list of users for who I want to email, there would be one email address per line. For example in the "notify" file I could have:

chuck@dom.com
dennis@dom.com

and so on

Then, for each person in the file I want to do awk out the username before the @ symbol:

while there are people in the file
echo "name, database is going down" | mail emailaddy
end

So, in effect for the first execution of the loop this would happen:
echo "chuck, database is going down" | mail chuck@dom.com


Regards
Chuck
13 REPLIES 13
Robin Wakefield
Honored Contributor

Re: Shell script - while do loop

Hi Chuck,

while read addr ; do
echo $addr | cut -d@ -f1 | read name
echo "$name, database is going down | mail $addr"
done < notify

Rgds, Robin
Robin Wakefield
Honored Contributor
Solution

Re: Shell script - while do loop

Duh,

move the quotes:

while read addr ; do
echo $addr | cut -d@ -f1 | read name
echo "$name, database is going down" | mail $addr
done < /tmp/names

Rgds, Robin
Tom Maloy
Respected Contributor

Re: Shell script - while do loop

OTOH, if you use ksh:

while read addr ; do
echo "${addr%@*}, database is going down" | mail $addr
done < /tmp/names

the ${} is a ksh construct. The %@* will remove the pattern that starts with @ from the variable.

Tom
Carpe diem!
James R. Ferguson
Acclaimed Contributor

Re: Shell script - while do loop

Hi:

The built-in pattern substitution command offerd by Tom is available in the Posix shell too. See 'man -sh-posix'.

Regards!

...JRF...
Leif Halvarsson_2
Honored Contributor

Re: Shell script - while do loop

IFS="@"
while read a b
do
echo "$a database is going down" |mail $a\@$b
done
harry d brown jr
Honored Contributor

Re: Shell script - while do loop


cat dbusernotifyfile | awk -F\@ '{name=$1; email=$0; printf("echo %s, database is go
ing down \| mailx -s \\\"database is going down\\\" %s\n", name, email);}' | xargs
-i sh {}


live free or die
harry
Live Free or Die
Chuck J
Valued Contributor

Re: Shell script - while do loop

James & Harry, your commands don't work!?
Chuck J
Valued Contributor

Re: Shell script - while do loop

Thanks everyone for your responses!!!

James & Harry, your commands don't work!?
James R. Ferguson
Acclaimed Contributor

Re: Shell script - while do loop

Hi Chuck:

#!/usr/bin/sh
addr=jrferguson@xyz.com
echo ${addr#*@} #...yields xyz.com
echo ${addr%@*} #...yields jrferguson

Regards!

...JRF...
harry d brown jr
Honored Contributor

Re: Shell script - while do loop


If you CUT & PASTED then you might have eneded up with a CTRL-J in the middle of the line. I just cut and pasted and I had to remove the CTRL-J and it worked fine.

I included the subject line, because there is nothing more anoying than an email without a subject, which is why I used mailx.

what was the error message? Did you replace the file being cat'ed with the file of email addresses you are using?

live free or die
harry
Live Free or Die
Sean OB_1
Honored Contributor

Re: Shell script - while do loop

How about this:

for ADDRESS in `cat /dir/notify`
do
USERNAME=` echo $ADDRESS| cut -d@ -f1 `
echo "$USERNAME, database is going down" | mail $ADDRESS
done


Chuck J
Valued Contributor

Re: Shell script - while do loop

G'Day Harry. Error is:

# ./awks
awk: The string echo %s, d cannot contain a newline character.
The source line is 1.
The error context is
{name=$1; email=$0; printf("echo %s, database is go >>>
<<<
syntax error The source line is 2.
awk: The statement cannot be correctly parsed.
The source line is 2.

./awks[4]: -i: not found.
Chuck J
Valued Contributor

Re: Shell script - while do loop

Harry, don't worry - I made the whole thing one line of code and it worked, but the email said this:

This message uses a character set that is not supported by the Internet Service. To view the original message content, open the attached message. If the text doesn't display correctly, save the attachment to disk, and then open it using a viewer that can display the original character set.

Then when you open the attachment it has the whole thing in there:

Received: from blah.com([19.150.203.126]) by blah.com with SMTP (Microsoft Exchange Internet Mail Service Version 5.5.2655.15)
id TTXP9425; Fri, 27 Sep 2002 04:08:06 -0400
Received: (from root@localhost) by blah.blah.com (8.7.1/8.7.1) id JAA07720 for me@blah.com; Fri, 27 Sep 2002 09:09:04 +0100 (BST)
Date: Fri, 27 Sep 2002 09:09:04 +0100 (BST)
From: root@blah.com
Message-Id: <200209270809.JAA07720@blah.com>
To: me@blah.com
Subject: database is going down
Mime-Version: 1.0
Content-Type: text/plain; charset=X-roman8
Content-Transfer-Encoding: 7bit

me, database is going down