Search code examples
linuxshellunixsolarissunos

new line in shell script


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

Solution

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