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

时间:2015-12-21 15:54:13

标签: 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

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

2 个答案:

答案 0 :(得分:3)

您可以滥用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

答案 1 :(得分:1)

尝试:

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