将三个echo语句的输出传递给mail

时间:2020-11-27 14:08:29
echo "Total items: `echo $QUERY1 | awk '{print $1}'`"
echo "Total Error: `echo $QUERY1 | awk '{print $2}'`"
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命令在一封电子邮件中发送这三件事。因此,每当我收到任何电子邮件时,邮件正文应该如下所示,每行应该有一个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 

2 个解决方案

#1


16  

Your requirement is not completely clear, but try this

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

{
    echo "Total items: `echo $QUERY1 | awk '{print $1}'`"
    echo "Total Error: `echo $QUERY1 | awk '{print $2}'`"
    echo "Percentage: $QUERY2"
} | mail -s "subject" toUser1@xyz.com,toUser2@abc.com

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.

{..}对创建一个进程组,所有std-output重定向到1 | (管道),连接到邮件程序的std-in。

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

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

Also sendmail will 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" toUser1@xyz.com,toUser2@abc.com

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" toUser1@xyz.com,toUser2@abc.com

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.

最后,作为o.p.没有包含任何样本数据,代码只是轻微测试。

#2


1  

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

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

            #!/bin/bash

            echo_statement(){

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

            }
            echo_statement | mail -s "subject" you@yourdomain.com

#1


16  

Your requirement is not completely clear, but try this

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

{
    echo "Total items: `echo $QUERY1 | awk '{print $1}'`"
    echo "Total Error: `echo $QUERY1 | awk '{print $2}'`"
    echo "Percentage: $QUERY2"
} | mail -s "subject" toUser1@xyz.com,toUser2@abc.com

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.

{..}对创建一个进程组,所有std-output重定向到1 | (管道),连接到邮件程序的std-in。

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

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

Also sendmail will 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" toUser1@xyz.com,toUser2@abc.com

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" toUser1@xyz.com,toUser2@abc.com

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.

最后,作为o.p.没有包含任何样本数据,代码只是轻微测试。

#2


1  

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

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

            #!/bin/bash

            echo_statement(){

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

            }
            echo_statement | mail -s "subject" you@yourdomain.com