0

我的 shell 脚本中有以下命令,可以在一封电子邮件中发送所有四个 echo 语句

{
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

但是当我看到我的电子邮件时,我得到了下面我不想要的输出 -

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

我需要逐行输出如下所示的内容。

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

任何建议为什么会这样?我正在跑步SunOS (Solaris)

更新:-

在尝试了给出的建议后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

我在电子邮件中收到的输出 -

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

这是不对的..我应该得到这样的输出-

Data Successfully loaded into LIP_DATA_QUALITY table

Total Items Purchased: 3712928

Total Items MissingorMismatch: 393455

Error Percentage: 10.596892802661404

我在跑步SunOS

bash-3.00$ uname -a
SunOS lvsaishdc3in0001 5.10 Generic_142901-02 i86pc i386 i86pc
4

2 回答 2

2

可能会mailx忽略单个换行符并用空行分隔段落,类似于tex我们自己的 SO。

结合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

[看来您的 bash 无法识别$(command)语法,因此我已将其删除。]

[注意,我刚刚测试过,Solarismailx发送的电子邮件格式与我输入的完全相同。但是,如果您的邮件阅读器以 HTML 模式显示,它可能会折叠空格并将其全部显示为一行。]

于 2012-08-11T01:53:55.007 回答
2

尝试使用它来执行此操作\n,因为它通常是打印函数中换行符的标准转义序列。,如下所示:

{
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

更多信息可以在这里获得。

于 2012-08-11T00:59:29.400 回答