简体   繁体   English

使用Shell脚本将电子邮件发送到电子邮件组

[英]Send an email to a email group using shell script

Below is my shell script that is working fine. 下面是我的shell脚本,可以正常工作。

#!/bin/bash

DATE_YEST_FORMAT2=`perl -e 'use POSIX qw(strftime); print strftime "%Y%m%d",localtime(time()- 3600*96);'`
echo $DATE_YEST_FORMAT2

QUERY1=`hive -e "
set mapred.job.queue.name=hdmi-technology;
SELECT SUM(total_items_purchased), SUM(total_items_missingormismatch) from lip_data_quality where dt='$DATE_YEST_FORMAT2';`

QUERY2=`hive -e "
set mapred.job.queue.name=hdmi-technology;
SELECT 100 * SUM(total_items_missingormismatch*1.0) / SUM(total_items_purchased) FROM lip_data_quality where dt='$DATE_YEST_FORMAT2';"`

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

I am running the above shell script like this below- 我正在像下面这样运行上面的shell脚本-

sh -x test.sh

Problem Statement:- 问题陈述:-

From the above shell script, I am getting below three things from the last three echo statements- 从上面的shell脚本中,我从最后三个echo语句得到以下三点:

Total items purchased
Total Items MissingorMismatch
Error Percentage

I need to send all these above three things in an email to our email group we have DL-host@company.com by using that shell script above or suppose if I have 10 email list to which I need to send email with the same contents and subjects, then I think, I can store all these 10 emails list in some variable and just read it from there and keep on sending, right?. 我需要使用上述shell script上述所有三件事通过电子邮件发送到我们拥有DL-host@company.com的电子邮件组,或者假设我有10 email list ,我需要向它们发送内容相同的电子邮件和主题,那么我想,我可以将所有这10个电子邮件列表存储在某个变量中,然后从那里读取并继续发送,对吗? Is this possible to do it? 这可能做到吗? I was thinking email structure like this below, just making very simple. 我在下面考虑这样的电子邮件结构,只是非常简单。

Subject 学科

Test Data 测试数据

Mail Body 邮件正文

Total items purchased:-    Some Number
Total Items MissingorMismatch:-   Some Number
Error Percentage:-   Some Number

Any thoughts will be appreciated. 任何想法将不胜感激。

Update, After trying larsks suggestions, I am getting only last echo statement in an email meaning only the error percentage one, not all three in a single email:- 更新,尝试了larsks建议之后,我在电子邮件中仅得到最后一个echo语句,这意味着错误百分比为一,而不是单个电子邮件中的所有错误百分比为:-

#!/bin/bash

DATE_YEST_FORMAT2=`perl -e 'use POSIX qw(strftime); print strftime "%Y%m%d",localtime(time()- 3600*96);'`
echo $DATE_YEST_FORMAT2

QUERY1=`hive -e "
set mapred.job.queue.name=hdmi-technology;
SELECT SUM(total_items_purchased), SUM(total_items_missingormismatch) from lip_data_quality where dt='$DATE_YEST_FORMAT2';`

QUERY2=`hive -e "
set mapred.job.queue.name=hdmi-technology;
SELECT 100 * SUM(total_items_missingormismatch*1.0) / SUM(total_items_purchased) FROM lip_data_quality where dt='$DATE_YEST_FORMAT2';"`

echo "Total items purchased: `echo $QUERY1 | awk '{print $1}'`"
echo "Total Items MissingorMismatch: `echo $QUERY1 | awk '{print $2}'`"
echo "Error Percentage: $QUERY2" | mail -s "Test Data" rj@host.com

You typically use the /bin/mail program to send email from a shell script. 通常,您使用/bin/mail程序从Shell脚本发送电子邮件。 You provide a subject and recipients on the command line and the message body on stdin . 您可以在命令行上提供主题和收件人,并在stdin上提供消息正文。 For example, inside your script you could do something like this: 例如,在脚本中,您可以执行以下操作:

{
echo "Total items purchased: `echo $QUERY1 | awk '{print $1}'`"
echo "Total Items MissingorMismatch: `echo $QUERY1 | awk '{print $2}'`"
echo "Error Percentage: $QUERY2"
} | mail -s "Test Data" DL-host@company.com

You can also pipe the output of an existing script into mail (if you don't want to modify the script, or if you only want to send mail somtimes): 您还可以将现有脚本的输出通过管道传递到mail (如果您不想修改脚本,或者只想发送邮件somtimes):

<your script> | mail -s "Test Data" DL-host@company.com

You can specify multiple recipients on the command line, so: 您可以在命令行上指定多个收件人,因此:

... | mail -s "Test Data" address1@company.com address2@company.com

and so forth. 等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM