Stdout and Stderr
Introduction
If you do not want to loose your work, you will want to put your results into one or more files. However, you would want the errors and warnings to stay in a different file from the expected outputs (i.e. the results). This is easily done by understanding the concept of Standard Output (stdout
) and Standard Error (stderr
).
Using the stderr and stdout
Maybe the easiest way to understand what the stderr and stdout is by showing an example of how to print to them from an Emp script.
Emp provides one function for warning, and Lua comes with a built-in function for printing to the stdout and one for raising an error. See, for example, the script below:
As you can see, this script starts by warning the user that it does not really do anything useful. However, if the user wants to ignore that warning, it is his problem (although unexpected things may happen). Then, the program does something else: prints something to the stdout. Finally, the script decides to stop running, and the final part of it does not run.
By running this using the following command
You get
As you can see, the results and errors and warnings are all together. The next section of this tutorial explains how to separate these.
Note that errors and warnings often show the line of the script in which the error is caused.
Directing the stdout to a file
With this purpose, we use the '>'
operator. Such operator tells the program to "not write the stdout to the console, but to a file"
Check the results.txt
file to see what happened.
Directing the stderr to a file
With this purpose, we use the '2>'
operator. Such operator tells the program to "not write the stderr to the console, but to a file"
Some other useful tips
You should know that you can use both operators together
Also, find out what the >>
and 2>>
operators do (you can google this)
Conclusions
Controlling the stdout and the stderr allows classifying warnings and errors from expected results and messages printed during a program's execution.
This concept needs to be understood by both the users and the writers of scripts. Users can use them to work faster and easier; and programmers, to design proper script that warn, stop and print results in a correct way.
Last updated