script broadcast email

Dear All need advice,

I just want broadcast email with mail from shell script, where the script look up the file that contain email address and password

#!/bin/bash
SUBJECT="Mailserver Baru"
ISIEMAIL="/tmp/emailmessage.txt"
 for a in `(sed 's/"//g;s/\,.*//g' $1)`;
do
echo > $ISIEMAIL
EMAIL="$a"
echo "Dear $a" >> $ISIEMAIL
{  

for b in `sed 's/"//g;s/^.*\(........\)$/\1/g' $1`;
	do
{
echo; echo >> $ISIEMAIL
	echo "Kami sedang mengupgrade system email server, password email $EMAIL yang baru adalah $b" >> $ISIEMAIL
	echo "Mohon segera mengganti password anda dengan mengakses alamat berikut" >> $ISIEMAIL
     };
done;
echo; echo >> $ISIEMAIL
   };
break;
done;
/usr/bin/mail -s "$SUBJECT" "$EMAIL" < $ISIEMAIL 
echo > $ISIEMAIL 

content of $1 ;

"admin@somedomain.com","#97ukjkl"
"agung@mydomain.com","2adfpkjg"

the output after execute command ;

Dear admin@somedomain.com

Kami sedang mengupgrade system email server, password email admin@somedomain.com yang baru adalah #97ukjkl
Mohon segera mengganti password anda dengan mengakses alamat berikut

Kami sedang mengupgrade system email server, password email admin@somedomain.com yang baru adalah 2adfpkjg
Mohon segera mengganti password anda dengan mengakses alamat berikut

$a = wrong, because just read admin@somedomain.com
$b = right, because read on password field from each line

myquestion is ;

how can i use this script correctly, i mean execute line by line and looping till done

need your advice

Useless Use of Backticks

You should read the lines once, process once, instead of reading them n times and processing them once.

If you're running sed to process a single line, you're probably doing something wrong. Use shell builtins for it instead.

Also, you can use a little-known trick of xargs to process the quotes for you.

$ cat parse.sh

# Remove quotation marks via xargs trick -- xargs parses quoted strings
xargs -n 1 < data > /tmp/$$

while IFS="," read A B
do
        echo "A is $A"
        echo "B is $B"
done < /tmp/$$

rm -f /tmp/$$

$ ./parse.sh

A is admin@somedomain.com
B is #97ukjkl
A is agung@mydomain.com
B is 2adfpkjg

$
1 Like

thank's you sir,
it's work like charm,
here the code ;

#!/bin/bash
SUBJECT="Mailserver Baru"
ISIEMAIL="/tmp/emailmessage.txt"
xargs -n 1 < $1 > /tmp/$$

while IFS="," read A B
do
#        echo "A is $A"
#        echo "B is $B"
echo > $ISIEMAIL
EMAIL="$A"
echo "Dear $A" >> $ISIEMAIL
echo; echo >> $ISIEMAIL
        echo "Kami sedang mengupgrade system email server, password email $A yang baru adalah $B" >> $ISIEMAIL
        echo "Mohon segera mengganti password anda dengan mengakses alamat berikut" >> $ISIEMAIL
echo; echo >> $ISIEMAIL

/usr/bin/mail -s "$SUBJECT" "$EMAIL" < $ISIEMAIL
#echo > $ISIEMAIL 
done < /tmp/$$
rm -f /tmp/$$