2012-11-07 68 views
1

in linux scripting,如何通过邮件发送数组值

有没有办法使用mail函数来一次发送数组值

function my_mail_function(){ 

# send array values 
mail array_values_here "[email protected]" ; 
} 

谢谢

+2

[你有什么试过](http://whathaveyoutried.com/)?和[你真的想做什么](http://mywiki.wooledge.org/XyProblem)? – ghoti

回答

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 <[email protected]>" 
    echo "From: Julio Fong <[email protected]>" 
    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 [email protected] [email protected] 

注意,这将是更安全的使用sendmail,而不是直接依靠mailMail命令的可用性和配置。您的sendmail二进制文件可能与我的地方不在同一个地方;如果/usr/sbin/不适用于您,请检查/usr/libexec/。这取决于你正在运行的Linux的分布。

+0

完美,谢谢;) –

2

使用mail正确的方法是:

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

命令从标准输入读取电子邮件的主体,所以你可以格式化你怎么样从管道或这里-DOC或文件中读入或...

function my_mail_function(){ 
    printf "%s\n" "${array_var[@]}" | mail -s "array values" [email protected] 
}