0

in a Bash script I need to execute a command.

I also need to catch its output to let the script send it to me via e-mail.

In the e-mail I want to print the entire output of the command, and I wish to highlight the error strings, preserving the order of the messages in the same order the command prints out.

For example, if the command prints out the following output:

180321 15:52:02 command: message 1
180321 15:52:03 command: message 2
180321 15:52:04 command: error 1
180321 15:52:05 command: error 2
180321 15:52:06 command: message 3
180321 15:52:07 command: message 4
180321 15:52:08 command: error 3

I want to receive an e-mail with a specific subject (which tells me that we had an error) and with this body:

180321 15:52:02 command: message 1

180321 15:52:03 command: message 2

180321 15:52:04 command: error 1

180321 15:52:05 command: error 2

180321 15:52:06 command: message 3

180321 15:52:07 command: message 4

180321 15:52:08 command: error 3

Could you help me to find out how I could make this?

Thank you!

Mat
  • 586
  • 2
  • 10
  • 25
  • 1
    Can't be done without working with a syscall-level trace. The only way an order *exists at all* is if they're both copies of the same file descriptor, and if they're copies of the same descriptor, you can't tell from outside the process which one was written to at any given time. – Charles Duffy Mar 21 '18 at 15:38
  • 1
    Over on [unix.se], see also [Can I configure my shell to print stdout and stderr in different colors?](https://unix.stackexchange.com/questions/12439/can-i-configure-my-shell-to-print-stderr-and-stdout-in-different-colors), which has an answer much less inaccurate than many of those on the related questions here. – Charles Duffy Mar 21 '18 at 15:42
  • ...so, if you have a hard requirement to capture this from unmodified software without the huge performance impact that `strace -f` has, I'd consider working with [Sysdig](https://sysdig.com/opensource/) to be able to get a solid answer to "which writes happen to which file descriptors in which order?". That said, note the comment there with [an LD_PRELOAD library](https://github.com/sickill/stderred) that will hook into writes to stderr via libc functions and modify them (it's cheating -- not "unmodified software" -- but if it gets what you need...). – Charles Duffy Mar 21 '18 at 15:44
  • Thanks! And sorry for the duplicate – Mat Mar 21 '18 at 15:46

0 Answers0