Hack Like a Pro: Linux Basics for the Aspiring Hacker, Part 16 (Stdin, Stdout, & Stderror)
Welcome back, my budding hackers!
In previous tutorials, we've looked at some of the basic commands and concepts for using Linux. Along the way, I realized that I've failed to provide you with some basic background material on the stdin, stdout, and stderror.
In human language, these are often referred to as standard input (stdin), standard output (stdout), and standard error (stderror). These represent how and where Linux sends the output from your commands (stdout), where it receives its input (stdin), and where it sendsits error messages (stderror).
Since you've now been using Linux for awhile, you realize that both standard output and standard error are sent to your computer screen. Both as the name implies, this is the standard place and not necessarily the only place. Linux let's us define where our output and error messages should go.
Before we go any further, let's take a moment to define some terms.
Whenever you complete a command, it must know where to send the output. You might want to send it to a file, a printer, the screen, etc. The default for standard output is the computer screen. Standard output is often referred to as stdout or simply use the numeric representation of 1.
Standard input is where the program or command gets the information it needs. By default, in Linux this is the keyboard, but can be a file, etc. Standard input is often referred to as stdin or simply represented by the numeric representation of 0.
When we make a mistake or our program throws an error, it send the error message to stanadard error. By default, this is our computer screen. Standard error is often referred to as stderror or simply represented by the numeral 2.
When we want to direct any of these three from the command line or a script, we use the numeric representation of each, 0 for stdin, 1 for stdout, and 2 for stderr.
To demonstrate how we can use and manipulation these I/O streams, let's do a listing of two different directories, /etc/hosts and /etc/snort.
In Linux, you can do listings of more than one directory at a time. The /etc/snort directory is where our configuration file for snort resides and /etc/hosts is a directory where we can set static name resolution in Linux (I'll do a new Linux tutorial on DNS and name resolution in Linux soon).
If we wanted to see the two directories, we could type:
- ls /etc/hosts /etc/snort
As you can see, the listing comes back to us by the standard output to our computer screen showing us the listing of both directories.
Now, let's try the same thing, but this time let's list a directory that doesn't exist, such as /etc/aircrack-ng.
- ls /etc/hosts /etc/aircrack-ng
As you can see, our BASH shell comes back with two outputs, the standard output from etc/hosts and the standard error from the non-existent directory.
Next, let's suppose that we want to separate our standard output from our standard error. Imagine we're running a script where we don't want to see our output messages until after the script has run, but we need to see error messages on our screen immediately. We could rewrite our command as:
- ls /etc/hosts /etc/aircrack-ng 1>goodoutput
Let's now imagine just the reverse of our previous scenario where instead we want to see our output on screen, but store our error messages to a separate file for viewing later. We could write:
- ls /etc/hosts /etc/aircrack-ng 2>erroroutput
Now, after the command has been run, we can go back and cat the erroroutput file to view any possible error messages.
Now, let's imagine a script where we want both our standard output and standard error to be directed to separate files for viewing later. We can type:
- ls /etc/hosts /etc/aircrack-ng 1>goodoutput 2>erroroutput
Notice that nothing comes back to our screen, neither standard output or standard error.
Finally, what if we wanted both standard error and standard output to be written to same file? We could type:
- ls /etc/hosts /etc/aircrack-ng >goodoutput 2>&1
Notice that I did not use the 1 before the >goodoutput as BASH defaults to stdout if no number is used.
Hope you enjoyed this quick lesson in Linux stdin, stdout, and stderror, but we have so much more coming, so keep coming back my budding hackers!