shell脚本中的新行

时间:2012-08-11 00:52:11

标签: linux shell unix solaris sunos

我在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

2 个答案:

答案 0 :(得分: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

可以获得更多信息here

答案 1 :(得分: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)语法,因此我已将其删除。]

[N.B。我刚刚测试过,Solaris的mailx发送的电子邮件格式正如我输入的那样。但是,如果您的邮件阅读器以HTML模式显示,它可能会折叠空白并将其全部显示为一行。]