2

I have a basic script that looks like this:

#!/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

What I need to do is label the contents to distinguish between the 2 files so when I get the email I know what output came from command 1 and what came from command 2. What is the best way to do this?

user53029
  • 675
  • 1
  • 8
  • 23

2 Answers2

3

You can abuse 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
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
1

Try:

{ echo "From command 1:"; cat /dir/file1; echo "From command 2:"; cat /dir/file2; } | mailx -s "System X has over $var connections." email recipients
jianweichuah
  • 1,417
  • 1
  • 11
  • 22
  • Don't you need some braces? `{ echo "From command 1:"; cat file1; echo "From command 2:"; cat file2; } | mailx ...` -- otherwise, the first three commands won't be included in the pipeline. – Charles Duffy Dec 21 '15 at 16:20
  • @CharlesDuffy Right, good catch :) – jianweichuah Dec 21 '15 at 16:22
  • 2
    The `;` after the last `cat` is actually needed if you're putting the `}` on the same line; otherwise the `}` is treated as an argument to `cat`. The spaces might be mandatory too, I'd have to check. – Charles Duffy Dec 21 '15 at 16:24
  • 2
    [Tested: Spaces are actually mandatory on both sides of the `{` and `}` characters]. – Charles Duffy Dec 21 '15 at 16:27
  • `$ {echo "123"; echo "hello"; echo "abc"; echo "hello"} | grep hello >> hello >> hello` @CharlesDuffy I don't think the spaces and `;` are needed. – jianweichuah Dec 21 '15 at 16:28
  • Thanks guys. Works perfectly! – user53029 Dec 21 '15 at 16:28
  • 1
    @jianweichuah, which shell? I tested against bash 3.2.57(1)-release and they're certainly required there. – Charles Duffy Dec 21 '15 at 16:30
  • @jianweichuah, also, if you read the POSIX sh standard, `{` and `}` are defined as reserved words -- and something that has no spaces between it and other text isn't a standalone word. – Charles Duffy Dec 21 '15 at 16:31
  • @jianweichuah, ...to quote: "This recognition shall only occur when none of the characters is quoted and when the word is used as: - The first word of a command - The first word following one of the reserved words other than case, for, or in - The third word in a case command (only in is valid in this case) - The third word in a for command (only in and do are valid in this case)" – Charles Duffy Dec 21 '15 at 16:32
  • @CharlesDuffy what do you get when you run the above command? – jianweichuah Dec 21 '15 at 16:33
  • ...and, from the grammar specification from the POSIX sh standard: `/* These are reserved words, not operator tokens, and are recognized when reserved words are recognized. */` – Charles Duffy Dec 21 '15 at 16:33
  • + '{echo' 123 -bash: {echo: command not found – Charles Duffy Dec 21 '15 at 16:33
  • ...when running it with `set -x`. – Charles Duffy Dec 21 '15 at 16:34
  • Perhaps your shell might be zsh? zsh *does* actually treat `{` and `}` as operator tokens, defying the POSIX sh standard on that point (among others). – Charles Duffy Dec 21 '15 at 16:35
  • *nod* -- that's a `zsh`ism; no POSIX-compliant shell behaves that way. Might I ask that you correct your answer to be portable, or declare its portability limitations explicitly? – Charles Duffy Dec 21 '15 at 16:36
  • Alright, sorry about that. Since this question is tagged with `bash`. I'll update the answer. Thanks for pointing it out! – jianweichuah Dec 21 '15 at 16:37
  • Incidentally, this is very much the reason I gave up zsh myself ~12 years ago -- realized that its deviations from the standard were making me sloppy when writing code for other shells. – Charles Duffy Dec 21 '15 at 16:41
  • Hahaha, I shall look into that ;) – jianweichuah Dec 21 '15 at 16:42
  • I think this link will be useful for understanding bash commands grouping: `https://www.gnu.org/software/bash/manual/html_node/Command-Grouping.html` – Damir Dec 28 '22 at 22:59