Reputation: 3904
This is just a little script to add a user to an e-mail MySQL database, assign an alias to him if an argument is defined, send a welcome email to the user and send e-mails to everybody else notifying about the newcomer. But everytime I try to run the script, I only get the message UNEXPECTED END OF FILE
. Here's the script:
#! /bin/bash
USER=$1
SECTOR=$2
if [ -z "${SECTOR}" ];
then
mysql --host=localhost --user=verysecretdatabaseuser --password=verysecretdatabasepass mydatabase << EOF
insert into users (email,name,uid,gid,homedir,maildir,crypt,force_change_pwd) values("${USER}@example.com.br","${USER}",1001,1001, "/var/mail/${USER}/","/var/mail/${USER}/.maildir/",ENCRYPT("verysecretdefaultpass"), "yes" );
EOF
else
mysql --host=localhost --user=verysecretdatabaseuser --password=verysecretdatabasepass mydatabase << EOF
insert into users(email,name,uid,gid,homedir,maildir,crypt,force_change_pwd) values("${USER}@example.com.br","${USER}",1001,1001,"/var/mail/${USER}/","/var/mail/${USER}/.maildir/",ENCRYPT("verysecretdefaultpass"), "yes" ); EOF
insert into alias(alias,destination) values ("${SECTOR}@example.com.br", "${USER}@example.com.br");
EOF
fi
# send e-mail so my postfix can create the mail directories
/usr/sbin/sendmail -f [email protected] "${USER}@example.com.br" << EOF
Subject: Bem Vindo!
Seja bem vindo ao Servidor de e-mails da Empresa! Com muito orgulho hospedamos nosso e-mail em nosso próprio servidor. Em caso de dúvidas contacte <[email protected]>
.
EOF
# fetching everyone already in the database
users=`mysql --user=myverysecretuser --password=myverysecretpass -s -N -e "SELECT myverysecretfield FROM myverysecretdatabase.myverysecretusertable"`
#sending an e-mail to each user
for user in $users
do
echo "${user}"
/usr/sbin/sendmail -f [email protected] "${user}" << EOF
Subject: Funcionario novo!
Prezados, deem as boas vindas ao funcionario novo "${USER}" que acabou de entrar no departamento de "${SECTOR}". Para se comunicar com o funcionario novo, basta enviar e-mails para: "${USER}@example.com.br". O funcionario novo tambem recebera e-mails que forem enviados para "${SECTOR}@example.com.br".
.
EOF
done
What am I doing wrong?
Upvotes: 0
Views: 899
Reputation: 185179
Try this :
#! /bin/bash
USER=$1
SECTOR=$2
if [ -z "${SECTOR}" ];
then
mysql --host=localhost --user=verysecretdatabaseuser --password=verysecretdatabasepass mydatabase << EOF
insert into users (email,name,uid,gid,homedir,maildir,crypt,force_change_pwd) values("${USER}@example.com.br","${USER}",1001,1001, "/var/mail/${USER}/","/var/mail/${USER}/.maildir/",ENCRYPT("verysecretdefaultpass"), "yes" );
EOF
else
mysql --host=localhost --user=verysecretdatabaseuser --password=verysecretdatabasepass mydatabase << EOF
insert into users(email,name,uid,gid,homedir,maildir,crypt,force_change_pwd) values("${USER}@example.com.br","${USER}",1001,1001,"/var/mail/${USER}/","/var/mail/${USER}/.maildir/",ENCRYPT("verysecretdefaultpass"), "yes" ); EOF
insert into alias(alias,destination) values ("${SECTOR}@example.com.br", "${USER}@example.com.br");
EOF
fi
# send e-mail so my postfix can create the mail directories
/usr/sbin/sendmail -f [email protected] "${USER}@example.com.br" << EOF
Subject: Bem Vindo!
Seja bem vindo ao Servidor de e-mails da Empresa! Com muito orgulho hospedamos nosso e-mail em nosso próprio servidor. Em caso de dúvidas contacte <[email protected]>
.
EOF
# fetching everyone already in the database
users=`mysql --user=myverysecretuser --password=myverysecretpass -s -N -e "SELECT myverysecretfield FROM myverysecretdatabase.myverysecretusertable"`
#sending an e-mail to each user
for user in $users
do
echo "${user}"
/usr/sbin/sendmail -f [email protected] "${user}" << EOF
Subject: Funcionario novo!
Prezados, deem as boas vindas ao funcionario novo "${USER}" que acabou de entrar no departamento de "${SECTOR}". Para se comunicar com o funcionario novo, basta enviar e-mails para: "${USER}@example.com.br". O funcionario novo tambem recebera e-mails que forem enviados para "${SECTOR}@example.com.br".
.
EOF
done
The closing EOF
must be at the beginning of the line.
You can test your script in dry-run
mode with :
bash -n script.sh
Upvotes: 3