[Gllug] This morning's shell script question

Nix nix at esperi.org.uk
Fri May 25 09:31:23 UTC 2007

On 24 May 2007, Richard Jones verbalised:
>>From a shell script I want to run a command and sends the command's
> stderr to a pipe (to the logger command, in fact).  But leave
> stdin/stdout connected to the terminal as normal.

As an aside, if you want to filter from a process taht might die
spontaneously on you (e.g. sftp once you send it a quit command) you
might need more effort. I've done this in the past:

exec 3>&1

{ foo; echo "@@EXITCODE@@ $?" >&2; } 2>&1 1>&3 |\
while read -r ERRMSG; do
    if expr "$ERRMSG" : "@@EXITCODE@@" >/dev/null; then
        # we exited; stash the return code away, exit

The @@EXITCODE@@ trick is particularly useful because most things don't
mention that an orderly process termination of `foo' in this case will
even hit a shell block with a SIGPIPE and kill it, possibly before it
parses the last few lines of output. Follow `foo' with that exitcode
echo and you dodge the SIGPIPE and capture the return code as well. :)

Aiming for 10^10.
Gllug mailing list  -  Gllug at gllug.org.uk

More information about the GLLUG mailing list