2017-04-05 22 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

相關問題