我有一个如下所示的基本脚本:
#!/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
关于bash - 如何在 bash 脚本中标记 std 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34399579/