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

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

关于bash - 如何在 bash 脚本中标记 std 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34399579/

相关文章:

c - 如何禁止可执行文件读取/修改给定目录以外的任何文件?

linux - Bash 仅打印标准错误,而不是标准输出

bash - 如果用户错误地调用脚本,使 Bash 脚本退出并打印错误消息

bash - 如何使用 fzf 只搜索目录?

linux - 如何在 shell 中创建别名以转到父目录

linux - 通过命令 shell 脚本使用 tshark 进行转换

bash - 通过 cron 运行脚本与手动执行的结果不同

ruby-on-rails - ‘ bash ’: no such file or directory

linux - "No such file or directory"在 bash 的 while 读取循环中使用 cut

linux - 检查已安装的软件包,如果没有找到则安装