In: Computer Science
how to use python to read from stdin and print stderr in bash
During test execution any output sent to stdout and stderr is captured. If a test or a setup method fails its according captured output will usually be shown along with the failure traceback.
In addition, stdin is set to a “null” object which will fail on attempts to read from it because it is rarely desired to wait for interactive input when running automated tests.
Whenever you work with bash there are three file descriptors that are always in play and that you need to be aware of. These are, STDOUT, STDERR and STDIN which stand for standard output, standard error and standard input. Normally when programs are executed, unless they explicitly write output to a file, they will output to either standard output or standard error, which means the output will go to the terminal and you will see it on the screen (since both STDOUT and STDERR go to the screen by default). STDIN is where your program will get input data from if you prompt for input from the user. The user enters some data on the screen and your program or script will read it (I won’t say too much more about STDIN).
Of course STDOUT and STDERR are concepts, the actual file descriptors themselves are numbers. When you’re using bash, 1 will be the file descriptor that stands for STDOUT and 2 will be the file descriptor that stands for STDERR. So when you want to redirect the output of a program or script to a file rather than going to the screen you need to change where STDOUT or STDERR (or both) are pointing. To do this you can do the following:
ls -al 1>file1.txt 2>file2.txt
You use the > ‘operator’ to redirect where STDOUT and STDERR are
going to write their output by giving the > ‘operator’ a file
descriptor and the file you want the output to be written to. In
this case nothing will appear on the screen but two new files will
be created in the current directory, _file1.tx_t will have the
contents of STDOUT and file2.txt the contents of STDERR (of course
unless an error actually occurred file2.txt will be empty). If you
don’t specify any number .
example:-
ls -al > file1.txt
bash assumes you want to redirect STDOUT, so STDOUT will go to the
file you supply, while STDERR will still go to the screen.
But what if we want to redirect both standard output and standard error to the same file. This is easily possible and simply builds on the previous concept. we have seen the following construct before:
ls -al > file1.txt 2>&1
What this actually means is as follows. We first redirect standard
output to a particular file, when we don’t specify a
number, bash assumes we want to redirect STDOUT:
ls -al > file1.txt
The last bit of the command (2>&1) literally means:
(i)take STDERR (i.e. file descriptor 2)
(ii)and redirect it (i.e. > )
(iii)to the same place where STDOUT is pointing (&1)
The &1 in the last bit is a little tricky. You may think of the & as a sort-of reference and the 1 is of course STDOUT. So, &1 means – ‘the place that STDOUT is currently referencing’. Since we’ve previously redirected STDOUT to a file this will redirect STDERR to that same file. This is actually the old-school way of redirecting both STDOUT and STDERR to the same place. Otherwise , you simply need to do the following:
ls -al >& file1.txt
which will achieve the same result.