来自 shell 脚本的电子邮件中的列未对齐
Columns misaligned in email from shell script
我正在尝试创建一个脚本,以便在 unix FS 超过某个 %used 时提醒我。我已经成功创建了脚本并且能够触发对我的电子邮件的警报,但我一直在尝试格式化输出以使其易于阅读。下面是我的代码:
#!/bin/sh
#****************************************************************************************************************************
#Check all Filesystems and see how much space is being used
#****************************************************************************************************************************
set -x
# Load the .profile for the user
. /home/user/.profile
hostname=`uname -n`
Mail_ID="mail@mail.com"
subject="Check File Systems on $hostname"
dfoutput=/tmp2/user/logs/$hostname/dfoutput.txt
dfcleanoutput=/tmp2/user/logs/$hostname/dfcleanoutput.txt
badfilesizelog=/tmp2/user/logs/$hostname/badfs.txt
limit=70
rm $badfilesizelog
df -g | awk '{if ( != "Filesystem") print , , }' > $dfoutput
sed -e 's/%//' -e '/-/d' -e '/.*ora/d' -e '/.*var/d' -e '/.*usr/d' -e '/.*monitor/d' $dfoutput > $dfcleanoutput
cat $dfcleanoutput |
while read var1 var2 var3;
do
if [ $var2 -ge $limit ]
then
printf '%-90s %-50s %50s\n' $var1 $var2 $var3>> $badfilesizelog
fi
done
[ -f $badfilesizelog ] && cat $badfilesizelog | mail -s "$subject" $Mail_ID || echo "Do Nothing"
这是此脚本在电子邮件中的输出:
/dev/fs 70 /apps
host:/disk_test 75 /test_software
host:/schema_backup 71 /schema_backup
如您所见,它并没有像我希望的那样对齐所有内容:
/dev/fs 70 /apps
host:/disk_test 75 /test_software
host:/schema_backup 71 /schema_backup
如何才能使格式正常工作?我尝试了很多不同的方法,但总是遇到相同的对齐问题。
谢谢
我找到了一个使用 sendmail 而不是 mail 的有效解决方案,因为 mail 导致了我遇到的格式问题。以下是我使用的:
[ -f $badfilesizelog ] && awk ' BEGIN {
print "From: eaimgr@'$hostname'.com"
print "To: '$Mail_ID'"
print "MIME-Version: 1.0"
print "Content-Type: text/html"
print "Subject: Check File Systems on '$hostname'"
print "<html><body><table border=1 cellspacing=0 cellpadding=3>"
print "<tr>"
print "<td>Filesystem</td>";
print "<td>%Used</td>";
print "<td>Mounted On</td>";
print "</tr>"
} {
print "<tr>"
print "<td>""</td>";
print "<td>""</td>";
print "<td>""</td>";
print "</tr>"
} END {
print "</table></body></html>"
} ' $badfilesizelog | sendmail -t
我正在尝试创建一个脚本,以便在 unix FS 超过某个 %used 时提醒我。我已经成功创建了脚本并且能够触发对我的电子邮件的警报,但我一直在尝试格式化输出以使其易于阅读。下面是我的代码:
#!/bin/sh
#****************************************************************************************************************************
#Check all Filesystems and see how much space is being used
#****************************************************************************************************************************
set -x
# Load the .profile for the user
. /home/user/.profile
hostname=`uname -n`
Mail_ID="mail@mail.com"
subject="Check File Systems on $hostname"
dfoutput=/tmp2/user/logs/$hostname/dfoutput.txt
dfcleanoutput=/tmp2/user/logs/$hostname/dfcleanoutput.txt
badfilesizelog=/tmp2/user/logs/$hostname/badfs.txt
limit=70
rm $badfilesizelog
df -g | awk '{if ( != "Filesystem") print , , }' > $dfoutput
sed -e 's/%//' -e '/-/d' -e '/.*ora/d' -e '/.*var/d' -e '/.*usr/d' -e '/.*monitor/d' $dfoutput > $dfcleanoutput
cat $dfcleanoutput |
while read var1 var2 var3;
do
if [ $var2 -ge $limit ]
then
printf '%-90s %-50s %50s\n' $var1 $var2 $var3>> $badfilesizelog
fi
done
[ -f $badfilesizelog ] && cat $badfilesizelog | mail -s "$subject" $Mail_ID || echo "Do Nothing"
这是此脚本在电子邮件中的输出:
/dev/fs 70 /apps
host:/disk_test 75 /test_software
host:/schema_backup 71 /schema_backup
如您所见,它并没有像我希望的那样对齐所有内容:
/dev/fs 70 /apps
host:/disk_test 75 /test_software
host:/schema_backup 71 /schema_backup
如何才能使格式正常工作?我尝试了很多不同的方法,但总是遇到相同的对齐问题。
谢谢
我找到了一个使用 sendmail 而不是 mail 的有效解决方案,因为 mail 导致了我遇到的格式问题。以下是我使用的:
[ -f $badfilesizelog ] && awk ' BEGIN {
print "From: eaimgr@'$hostname'.com"
print "To: '$Mail_ID'"
print "MIME-Version: 1.0"
print "Content-Type: text/html"
print "Subject: Check File Systems on '$hostname'"
print "<html><body><table border=1 cellspacing=0 cellpadding=3>"
print "<tr>"
print "<td>Filesystem</td>";
print "<td>%Used</td>";
print "<td>Mounted On</td>";
print "</tr>"
} {
print "<tr>"
print "<td>""</td>";
print "<td>""</td>";
print "<td>""</td>";
print "</tr>"
} END {
print "</table></body></html>"
} ' $badfilesizelog | sendmail -t