Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cwgreene/75f69e36bc67e0fa93d855e748a400de to your computer and use it in GitHub Desktop.
Save cwgreene/75f69e36bc67e0fa93d855e748a400de to your computer and use it in GitHub Desktop.
# Let's set the terminal to terminate scripts on
# the first error of a script
PS /home/chris/test/stderrtest> $ErrorActionPreference='Stop'
# So, let's take a look at a simple c program.
PS /home/chris/test/stderrtest> cat ./test.c
#include <stdio.h>
int main() {
fprintf(stderr, "message 1\n");
fprintf(stderr, "message 2\n");
}
# Output of program
PS /home/chris/test/stderrtest> ./a.out
message 1
message 2
# Let's redirect the output from stderr to stdout
PS /home/chris/test/stderrtest> ./a.out 2>&1
message 1
# What happened to message 2?
# Let's redirect stderr to a file
PS /home/chris/test/stderrtest> ./a.out 2> doom
message 1
# What? Why did we see message 1?
# Let's look at the file
PS /home/chris/test/stderrtest> wc -c doom
0 doom
# It's empty!
# Let's get rid of the error preference
PS /home/chris/test/stderrtest> $ErrorActionPreference='Continue'
# Redirect the file output
PS /home/chris/test/stderrtest> ./a.out 2> doom
# view contents
PS /home/chris/test/stderrtest> cat doom
message 1
message 2
# Works as expected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment