OPS145 Lab 7 Newversion: Difference between revisions
(→STDIN) |
|||
Line 11: | Line 11: | ||
The file descriptor numbers mostly won't matter to you in this course, but they're good to know for the future. You'll remember them if you just memorize STDIN, STDOUT, STDERR, and recall that the count starts from zero. | The file descriptor numbers mostly won't matter to you in this course, but they're good to know for the future. You'll remember them if you just memorize STDIN, STDOUT, STDERR, and recall that the count starts from zero. | ||
[[File:DefaultSTDIO.png|center|thumb|800x800px|Default standard input/ouput]] | |||
== STDOUT == | == STDOUT == | ||
Line 33: | Line 34: | ||
* New commands: grep, head, tail, wc | * New commands: grep, head, tail, wc | ||
* Redirect STDOUT to file | * Redirect STDOUT to file | ||
* Redirect STDERR to file | * Redirect STDERR to file |
Revision as of 18:21, 2 March 2024
Input and Output
You need to study system programming to fully understand how input and output works, but without being a programmer you can still learn most of what you need to control input and output.
The introduction in this lab will probably be a little dry, but hopefuly it will start making more sense when you get to apply these concepts later in the lab.
Each application on a POSIX-compatible system (such as Linux) is always started with three data pipes attached to it. These are:
- Standard input (STDIN, file descriptor 0)
- Standard output (STDOUT, file descriptor 1)
- Standard error (STDERR, file descriptor 2)
The file descriptor numbers mostly won't matter to you in this course, but they're good to know for the future. You'll remember them if you just memorize STDIN, STDOUT, STDERR, and recall that the count starts from zero.
STDOUT
Standard output is the most straightforward to understand. Whatever normal output a command-line prints goes out through the standard output pipe.
By default the standard output pipe is connected to the terminal, and anything that goes into that pipe comes out as readable output on the screen.
STDERR
This is where things get complicated. The application can send output into STDOUT or STDERR. Both these pipes are connected to the terminal by default, and both outputs will look the same to the user. But they are different pipes - which means they are controlled separately.
One extra confusing factor is the definition of "error". Sometimes it's obvious what constitutes an error, but often it isn't.
The programmer who wrote the application had to make the decision which sort of output to send to STDOUT and which to send to STDERR. You can try and make an educated guess, but you'll have to confirm by experimenting.
STDIN
Standard input is only tricky until you become familiar with it. STDIN is also a data pipe, but by default instead of the pipe's input coming from the program and the output going to the terminal: the pipe's input comes from the terminal and the output goes into the program.
Up to this point in the course you haven't used STDIN, but it's used all the time and now is the time to learn about it.
Also:
- New commands: grep, head, tail, wc
- Redirect STDOUT to file
- Redirect STDERR to file
- Redirect STDERR to /dev/null
- Many commands can read from STDIN. E.g.: cat, less, tar, grep, head, tail
- The STDOUT from one command can be piped to the STDIN of another