Pipelines: Difference between revisions

From XPUB & Lens-Based wiki
Line 21: Line 21:


== Redirecting ''stdin'' with < ==
== Redirecting ''stdin'' with < ==
wc -l
"Word count" program can be used to simply count the number of lines of a text file (with the -l option). When the above command is run, wc "listens to stdin" which is the console/keyboard. The program appears to do nothing and the shell "hangs" waiting for input. Type a few lines in such as...
testing
one
two
three
<CTRL-D>
Finally, on a blank line, pressing Ctrl-D tells the shell "END OF FILE" -- or stop reading input, and wc will snap into action and output the number of lines it read from stdin.
wc -l < mytextfile
Tells wc to use mytextfile as stdin and thus shows how many lines are in that file.


== Piping (stdin=>stdout) with | ==
== Piping (stdin=>stdout) with | ==

Revision as of 09:58, 20 February 2013

Philosophy of the commandline: Small tools that do one thing very well, loosely connected together to make custom "pipelines" or workflows to do specific (or surprising) things.

stdin and stdout

Every program receives "standard in", and sends its output to "standard out". By default, stdin is taken from the keyboard, and stdout will display something to the screen. These mappings can be adjusted however using redirection using the special pipeline characters '>', '<', and '|'.


Redirecting stdout with >

date

Displays the date to the screen (no stdin used by date).

date > time.txt

Redirects the output of date and "saves as" time.txt.

cat time.txt

Display time.txt (to the screen by default)

Redirecting stdin with <

wc -l

"Word count" program can be used to simply count the number of lines of a text file (with the -l option). When the above command is run, wc "listens to stdin" which is the console/keyboard. The program appears to do nothing and the shell "hangs" waiting for input. Type a few lines in such as...

testing
one
two
three
<CTRL-D>

Finally, on a blank line, pressing Ctrl-D tells the shell "END OF FILE" -- or stop reading input, and wc will snap into action and output the number of lines it read from stdin.

wc -l < mytextfile

Tells wc to use mytextfile as stdin and thus shows how many lines are in that file.

Piping (stdin=>stdout) with |