2017-04-05 90 views
0

我有表名为的学生mysql数据库。该表有名为的电子邮件列表从mysql使用shell一个接一个提取列值

我用下面的命令将电子邮件发送到特定学生,由PID鉴定:

$email=$(mysql --login-path=local --database=ccps -ss -e "select email from student where pid=132054"); 
echo "This is sample mail" | mail -s "Test" $email 

我想知道如何发送电子邮件到每个并在数据库中每一个学生。由于我不知道如何使用循环列电子邮件

谢谢。

+0

使用这样的邮件是非常丑陋的做法,你真的应该使用脚本语言来编写完整的电子邮件信息,但是如果你只是测试,那么这个技巧就是'xargs'。 – tadman

回答

0

我能想到的最直接的方法是:

for email in `mysql --login-path=local --database=ccps -ss -e "select email from student order by pid;"` 
do 
     echo "This is sample mail" | mail -s "Test" $email 
done 

如果您有没有碰到过他们面前,只是MySQL的前,然后在命令结束的引号是左而不是通常的单引号。正如前面的评论所言,像PHP这样的东西在构建更复杂的电子邮件方面会更好/更容易。

+0

非常感谢。它的工作就像一个魅力:) – Liston

1

为了游玩,像这样的东西会为你工作。但有更好的方法来做到这一点。

read -ra student_emails <<< $(mysql --login-path=local --database=ccps -ss \ 
            -e "select email from student order by pid;") 
for email in ${student_emails[@]} 
do 
    echo "This is sample mail" | mail -s "Test" ${email[0]} 
done 
+0

非常感谢你:) – Liston

相关问题