Linux 将三个 echo 语句的输出通过管道传送到邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11875689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:02:20  来源:igfitidea点击:

Pipe the output from three echo statement to mail

linuxemailshellunixsolaris

提问by AKIWEB

echo "Total items: `echo $QUERY1 | awk '{print }'`"
echo "Total Error: `echo $QUERY1 | awk '{print }'`"
echo "Percentage: $QUERY2"

How can I send these three things in a single email using mail command. So the mail body should be like this below whenever I get any email, in each line there should be one echo statement-

如何使用mail command. 因此,每当我收到任何电子邮件时,邮件正文都应该如下所示,每一行都应该有一个 echo 语句-

Total items:-    Some Number
Total Error:-   Some Number
Percentage:-   Some Number

I am running SunOS

我在跑步 SunOS

bash-3.00$ uname -a 
SunOS lvsaishdc3in0001 5.10 Generic_142901-02 i86pc i386 i86pc 

采纳答案by shellter

Your requirement is not completely clear, but try this

你的要求不完全清楚,但试试这个

{
    echo "Total items: `echo $QUERY1 | awk '{print }'`"
    echo "Total Error: `echo $QUERY1 | awk '{print }'`"
    echo "Percentage: $QUERY2"
} | mail -s "subject" [email protected],[email protected]

The { .. }pair creates a process group, and all std-output is redirected into the 1 |(pipe), which connects to the std-in of your mail program.

{ .. }对创建了一个进程组,所有标准输出都被重定向到 1 |(管道),它连接到邮件程序的标准输入。

You may need to use mailx, -sspecifies subject, which I see from your other question on this topic that you seem to understand.

您可能需要使用mailx,-s指定主题,我从您似乎理解的关于此主题的其他问题中看到了这一点。

Also sendmailwill need to be running and properly configured for any mail to be delivered from the machine that you execute this script.

sendmail需要运行并正确配置从您执行此脚本的机器发送的任何邮件。

IHTH

IHTH



Edit: 2015-11-07

编辑:2015-11-07

Just got a 'nice answer' star for this, and on on review, I'm surprised that I didn't comment on excessive use of processes. For this case, this can be reduced to one call to awk, i.e.

刚刚为此获得了一个“不错的答案”星,并且在中,我很惊讶我没有评论过度使用流程。对于这种情况,这可以减少到一次调用awk,即

awk -v q1="$QUERY1" -v q2="$QUERY2" \
 'END {
    split(q1,q1arr)
    print "Total items: " q1arr[1] \
          "Total Error: " q1arr[2] \
          "Percentage: " q2
}' /dev/null \
| mail -s "subject" [email protected],[email protected]

Or for the one-liner crowd ;-), that is

或者对于单线人群;-),那就是

awk -v q1="$QUERY1" -v q2="$QUERY2" 'END {split(q1,q1arr);print "Total items: " q1arr[1] "\nTotal Error: " q1arr[2] "\nPercentage: " q2 }' /dev/null | mail -s "subject" [email protected],[email protected]

The { .. }aren't needed in this case, as there is only one process connecting to the pipe.

{ .. }不需要在这种情况下,只有一个连接到所述管的过程。

For a case like a summary report being sent once a day, the original code is completely usable (but non-optimal). However, coding non-optimally leads to bad habits. Calling 5 processes when one will suffice in a loop that runs 1000s of times in a day, will consume compute resources unnecessarily.

对于每天发送一次汇总报告之类的情况,原始代码完全可用(但不是最佳的)。然而,非最佳编码会导致坏习惯。在一天运行 1000 次的循环中调用 5 个进程就足够了,这将不必要地消耗计算资源。

Finally, as the o.p. didn't include any sample data, the code is only lightly tested.

最后,由于 op 不包含任何示例数据,因此代码仅经过轻微测试。

回答by Brian Carpio

Just create a function in bash and | (pipe) it to sendmail.

只需在 bash 和 | 中创建一个函数 (管道)它发送邮件。

            #!/bin/bash

            echo_statement(){

            echo "Total items: `echo $QUERY1 | awk '{print }'`"
            echo "Total Error: `echo $QUERY1 | awk '{print }'`"
            echo "Percentage: $QUERY2"

            }
            echo_statement | mail -s "subject" [email protected]