Linux Basics for the Aspiring Hacker, Part 16 (Stdin, Stdout, & Stderror)
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.
Standard Output (1)
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 (0)
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.Standard Error (2)
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.Step 1List Two Directories
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).
- ls /etc/hosts /etc/snort
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
Step 2Send Standard Output to a File
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
- ls /etc/hosts /etc/aircrack-ng 2>erroroutput
Step 3Send Standard Output & Standard Error to Separate File
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
Step 4Send Both Standard Output & Standard Input to Same File
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
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!
Comments
Post a Comment