如何在bash脚本中标记std输出

  • 本文关键字:std 输出 脚本 bash bash
  • 更新时间 :
  • 英文 :


我有一个基本脚本,如下所示:

#!/bin/bash
### run these 2 commands and dump the contents to a file
run command 1 > /dir/file1
run command 2 > /dir/file2
##### run this command, echo the output and email me if $var is above a certain # and send the contents of the dump files in the email
var=$(netstat -an | wc -l)
echo $var
if [ "$var" -ge 700 ]; then
cat /dir/file1 /dir/file2 | mailx -s "System X has over $var connections. "    email recipients
fi

我需要做的是标记内容以区分这两个文件,这样当我收到电子邮件时,我就知道命令1和命令2的输出是什么。最好的方法是什么?

您可以滥用head:

$ cat file1
Contents of file 1
$ cat file2
Contents of file 2
$ head -n -0 file*                                                                                                                                                                                                                   
==> file1 <==
Contents of file 1
==> file2 <==
Contents of file 2

尝试:

{ echo "From command 1:"; cat /dir/file1; echo "From command 2:"; cat /dir/file2; } | mailx -s "System X has over $var connections." email recipients

最新更新