bash —How to view your own standard errors

Asked 2 years ago, Updated 2 years ago, 107 views

I'm looking for a way to see what the process has printed in its own standard error later.

It's the background.
There are commands mycmd, mycheck, and mysendmail.
The command "mycmd" executes the command "mycheck" and executes mysendmail if an error occurs.
"mysendmail" sends an email to the administrator using the standard input as the body.
The error log that "mycheck" prints should be printed in both the standard error of the caller "mycmd" and the body of the email.

Currently, I use process replacement to write as follows:

Command "mycmd":

mycheck2>>(teeerr.log>&2)
if [$?-ne0]; then
  mysendmail<err.log
  exit-1
fi

There is no problem with the operation, but the temporary file "err.log" is created.
I don't think it's smart enough.
Is there a way to meet specifications without creating temporary files?

If possible, it would be ideal to use a reference to your own standard error (for example, the contents of the standard error are recorded in the virtual file "STDERR_CACHE") to:

mycheck||{
  mysendmail<$STDERR_CACHE
  exit-1
}

bash

2022-09-30 19:27

1 Answers

How about storing it in the shell variable (STDERR_CACHE), using bash's here string(<<<) to enter mysendmail?

#!/bin/bash
        :

STDERR_CACHE="$(mycheck2>&1>/dev/null)"
exit_status=$?
echo "${STDERR_CACHE}" > 2

if [${exit_status}-ne0]; then
  mysendmail<<"${STDERR_CACHE}"
  exit-1
fi


2022-09-30 19:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.