Skip to content

Instantly share code, notes, and snippets.

@marek-saji
Last active November 21, 2019 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marek-saji/095f59628e6d8f1405599cc270e63df3 to your computer and use it in GitHub Desktop.
Save marek-saji/095f59628e6d8f1405599cc270e63df3 to your computer and use it in GitHub Desktop.
#!/bin/sh
foo ()
{
echo stdout
echo stderr 1>&2
return 1
}
{
{
{
# 1. Copy stdout to 3 and stderr to stdout (1)
thing 2>&1 1>&3
# 2. Print exit status to 4
echo $? 1>&4
# 3. Our stdout is original stderr, so we can capture error messages
} | tee ./errors
# 4. Copy 1 (originbal stderr) to back 2
# Copy 4 to 1, read it and produce exit status
} 4>&1 1>&2 | ( read -r STATUS; exit "$STATUS" )
# 5. Copy 3 (original stdout) back to 1
} 3>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment