0

I have the below command in my shell script to send all the four echo statements in one email

{
echo "Data Successfully loaded into LIP table"
echo "Total Items Purchased: `echo $QUERY1 | awk '{print $1}'`"
echo "Total Items MissingorMismatch: `echo $QUERY1 | awk '{print $2}'`"
echo "Error Percentage: $QUERY2" 
} | mailx -s "Report for $DATE_YEST_FORMAT1" -r rj@host.com user2@host.com

But when I see my email, I get the output like below which I don't want-

Data Successfully loaded into LIP table Total Items Purchased: 3956391 
Total Items MissingorMismatch: 975825 Error Percentage: 24.66452380464924

I need output something like below as line by line.

Data Successfully loaded into LIP table 
Total Items Purchased: 3956391 
Total Items MissingorMismatch: 975825 
Error Percentage: 24.66452380464924

Any suggestion why is it happening like this? I am running SunOS (Solaris).

Update:-

After trying the suggestion give by Kevin

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';"`


mailx -s "LIP Data Quality Report for $DATE_YEST_FORMAT1" -r rj@host.com rj@host.com uname@host.com <<EOF
Data Successfully loaded into LIP_DATA_QUALITY table

Total Items Purchased: $(echo $QUERY1 | awk '{print $1}')

Total Items MissingorMismatch: $(echo $QUERY1 | awk '{print $2}')

Error Percentage: $QUERY2
EOF

Output that I got in an email-

Data Successfully loaded into LIP_DATA_QUALITY table

Total Items Purchased: $(echo 3712928   393455 | awk '{print }')

Total Items MissingorMismatch: $(echo 3712928   393455 | awk '{print }')

Error Percentage: 10.596892802661404

Which is not right.. I should be getting output like this-

Data Successfully loaded into LIP_DATA_QUALITY table

Total Items Purchased: 3712928

Total Items MissingorMismatch: 393455

Error Percentage: 10.596892802661404

I am running SunOS

bash-3.00$ uname -a
SunOS lvsaishdc3in0001 5.10 Generic_142901-02 i86pc i386 i86pc
arsenal
  • 23,366
  • 85
  • 225
  • 331
  • FYI, you should use a heredoc for that input, rather than echoes like that. – Kevin Aug 11 '12 at 00:58
  • can you show me one example? As I am new to shell script, so whatever easy approach I found, I did it like that.. – arsenal Aug 11 '12 at 00:59
  • Try using `printf` instead of `echo` and add a `\n` at the end of your string, e.g `printf "Data Successfully loaded into LIP table\n"` – Ulrich Dangel Aug 11 '12 at 01:10
  • What's your mail reader? If it's Outlook, look for an option "remove extra whitespace" (or something like that) and turn it off. – glenn jackman Aug 11 '12 at 03:01

2 Answers2

2

Try to do it using \n as it is usually the standard escape sequence for a newline in a printing function., like so:

{
echo -e "Data Successfully loaded into LIP table\n"
echo -e "Total Items Purchased: `echo $QUERY1 | awk '{print $1}'\n"
echo -e "Total Items MissingorMismatch: `echo $QUERY1 | awk '{print $2}'\n"
echo -e "Error Percentage: $QUERY2\n" 
} | mailx -s "Report for $DATE_YEST_FORMAT1" -r rj@host.com user2@host.com

more info can be gotten here.

arsenal
  • 23,366
  • 85
  • 225
  • 331
Rivasa
  • 6,510
  • 3
  • 35
  • 64
  • This is not working.. I have already tried this. `\n` only doesn't works I guess. We need to add something more here. – arsenal Aug 11 '12 at 01:02
  • 1
    In order for `echo` to process escape codes like `\n`, you must use `echo -e`. This is a Bash extension and is not portable across shells. Using `printf` much more portable. – Barton Chittenden Aug 11 '12 at 01:14
2

It's possible that mailx ignores single newlines and separates paragraphs by an empty line, similar to tex and our own SO.

That combined with a heredoc

mailx -s "Report for $DATE_YEST_FORMAT1" -r rj@host.com user2@host.com <<EOF
Data Successfully loaded into LIP table

Total Items Purchased: `echo $QUERY1 | awk '{print $1}'`

Total Items MissingorMismatch: `echo $QUERY1 | awk '{print $2}'`

Error Percentage: $QUERY2
EOF

[It appears that your bash does not recognize the $(command) syntax, so I have removed that.]

[N.B. I just tested, and Solaris's mailx sent an email formatted precisely as I typed it. If your mail reader is displaying in HTML mode, however, it may collapse whitespace and show it all as one line.]

Kevin
  • 53,822
  • 15
  • 101
  • 132
  • Thanks Kevin, But when I tried this thing in my Shell Script I got the result like this- `Data Successfully loaded into LIP_DATA_QUALITY table Total Items Purchased: $(echo 3712928 393455 | awk '{print }') Total Items MissingorMismatch: $(echo 3712928 393455 | awk '{print }') Error Percentage: 10.596892802661404 ` Which is not right. If you see my Output in the question which is different than yours, As echo also got printed in an email. – arsenal Aug 11 '12 at 05:48
  • I also updated the question with the script and output I am getting. And what Output I should be getting. – arsenal Aug 11 '12 at 06:04
  • That's odd, mine's interpreting variables and subshells properly. What shell are you using? – Kevin Aug 11 '12 at 15:50
  • I am running `SunOS` and I am working bash shell. Updated the question as well with more details. – arsenal Aug 11 '12 at 17:49
  • bash 3 may not have `$(...)`, try it with the backticks. – Kevin Aug 11 '12 at 18:27
  • I was not able to understand. Can you edit your answer basis on what you said. As I am new to shell stuff.. So I am having hard time understanding the syntax.. – arsenal Aug 11 '12 at 18:39
  • Backticks are the subshell form you were using originally, see my update. – Kevin Aug 11 '12 at 18:48