1

Below is my shell script that is working fine.

#!/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-

sh -x test.sh

Problem Statement:-

From the above shell script, I am getting below three things from the last three echo statements-

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?. 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:-

#!/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
arsenal
  • 23,366
  • 85
  • 225
  • 331
  • 2
    You missed the part where he used `{` and `}` to group the echo statements. – glenn jackman Aug 09 '12 at 03:31
  • What's with running the first query twice only to extract different output fields? – tripleee Aug 09 '12 at 03:42
  • @tripleee, what do you mean? I was not able to understand. Can you give some example what you saying? – arsenal Aug 09 '12 at 04:28
  • Avoid the backticks altogether, run `hive ... query 1 text ... | awk '{ print "Total Items Purchased" $1; print "Total Items Missing or Mismatch:" $2 }'`. Overall, huge overuse of backticks for the sole purpose of `echo`ing the output; the `echo`s are inelegant and in some cases problematic. – tripleee Aug 09 '12 at 09:06

1 Answers1

5

You typically use the /bin/mail program to send email from a shell script. You provide a subject and recipients on the command line and the message body on 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):

<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.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • Thanks larsks for the suggestion, but it has confused me more. Can you provide me the exact code for all your cases, then I can understand more as I am new to shell script. So I am very confuse in understanding the syntax and all other things. – arsenal Aug 09 '12 at 01:23
  • I updated the question with the script I tried as per your suggestion, but I got only last echo statement in an email. – arsenal Aug 09 '12 at 01:34
  • The sample code is missing a closing '}' char before the '|' char. See my answers to http://stackoverflow.com/users/819916/nevzz03 . Are you guys working on the same project or is this schoolwork? (If so you should tag it as such.) Else, please coordinate your questions, rather than asking the same thing multiple times. Good luck. – shellter Aug 09 '12 at 02:38
  • You can also use `read v1 v2 <<< $QUERY1` instead of two separate `echo|awk` calls. – glenn jackman Aug 09 '12 at 03:31
  • @glennjackman, I was not able to understand which part you are talking about? – arsenal Aug 09 '12 at 04:29
  • `read v1 v2 <<< $QUERY1; { echo "Total items purchased: $v1"; echo "Total Items MissingorMismatch: $v2"; echo "Error Percentage: $QUERY2"; } | mail -s "Test Data" DL-host@company.com` – glenn jackman Aug 09 '12 at 05:13