格式化问题

维杰

我有一个类似下面的小脚本,如果出现问题,输出将在屏幕上突出显示(红色,黄色等)

normal=$(tput sgr0)
red=$(tput setaf 1)
yellow=$(tput setaf 3)

df -h >/dev/null 2>&1 &
xx_pid=$!
sleep 3
if [ `ps -ef| grep $xx_pid | grep -v grep | wc -l` -gt 0 ]; then
kill $xx_pid > /dev/null 2>&1
 printf "%-70s %s\n" "${red}df -h response : "  "  ......... taking more  than 30 Seconds to complete, exiting script${normal}" | tee -a $log
 exit
else
 printf "%-70s %s\n" "df -h response : "  " ......... NORMAL (Completed in  less than 30sec)" | tee -a $log
 fi

但是在日志文件中,我看到如下垃圾字符([[31m和(B [m)

[31mdf -h response :                    ......... taking more than 30 Seconds to complete, exiting script(B[m

有什么方法可以避免那些垃圾字符而无需写入日志文件。

托马斯·迪基

有没有必要使用tee:您可以将您的信息给一个变量,printf 用颜色,然后将消息追加到日志文件:

 msg=$(printf "%-70s %s" "df -h response : "  " ......... NORMAL (Completed in >
 printf "%s%s%s\n" "${red}" "$msg" "${normal}"
 printf "%s\n" "$msg" >>$log

而不是

 printf "%-70s %s\n" "${red}df -h response : "  "  ......... taking more  than 30 Seconds to complete, exiting script${normal}" | tee -a $log

问题tee在于它只能写入标准输出。可以使用脚本来分别重定向标准输出,但这很麻烦:

 ( printf "%-70s %s\n" "df -h response : "  "  ......... taking more  than 30 Seconds to complete, exiting script" | tee -a $log ) | sed -e "s/^/${red}/" -e "s/$/${normal}/"

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章