1833569 Members
4522 Online
110061 Solutions
New Discussion

Shell Scripting Help...

 
SOLVED
Go to solution
Magnus Andersen
Advisor

Shell Scripting Help...

Hi All,

I'm writing a script were the user can add a parameter when executing the script with an e-mail list. I get the information and I end up with a list similar to the list below in a variable called $EMAIL.

magnus,heath,mark

I've been playing with awk to extract the data and send an e-mail to each person. My problem in doing that is that I don't know the length of the list.

Does anyone have any ideas of how to accomplish this?

Thanks,
Magnus
8 REPLIES 8
Leif Halvarsson_2
Honored Contributor

Re: Shell Scripting Help...

Hi,
there is a NF variable with the number of fields in a record. And a FS variable where you can change the default field separator from "space" to ",".
Mark Grant
Honored Contributor
Solution

Re: Shell Scripting Help...

Another alternative would be to change the shell internal field seperator to a "," and then just use a "for..." loop. Something like

IFS=",
"

VARIABLE="magnus,heath,mark"

for NAME in $VARIABLE
do
mailx -s "Your mail sir/madam" $NAME
done
Never preceed any demonstration with anything more predictive than "watch this"
Magnus Andersen
Advisor

Re: Shell Scripting Help...

I've tried using them, but since I am new to this I am doing something wrong. I can't get it to work.
Rodney Hills
Honored Contributor

Re: Shell Scripting Help...

You could avoid using awk and just shell-

#!/usr/bin/sh
IFS=","
list="magnus,heath,mark"
for $user in $list ; do
mailx -s "from a shell" $user@company.con done

Variable IFS is used to specify the parameter seperator character (in this case you are using comma).

HTH

-- Rod Hills

There be dragons...
H.Merijn Brand (procura
Honored Contributor

Re: Shell Scripting Help...

in perl,

# perl -e'qx"mailx -s Test $_ $ENV{EMAIL}'

given that the content of the mail is in the file content.txt

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Magnus Andersen
Advisor

Re: Shell Scripting Help...

Thanks Mark and Rodney. I tried your way and it worked right away...:)
Michael Schulte zur Sur
Honored Contributor

Re: Shell Scripting Help...

Hi,

with Mark's solution you would have to write the mails yourself, did it hang?

Michael
Magnus Andersen
Advisor

Re: Shell Scripting Help...

Michael,

Here is the code I ended up using for the e-mail function. It does not Hang. I've just tested it successfully.

# Function send_email_paging
send_email_paging(){
if [ "${PAGING_NOTIFY}" = 1 ]
then
if [ "${EMAIL_NOTIFY}" = 1 ]
then
echo "Pages is being sent to " $PAGING | tee -a $LOGFILE
IFS=","
for NAME in $PAGING
do
echo "cpscript.log has been sent to your e-mail" | mailx -s "cpsript" $NAME
done
IFS=" "
fi
if [ "${EMAIL_NOTIFY}" = 0 ]
then
echo "Pages is being sent to " $PAGING | tee -a $LOGFILE
IFS=","
for NAME in $PAGING
do
echo "Review cpscript.log on " `hostname` | mailx -s "cpscript" $NAME
done
IFS=" "
fi
fi

if [ "${EMAIL_NOTIFY}" = 1 ]
then
echo "Log file is being sent to " $EMAIL | tee -a $LOGFILE
IFS=","
for NAME in $EMAIL
do
mailx -s "cpscript.log" $NAME < $LOGFILE
done
IFS=" "
fi
}