Linuxスクリプトでは、
メール関数を使用して配列値を一度に送信する方法はありますか?
function my_mail_function(){
# send array values
mail array_values_here "mymail@domain.tld" ;
}
ありがとうございました
ほんの少しの bash コードで配列をステップ実行できます。
#!/bin/bash
# Here's a simple array...
a=(one two three)
# The brackets encapsulate multiple commands to feed to the stdin of sendmail
(
echo "To: Mister Target <target@example.com>"
echo "From: Julio Fong <jf@example.net>"
echo "Subject: Important message!"
echo ""
count=1
for item in ${a[@]}; do
printf "Value %d is %s\n" "$count" "$item"
((count++))
done
echo ""
) | /usr/sbin/sendmail -oi -fjf@example.net target@example.com
またはコマンドsendmail
の可用性と構成に依存するよりも、直接使用する方が安全であることに注意してください。あなたのバイナリは私のものと同じ場所にないかもしれません。うまくいかない場合は、 を確認してください。実行している Linux のディストリビューションによって異なります。mail
Mail
sendmail
/usr/sbin/
/usr/libexec/
適切な使用方法mail
は次のとおりです。
mail -s "subject here" recipient1 recipient2 ...
このコマンドは標準入力から電子メールの本文を読み取るので、好きなようにフォーマットして、パイプ、ヒアドキュメント、ファイルなどから読み取ることができます。
function my_mail_function(){
printf "%s\n" "${array_var[@]}" | mail -s "array values" mymail@domain.tld
}