bash在电子邮件进程中丢失了printf格式

时间:2015-01-12 16:52:56

标签: bash format

所以我必须在早上查看一系列的竞赛时间。 大多数情况下,我只需要将文件拖尾-1然后检查时间。我自动化了 进入一个不错的小报告。

PROCESS            DATE
============================================
topher_time                06:55.192Z
big_sync_morning_time      07:15:45
etime_prod                 07:46.952Z
database_time              08:08:49
trigger                    08:38:13,076
trans_database_time        08:55:33

问题是当我尝试将报告邮寄给小组时 - 它就全部出来了 弄乱。 我想也许是因为每个人都使用html作为编辑/读者 电子邮件,所以我在它周围放了一些电子邮件标签 - 但它仍然搞砸了。 报告在终端上格式很好,所以我可以剪切和粘贴 报告到电子邮件 - 但是当我尝试通过电子邮件发送程序中的报告时 格式化是一团糟。

#!/bin/bash
divider=======================================================
totalwidth=44
header="\n %-18s %-16s \n"
format=" %-18s   %-16s \n"

topher=$(tail -1 /come/and/play/topher.log| ~/walt/convert_gm_est )
big_sync_morning=$(tail -1 /come/and/play/big_sync-morning.log|~/walt/convert_gm_est )
sub_shell=$(tail -1 /come/and/play/trainer.log|~/walt/convert_gm_est )
database=$(tail -1 /come/and/play/with/us/danny/database.log)
trans_c4=$(tail -1 /come/and/play/with/us/danny/regular.sys.log)
trans_database=$(tail -1 /come/and/play/with/us/danny/database.stdout|~/walt/convert_gm_est )


topher_time=$(printf "%s " $topher | awk '{print $1}')
big_sync_morning_time=$(printf "%s " ${big_sync_morning} | awk '{print $1}')
etime_prod=$(printf "%s " ${sub_shell}| awk '{print $1}')
database_time=$(printf "%s " $database | awk '{print $5}')
triggertime=$(printf "%s " $trans_c4 | awk '{print $1}')
trans_database_time=$(printf "%s " $trans_database | awk '{print $5}')

mail -s "morning batch completion times" "capser@casper.com" << END_MAIL
echo "<html>"
echo "<body>"
echo "<pre>"
printf "$header" PROCESS DATE
printf "%$totalwidth.${totalwidth}s\n" "$divider"
printf "$format" \
topher "$topher_time" \
big_sync_morning "$big_sync_morning_time" \
etime_prod "$etime_prod" \
database "$database_time" \
trans_c4 "$triggertime" \
trans_database "$trans_database_time"

printf "%s\n"
echo "</pre>"
echo "</body>"
echo "</html>"

END_MAIL

这是从命令行运行的过程。

PROCESS            DATE
============================================
topher_time                06:55.192Z
big_sync_morning_time      07:15:45
etime_prod                 07:46.952Z
database_time              08:08:49
trigger                    08:38:13,076
trans_database_time        08:55:33

这是邮寄的报告 - 这就是电子邮件中的样子。

echo "<html>"
echo "<body>"
echo "<pre>"
printf "\n %-18s %-16s \n" PROCESS DATE
printf "%44.44s\n" "======================================================"
printf " %-18s   %-16s \n" topher_time "06:55.192Z" big_sync_morning_time "" etime_prod "07:46.952Z" database_time "08:08:49" trigger "08:38:13,076" trans_database_time "08:55:33"
printf "%s\n"
echo "</pre>"
echo "</body>"
echo "</html>"

更新: 围绕{}中的命令,然后将它们输送到邮件命令工作 - 有点 对命令进行分组不会保留printf格式。 还有另一种方法可以确保格式化吗?

PROCESS            DATE
============================================
topher_time     06:55.192Z
big_sync_morning_time  07:15:45
etime_prod        07:46.952Z
database_time     08:08:49
trigger           08:38:13,076
trans_database_time   08:55:33

1 个答案:

答案 0 :(得分:3)

您会注意到它只是展开并打印了您的命令而不是执行它们。要改为输入一系列命令的结果,请使用命令组:

{
  echo "<html>"
  echo "<body>"
  echo "<pre>"
  printf "$header" PROCESS DATE
  ...
} | mail -s "morning batch completion times" "capser@casper.com"