1

Linuxスクリプトでは、

メール関数を使用して配列値を一度に送信する方法はありますか?

function my_mail_function(){

# send array values
mail array_values_here "mymail@domain.tld" ;
}

ありがとうございました

4

2 に答える 2

3

ほんの少しの 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 のディストリビューションによって異なります。mailMailsendmail/usr/sbin//usr/libexec/

于 2012-11-07T15:13:53.883 に答える
2

適切な使用方法mailは次のとおりです。

mail -s "subject here" recipient1 recipient2 ...

このコマンドは標準入力から電​​子メールの本文を読み取るので、好きなようにフォーマットして、パイプ、ヒアドキュメント、ファイルなどから読み取ることができます。

function my_mail_function(){
    printf "%s\n" "${array_var[@]}" | mail -s "array values" mymail@domain.tld
}
于 2012-11-07T19:51:24.383 に答える