new line in shell script

arsenal picture arsenal · Aug 11, 2012 · Viewed 8.4k times · Source

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 [email protected] [email protected]

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 [email protected] [email protected] [email protected] <<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

Answer

Rivasa picture Rivasa · Aug 11, 2012

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 [email protected] [email protected]

more info can be gotten here.