0

(Firstly I've been looking for an hour so I'm pretty sure this isn't a repeat)

I need to write a script that executes 1 command, 1 time, and then does the following:

  1. Saves both the stdout and stderr to a file (while maintaining their proper order)
  2. Saves stderr only to a variable.

Elaboration on point 1, if I have a file like so

echo "one"
thisisanerrror
echo "two"
thisisanotherError

I should expect to see output, followed by error, followed by output, followed by more error (thus concatenating is not sufficient).

The closest I've come is the following, which seems to corrupt the log file:

errs=`((./someCommand.sh 2>&1 1>&3) | tee /dev/stderr ) 3>file.log 2>&3 `
Anfurny
  • 134
  • 13
  • (./test.sh 2> >(tee /dev/fd/3 > /dev/fd/2)) 3>&1 2>error.log 1>&2 Also seems to work on a mac, but causes corruption on a linux box – Anfurny Sep 11 '12 at 01:00

1 Answers1

1

This might be a starting point:

How do I write stderr to a file while using "tee" with a pipe?

Edit:

This seems to work:

((./foo.sh) 2> >(tee >(cat) >&2)) > foo.log

Split stderr with tee, write one copy to stdout (cat) and write the other to stderr. Afterwards you can grab all the stdout and write it to a file.

Edit: to store the output in a variable

varx=`((./foo.sh) 2> >(tee >(cat) >&2))`

I also saw the command enclosed in additional double quotes, but i have no clue what that might be good for.

Community
  • 1
  • 1
romedius
  • 775
  • 6
  • 20
  • Remember I want stderr to end up in a variable. And I don't want to have to read it out of a file because this introduces a failure point if multiple copies are run at once. – Anfurny Sep 11 '12 at 17:16
  • Okay, but how do I do both of those with one execute (this is a script I can't run twice) ? The second example doesn't seem to write to a file. – Anfurny Sep 14 '12 at 21:59