{"id":34,"date":"2022-03-04T15:11:16","date_gmt":"2022-03-04T20:11:16","guid":{"rendered":"https:\/\/pressbooks.library.ryerson.ca\/opsyshiraki\/chapter\/lecture-2-part-2-2\/"},"modified":"2025-04-29T14:16:22","modified_gmt":"2025-04-29T18:16:22","slug":"inputoutput","status":"publish","type":"chapter","link":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/chapter\/inputoutput\/","title":{"raw":"Input and Output: Redirection and Pipes","rendered":"Input and Output: Redirection and Pipes"},"content":{"raw":"<p class=\"c15\"><span class=\"c7\">By design most Unix commands are small and simple in their functionality. \u00a0To solve beyond the trivial requires the use of several steps or commands. \u00a0How to sequence and combine commands in Unix requires an understanding of how input and output is managed. \u00a0The next sections will introduce these concepts and show how more complex problems may be solved.<\/span><\/p>\r\n\r\n<h1 id=\"h.wt6mdxt54xsg\" class=\"c4\"><span class=\"c26\">Filters<\/span><\/h1>\r\n<p class=\"c15\">One may think of a filter as a black box with an input and an output. Most Unix commands can be thought of as a filter. \u00a0The inputs and outputs have been given formal names. The input is named <span class=\"c37\">standard input <\/span>(STDIN); the output is named <span class=\"c37\">standard output <\/span>(STDOUT); and there is a secondary output named <span class=\"c37\">standard error <\/span><span class=\"c7\">\u00a0(STDERR) which will be discussed in more detail later. \u00a0These inputs and outputs are associated with file descriptors or stream numbers 0, 1, and 2 respectively.<\/span><\/p>\r\n<p class=\"c2\">By default, Unix commands read from standard input and print to standard output. \u00a0 Any error messages are sent to standard error.\r\n<span style=\"overflow: hidden; margin: 0.00px 0.00px; border: 0.00px solid #000000; width: 624.00px; height: 164.00px;\">\r\n<img alt=\" arrow on left points from standard input to box containing word &quot;filter&quot;; two arrows project from right of box 1. standard output 2. standard error\" src=\"http:\/\/pressbooks.library.ryerson.ca\/opsyshiraki\/wp-content\/uploads\/sites\/330\/2022\/03\/image4.png\" style=\"width: 624.00px; height: 164.00px; margin-left: 0.00px; margin-top: 0.00px;\" width=\"902\" height=\"237\" class=\"alignnone\" \/>\r\n<\/span><\/p>\r\n\r\n<h2 id=\"h.bvaaykkz6xnq\" class=\"c0\"><span class=\"c5\">Examples of Unix commands as filters<\/span><\/h2>\r\n<table class=\"grid\" style=\"height: 178px;\">\r\n<tbody>\r\n<tr>\r\n<th style=\"width: 71.0026px;\"><span class=\"c7\">Command<\/span><\/th>\r\n<th style=\"width: 612.852px;\">\r\n<p class=\"c10\"><span class=\"c7\">Description<\/span><\/p>\r\n<\/th>\r\n<\/tr>\r\n<tr class=\"c9\" style=\"height: 30px;\">\r\n<th class=\"c32\" style=\"height: 30px; width: 71.0026px;\" scope=\"row\">\r\n<pre class=\"c10\"><span class=\"c7\">cut<\/span><\/pre>\r\n<\/th>\r\n<th class=\"c13\" style=\"height: 30px; width: 612.852px;\" scope=\"row\">\r\n<p class=\"c10\"><span class=\"c7\">reads from standard input and passes selected portions (columns, fields) to standard output<\/span><\/p>\r\n<\/th>\r\n<\/tr>\r\n<tr class=\"c9\" style=\"height: 30px;\">\r\n<th class=\"c32\" style=\"height: 30px; width: 71.0026px;\" scope=\"row\">\r\n<pre class=\"c10\"><span class=\"c7\">grep<\/span><\/pre>\r\n<\/th>\r\n<th class=\"c13\" style=\"height: 30px; width: 612.852px;\" scope=\"row\">\r\n<p class=\"c10\"><span class=\"c7\">reads from standard input and prints matching lines to standard output<\/span><\/p>\r\n<\/th>\r\n<\/tr>\r\n<tr class=\"c9\" style=\"height: 28px;\">\r\n<th class=\"c32\" style=\"height: 28px; width: 71.0026px;\" scope=\"row\">\r\n<pre class=\"c10\"><span class=\"c7\">head &amp; tail<\/span><\/pre>\r\n<\/th>\r\n<th class=\"c13\" style=\"height: 28px; width: 612.852px;\" scope=\"row\">\r\n<p class=\"c10\"><span class=\"c7\">reads from standard input and prints the first (last) few lines to standard output<\/span><\/p>\r\n<\/th>\r\n<\/tr>\r\n<tr class=\"c9\" style=\"height: 30px;\">\r\n<th class=\"c32\" style=\"height: 30px; width: 71.0026px;\" scope=\"row\">\r\n<pre class=\"c10\"><span class=\"c7\">cat <\/span><\/pre>\r\n<\/th>\r\n<th class=\"c13\" style=\"height: 30px; width: 612.852px;\" scope=\"row\">\r\n<p class=\"c10\"><span class=\"c7\">a transparent filter: reads from standard input and prints the same to standard output<\/span><\/p>\r\n<\/th>\r\n<\/tr>\r\n<tr class=\"c9\" style=\"height: 30px;\">\r\n<th class=\"c32\" style=\"height: 30px; width: 71.0026px;\" scope=\"row\">\r\n<pre class=\"c10\"><span class=\"c7\">wc<\/span><\/pre>\r\n<\/th>\r\n<th class=\"c13\" style=\"height: 30px; width: 612.852px;\" scope=\"row\">\r\n<p class=\"c10\"><span class=\"c7\">reads from standard input and prints summary information to standard output<\/span><\/p>\r\n<\/th>\r\n<\/tr>\r\n<tr class=\"c9\" style=\"height: 30px;\">\r\n<th class=\"c32\" style=\"height: 30px; width: 71.0026px;\" scope=\"row\">\r\n<pre class=\"c10\"><span class=\"c7\">...<\/span><\/pre>\r\n<\/th>\r\n<th class=\"c13\" style=\"height: 30px; width: 612.852px;\" scope=\"row\">\r\n<p class=\"c10\"><span class=\"c7\">not an exhaustive list<\/span><\/p>\r\n<\/th>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<h1 id=\"h.6mekb4q7u4cf\" class=\"c0\"><span class=\"c26\">Redirection<\/span><\/h1>\r\n<p class=\"c15\"><span class=\"c7\">When working interactively in a Unix session, the default setup is to have standard input draw from the keyboard, and standard output (as well as standard error) directed at the screen. \u00a0In other words, a command reading from standard input will wait for keystrokes. A command printing to standard output will have its output appear on the terminal screen.<\/span><\/p>\r\n\r\n\r\n[caption id=\"\" align=\"alignnone\" width=\"939\"]<img alt=\"arrow points from keyboard to filter box; two arrows emanate from filter both directed at screen\" src=\"http:\/\/pressbooks.library.ryerson.ca\/opsyshiraki\/wp-content\/uploads\/sites\/330\/2022\/03\/image6.png\" style=\"width: 624.00px; height: 297.33px; margin-left: 0.00px; margin-top: 0.00px;\" width=\"939\" height=\"447\" \/> Default association of keyboard and screen to filter.[\/caption]\r\n<p class=\"c15\"><span class=\"c7\">While this arrangement works well, there will come situations where the user will want to save the output of a command to a file, or substitute a file for keyboard input. \u00a0This is accomplished through the concept of redirection where one of the inputs or outputs is associated with a file.<\/span><\/p>\r\n\r\n<h2 id=\"h.gss9xc26v69s\" class=\"c41 c11\"><span class=\"c5\">Redirection of standard output ( &gt; operator)<\/span><\/h2>\r\n<p class=\"c2\"><span class=\"c7\">\u00a0<\/span><span class=\"c7\">The user can save the standard output of any Unix command by redirecting standard output to a file using the &gt; operator. \u00a0Graphically, the concept is illustrated as follows.<\/span><\/p>\r\n\r\n\r\n[caption id=\"\" align=\"alignnone\" width=\"939\"]<img alt=\"arrow points from keyboard to filter box; standard output arrow aims at file; standard error arrow aims at screen.\" src=\"http:\/\/pressbooks.library.ryerson.ca\/opsyshiraki\/wp-content\/uploads\/sites\/330\/2022\/03\/image7.png\" style=\"width: 624.00px; height: 297.33px; margin-left: 0.00px; margin-top: 0.00px;\" width=\"939\" height=\"447\" \/> Redirection of Standard Output[\/caption]\r\n<table class=\"grid\">\r\n<tbody>\r\n<tr>\r\n<th><span class=\"c14 c12\">Operator syntax<\/span><\/th>\r\n<td>\r\n<p class=\"c8\">Examples and explanation<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"c28\">\r\n<th class=\"c31\" scope=\"row\">\r\n<pre class=\"c8\"><span class=\"c14 c12\">cmd &gt; some_file<\/span><\/pre>\r\n<\/th>\r\n<td class=\"c29\">\r\n<p class=\"c8\">e.g.<\/p>\r\n\r\n<pre class=\"c8\"><span class=\"c14 c12\">cat chapter1 &gt; book<\/span><\/pre>\r\n<ul class=\"c3 lst-kix_eni0n9t8gcec-0 start\">\r\n \t<li class=\"c16 c21 li-bullet-0\"><span class=\"c19\">\u00a0<\/span><span class=\"c7\">if the file already exists it will overwrite it<\/span><\/li>\r\n<\/ul>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"c39\">\r\n<th class=\"c31\" scope=\"row\">\r\n<pre class=\"c8\"><span class=\"c14 c12\">cmd &gt;&gt; some_file<\/span><\/pre>\r\n<\/th>\r\n<td class=\"c29\">\r\n<p class=\"c8\">e.g.<\/p>\r\n\r\n<pre class=\"c8\"><span class=\"c12 c14\">cat chapter3 &gt;&gt; book<\/span><\/pre>\r\n<ul class=\"c3 lst-kix_5ixwda23g4fh-0 start\">\r\n \t<li class=\"c21 c16 li-bullet-0\"><span class=\"c7\">the &gt;&gt; operator appends an existing file<\/span><\/li>\r\n<\/ul>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<h2 id=\"h.sb2v99olmxe6\" class=\"c0\"><span class=\"c5\">Redirecting standard error ( 2&gt; operator)<\/span><\/h2>\r\n<p class=\"c15\"><span class=\"c7\">The user can save the standard error of any Unix command by redirecting standard error to a file using the 2&gt; operator. Graphically, the concept is illustrated as follows.<\/span><\/p>\r\n\r\n\r\n[caption id=\"\" align=\"alignnone\" width=\"939\"]<img alt=\"arrow points from keyboard to filter box; standard output arrow aims at screen; standard error arrow aims at file.\" src=\"http:\/\/pressbooks.library.ryerson.ca\/opsyshiraki\/wp-content\/uploads\/sites\/330\/2022\/03\/image3.png\" style=\"width: 624.00px; height: 297.33px; margin-left: 0.00px; margin-top: 0.00px;\" width=\"939\" height=\"447\" \/> Redirection of Standard Error[\/caption]\r\n<table class=\"grid\" style=\"width: 486px;\">\r\n<tbody>\r\n<tr>\r\n<th style=\"width: 208.6px;\"><span class=\"c14 c12\">Operator syntax<\/span><\/th>\r\n<td style=\"width: 249.167px;\">\r\n<p class=\"c8\">Examples and explanation<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"c46\">\r\n<th class=\"c31\" style=\"width: 208.6px;\" scope=\"row\">\r\n<pre class=\"c8\"><span class=\"c14 c12\">cmd 2&gt; some_file<\/span><\/pre>\r\n<\/th>\r\n<td class=\"c29\" style=\"width: 249.167px;\">\r\n<pre class=\"c8\">e.g.\r\n<span class=\"c12\">cat chapter1 <\/span><span class=\"c12\">chapter4 <\/span><span class=\"c14 c12\">2&gt; errors<\/span><\/pre>\r\n<p class=\"c6\"><span class=\"c14 c12\">\u00a0<\/span><\/p>\r\n\r\n<ul class=\"c3 lst-kix_45riwvqygofu-0 start\">\r\n \t<li class=\"c10 c16 c48 li-bullet-0\">saves error messages in file errors but standard output is displayed on the screen<\/li>\r\n<\/ul>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"c34\">\r\n<th class=\"c31\" style=\"width: 208.6px;\" scope=\"row\">\r\n<pre class=\"c8\"><span class=\"c14 c12\">cmd &gt; some_file 2&gt; another_file<\/span><\/pre>\r\n<\/th>\r\n<td class=\"c29\" style=\"width: 249.167px;\">\r\n<pre class=\"c8\">e.g.\r\n<span class=\"c14 c12\">cat chapter1 &gt; book 2&gt; errors<\/span><\/pre>\r\n<p class=\"c6\"><span class=\"c14 c12\">\u00a0<\/span><\/p>\r\n\r\n<ul class=\"c3 lst-kix_87jixnldtqct-0 start\">\r\n \t<li class=\"c10 c48 c16 li-bullet-0\"><span class=\"c7\">saves standard output to file book, and saves standard error messages in file errors<\/span><\/li>\r\n<\/ul>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<h2 id=\"h.shd9x97a7p7v\" class=\"c0\"><span class=\"c5\">Merging two streams ( &gt;&amp; operator)<\/span><\/h2>\r\n<p class=\"c2\"><span class=\"c7\">\u00a0<\/span><span class=\"c7\">The user can save both standard output and standard error of any Unix command.\u00a0 This is accomplished by first redirecting standard error to a file, and then merging standard error with standard output.\u00a0 The syntax of the merge operator is m&gt;&amp;n where stream m is merged with wherever stream n is already going.\u00a0 \u00a0Graphically, the concept is illustrated as follows.\u00a0 Here stream 2 (standard error) is merged with stream 1 (standard output).<\/span><\/p>\r\n<p class=\"c2\"><span class=\"c7\">\u00a0<\/span><img alt=\"arrow points from keyboard to filter box; two arrows emanate from filter both directed to a plus symbol showing a merge; one arrow goes from plus symbol to file.\" src=\"http:\/\/pressbooks.library.ryerson.ca\/opsyshiraki\/wp-content\/uploads\/sites\/330\/2022\/03\/image5.png\" width=\"939\" height=\"447\" style=\"width: 624.00px; height: 297.33px; margin-left: 0.00px; margin-top: 0.00px;\" \/><\/p>\r\n<p class=\"c2\">Standard Output and Standard Error are merged (combined) and re-directed to a file<\/p>\r\n\r\n<table class=\"grid\" style=\"width: 336px; height: 196px;\">\r\n<tbody>\r\n<tr style=\"height: 38px;\">\r\n<th style=\"width: 164.797px; height: 38px;\"><span class=\"c14 c12\">Operator syntax<\/span><\/th>\r\n<td style=\"width: 459.828px; height: 38px;\">\r\n<p class=\"c8\">Examples and explanation<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"c45\" style=\"height: 258px;\">\r\n<td class=\"c40\" style=\"width: 164.797px; height: 258px;\">\r\n<pre class=\"c8\"><span class=\"c14 c12\">cmd &gt; out_file 2&gt;&amp;1<\/span><\/pre>\r\n<\/td>\r\n<td class=\"c47\" style=\"width: 459.828px; height: 258px;\">\r\n<pre class=\"c8\">e.g.\r\n<span class=\"c14 c12\">cat text1 junk text3 &gt; both 2&gt;&amp;1<\/span><\/pre>\r\n<ul class=\"c3 lst-kix_6iqymci964mf-0 start\">\r\n \t<li class=\"c21 c16 li-bullet-0\"><span class=\"c7\">saves standard output and standard error to file both<\/span><\/li>\r\n \t<li class=\"c21 c16 li-bullet-0\"><span class=\"c7\">Explanation: Unix parses left to right. \u00a0First standard output is redirected to file \"both\". \u00a0Then the merge operator (&gt;&amp;) merges (blends) standard error (stream 2) with where standard output (stream 1) is already going (to file \"both\").<\/span><\/li>\r\n \t<li class=\"c21 c16 li-bullet-0\">Caution: How about\r\n<pre><span class=\"c14 c12\">cat text1 junk text3 &gt; both 2&gt; both<\/span><\/pre>\r\n<\/li>\r\n<\/ul>\r\n<ul class=\"c3 lst-kix_6iqymci964mf-1 start\">\r\n \t<li class=\"c21 c52 li-bullet-0\"><span class=\"c7\">This is an incorrect method of trying to merge two streams. \u00a0This causes a race condition (two streams competing for the same file) and correct results are not guaranteed.<\/span><\/li>\r\n<\/ul>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<h2 id=\"h.9i31k9ol401\" class=\"c0\"><span class=\"c5\">Redirecting standard input ( &lt; operator)<\/span><\/h2>\r\n<p class=\"c15\"><span class=\"c7\">The user can redirect standard input from a file instead of the keyboard to any Unix command using the &lt; operator. \u00a0Graphically, the concept is illustrated as follows.<\/span><\/p>\r\n\r\n\r\n[caption id=\"\" align=\"alignnone\" width=\"939\"]<img alt=\"arrow points from file to filter box; two arrows emanate from filter both directed at screen\" src=\"http:\/\/pressbooks.library.ryerson.ca\/opsyshiraki\/wp-content\/uploads\/sites\/330\/2022\/03\/image1.png\" style=\"width: 624.00px; height: 297.33px; margin-left: 0.00px; margin-top: 0.00px;\" width=\"939\" height=\"447\" \/> Redirection of Standard Input from a file[\/caption]\r\n<table class=\"grid\" style=\"width: 615px; height: 314px;\">\r\n<tbody>\r\n<tr>\r\n<th style=\"width: 165.875px;\"><span class=\"c14 c12\">Operator syntax<\/span><\/th>\r\n<td style=\"width: 458.75px;\">\r\n<p class=\"c8\">Examples and explanation<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"c45\">\r\n<td class=\"c40\" style=\"width: 165.875px;\">\r\n<pre class=\"c8\"><span class=\"c14 c12\">cmd &lt; some_file<\/span><\/pre>\r\n<\/td>\r\n<td class=\"c47\" style=\"width: 458.75px;\">\r\n<pre class=\"c8\"><span class=\"c7\">cat &lt; appendix<\/span><\/pre>\r\n<p class=\"c21\"><span class=\"c7\">While it may appear that the \"&lt;\" in the above command does not seem to do anything (works the same without the \"&lt;\"), the reason is that the cat command is smart and knows to look for input on the command line. \u00a0Unless a command is specifically designed to inspect the command line for input arguments, it is necessary to use the \"&lt;\" for redirection of standard input. \u00a0<\/span><\/p>\r\n<p class=\"c21\"><span class=\"c7\">Consider a more basic example of a simple script requesting input from the keyboard. \u00a0To substitute a file, it would be necessary to issue the following command, where the file \"keystrokes\" contains what the user would have typed.<\/span><\/p>\r\n\r\n<pre id=\"h.p94fyfyj5b4m\" class=\"c21 c11\"><span class=\"c14 c12\">myscript &lt; keystrokes<\/span><\/pre>\r\n<p class=\"c21 c22\"><span class=\"c7\">Here \"myscript\" represents a user-written Unix script (program), not a Unix command.<\/span><\/p>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<h1 id=\"h.1a28qkewep1a\" class=\"c0\"><span class=\"c26\">Pipes<\/span><\/h1>\r\n<p class=\"c15\"><span class=\"c7\">It is often the case that a problem in Unix is solved with multiple commands.\u00a0 Typically the output of the first command is saved in a file which is then used as input to a subsequent command. \u00a0The use of a pipe is considered a refinement of this approach potentially simplifying the solution.<\/span><\/p>\r\n\r\n<h2 class=\"c33\"><span class=\"c7\">Problem: To determine the number of entries in a directory<\/span><\/h2>\r\n<h3>Method 1<\/h3>\r\n<table class=\"grid\">\r\n<thead>\r\n<tr class=\"c9\">\r\n<th class=\"c18\" colspan=\"1\" rowspan=\"1\" scope=\"col\">\r\n<p class=\"c1\"><span class=\"c7\">Graphical view<\/span><\/p>\r\n<\/th>\r\n<th class=\"c18\" colspan=\"1\" rowspan=\"1\" scope=\"col\">\r\n<p class=\"c10\"><span class=\"c7\">code<\/span><\/p>\r\n<\/th>\r\n<th class=\"c18\" colspan=\"1\" rowspan=\"1\" scope=\"col\">\r\n<p class=\"c10\"><span class=\"c7\">explanation<\/span><\/p>\r\n<\/th>\r\n<\/tr>\r\n<\/thead>\r\n<tbody>\r\n<tr class=\"c9\">\r\n<td class=\"c18\" colspan=\"1\" rowspan=\"1\">\r\n<p class=\"c1\">ls -&gt;\r\n<span class=\"c50\">file_list <\/span>\r\n<span class=\"c7\">-&gt; wc<\/span><\/p>\r\n<\/td>\r\n<td class=\"c18\" colspan=\"1\" rowspan=\"1\">\r\n<pre id=\"h.ckhvq4jx782t\" class=\"c1 c11\"><span class=\"c14 c12\">ls \/etc &gt; file_list<\/span>\r\n<span class=\"c14 c12\">wc -l file_list<\/span>\r\nrm file_list<\/pre>\r\n<\/td>\r\n<td class=\"c18\" colspan=\"1\" rowspan=\"1\">\r\n<ul class=\"c3 lst-kix_2j8r3h84ts86-0 start\">\r\n \t<li class=\"c10 c16 li-bullet-0\"><span class=\"c7\">save output of ls command in file_list<\/span><\/li>\r\n \t<li class=\"c10 c16 li-bullet-0\"><span class=\"c7\">run wc command to count lines<\/span><\/li>\r\n \t<li class=\"c10 c16 li-bullet-0\"><span class=\"c7\">delete temporary file file_list<\/span><\/li>\r\n<\/ul>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<h3>Method 2<\/h3>\r\n<table class=\"grid\">\r\n<thead>\r\n<tr class=\"c9\">\r\n<th class=\"c18\" colspan=\"1\" rowspan=\"1\" scope=\"col\">\r\n<p class=\"c1\"><span class=\"c7\">Graphical view<\/span><\/p>\r\n<\/th>\r\n<th class=\"c18\" colspan=\"1\" rowspan=\"1\" scope=\"col\">\r\n<p class=\"c10\"><span class=\"c7\">code<\/span><\/p>\r\n<\/th>\r\n<th class=\"c18\" colspan=\"1\" rowspan=\"1\" scope=\"col\">\r\n<p class=\"c10\"><span class=\"c7\">explanation<\/span><\/p>\r\n<\/th>\r\n<\/tr>\r\n<\/thead>\r\n<tbody>\r\n<tr class=\"c9\">\r\n<td class=\"c18\" colspan=\"1\" rowspan=\"1\">\r\n<pre class=\"c33\">ls -&gt;\r\n<span style=\"overflow: hidden; margin: 0.00px 0.00px; border: 0.00px solid #000000; width: 65.31px; height: 20.00px;\">\r\n<img alt=\"\" src=\"http:\/\/pressbooks.library.ryerson.ca\/opsyshiraki\/wp-content\/uploads\/sites\/330\/2022\/03\/image2.png\" style=\"width: 65.31px; height: 20.00px; margin-left: 0.00px; margin-top: 0.00px;\" title=\"\" \/>\r\n<\/span>\r\n<span class=\"c7\">\u00a0-&gt; wc <\/span>\r\n\r\n<span class=\"c7\">\u00a0<\/span><\/pre>\r\n<\/td>\r\n<td class=\"c18\" colspan=\"1\" rowspan=\"1\">\r\n<pre class=\"c17 c36\"><span class=\"c12\">ls \/etc | wc -l<\/span><\/pre>\r\n<\/td>\r\n<td class=\"c18\" colspan=\"1\" rowspan=\"1\">\r\n<ul class=\"c3 lst-kix_2j8r3h84ts86-0\">\r\n \t<li class=\"c10 c16 li-bullet-0\"><span class=\"c7\">save output of ls command is sent directly to input of wc command<\/span><\/li>\r\n \t<li class=\"c10 c16 li-bullet-0\"><span class=\"c7\">no temporary file needed<\/span><\/li>\r\n<\/ul>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<p class=\"c20\"><span class=\"c7\">\u00a0<\/span><\/p>\r\n\r\n<div class=\"textbox\">\r\n<h2 class=\"c33\"><span class=\"c37\">Definition:<\/span><\/h2>\r\n<p class=\"c33\"><span class=\"c7\">A pipe connects STDOUT of previous command to STDIN of next command<\/span><\/p>\r\n\r\n<\/div>\r\n<p class=\"c33\"><span class=\"c7\">You can use a pipe multiple times creating a pipeline.<\/span><\/p>\r\n\r\n<pre class=\"c30\">e.g.\r\n<span class=\"c14 c12\">cmd1 | cmd2 | cmd3 | cmd4<\/span><\/pre>\r\n<p class=\"c33\"><span class=\"c7\">Building a pipeline should be an iterative process. Condense stepwise as you know the solutions work, otherwise there might be errors that might be difficult to detect from a single pipeline<\/span><\/p>\r\n<p class=\"c33\"><span class=\"c7\">Start out like this:<\/span><\/p>\r\n\r\n<pre id=\"h.69mc4iy1lwkm\" class=\"c0\"><span class=\"c14 c12\">\r\ncmd1 &gt; out1<\/span>\r\ncmd2 &lt; out1 &gt; out2\r\ncmd3 &lt; out2<\/pre>\r\n...\r\n<p class=\"c20\"><span class=\"c7\">\u00a0<\/span><\/p>\r\n\r\n<div class=\"textbox textbox--key-takeaways\"><header class=\"textbox__header\">\r\n<p class=\"textbox__title\">Key Takeaways<\/p>\r\n\r\n<\/header>\r\n<div class=\"textbox__content\">\r\n<ol>\r\n \t<li class=\"c17 c36\"><span class=\"c19\">\u00a0<\/span><span class=\"c37\">Redirection<\/span><span class=\"c7\">: Use between a command and a file<\/span><\/li>\r\n \t<li class=\"c17 c36\">\u00a0Pipe<span class=\"c7\" style=\"text-align: initial; font-size: 1em;\">: Use between commands<\/span><\/li>\r\n<\/ol>\r\n<\/div>\r\n<\/div>\r\n\r\n<hr \/>\r\n\r\n<h1 class=\"c2\"><span class=\"c7\">\u00a0<\/span><span class=\"c5\">Making your script executable<\/span><\/h1>\r\n<ol class=\"c3 lst-kix_w9cpc76ivqci-0 start\" start=\"1\">\r\n \t<li class=\"c15 c16 li-bullet-0\"><span class=\"c7\">create file containing unix commands<\/span><\/li>\r\n \t<li class=\"c15 c16 li-bullet-0\"><span class=\"c7\">Once per file, type either:<\/span>\r\n<ul class=\"c3 lst-kix_w9cpc76ivqci-0 start\">\r\n \t<li><span class=\"c7\">chmod u+x myscript<\/span><\/li>\r\n \t<li><span class=\"c7\">chmod 700 myscript<\/span><\/li>\r\n<\/ul>\r\n<\/li>\r\n \t<li class=\"c15 c16 li-bullet-0\"><span class=\"c7\">To run, type .\/myscript<\/span><\/li>\r\n<\/ol>\r\n<p class=\"c2\"><span class=\"c7\">\u00a0<\/span><\/p>\r\n&nbsp;","rendered":"<p class=\"c15\"><span class=\"c7\">By design most Unix commands are small and simple in their functionality. \u00a0To solve beyond the trivial requires the use of several steps or commands. \u00a0How to sequence and combine commands in Unix requires an understanding of how input and output is managed. \u00a0The next sections will introduce these concepts and show how more complex problems may be solved.<\/span><\/p>\n<h1 id=\"h.wt6mdxt54xsg\" class=\"c4\"><span class=\"c26\">Filters<\/span><\/h1>\n<p class=\"c15\">One may think of a filter as a black box with an input and an output. Most Unix commands can be thought of as a filter. \u00a0The inputs and outputs have been given formal names. The input is named <span class=\"c37\">standard input <\/span>(STDIN); the output is named <span class=\"c37\">standard output <\/span>(STDOUT); and there is a secondary output named <span class=\"c37\">standard error <\/span><span class=\"c7\">\u00a0(STDERR) which will be discussed in more detail later. \u00a0These inputs and outputs are associated with file descriptors or stream numbers 0, 1, and 2 respectively.<\/span><\/p>\n<p class=\"c2\">By default, Unix commands read from standard input and print to standard output. \u00a0 Any error messages are sent to standard error.<br \/>\n<span style=\"overflow: hidden; margin: 0.00px 0.00px; border: 0.00px solid #000000; width: 624.00px; height: 164.00px;\"><br \/>\n<img loading=\"lazy\" decoding=\"async\" alt=\"arrow on left points from standard input to box containing word &quot;filter&quot;; two arrows project from right of box 1. standard output 2. standard error\" src=\"http:\/\/pressbooks.library.ryerson.ca\/opsyshiraki\/wp-content\/uploads\/sites\/330\/2022\/03\/image4.png\" style=\"width: 624.00px; height: 164.00px; margin-left: 0.00px; margin-top: 0.00px;\" width=\"902\" height=\"237\" class=\"alignnone\" \/><br \/>\n<\/span><\/p>\n<h2 id=\"h.bvaaykkz6xnq\" class=\"c0\"><span class=\"c5\">Examples of Unix commands as filters<\/span><\/h2>\n<table class=\"grid\" style=\"height: 178px;\">\n<tbody>\n<tr>\n<th style=\"width: 71.0026px;\"><span class=\"c7\">Command<\/span><\/th>\n<th style=\"width: 612.852px;\">\n<p class=\"c10\"><span class=\"c7\">Description<\/span><\/p>\n<\/th>\n<\/tr>\n<tr class=\"c9\" style=\"height: 30px;\">\n<th class=\"c32\" style=\"height: 30px; width: 71.0026px;\" scope=\"row\">\n<pre class=\"c10\"><span class=\"c7\">cut<\/span><\/pre>\n<\/th>\n<th class=\"c13\" style=\"height: 30px; width: 612.852px;\" scope=\"row\">\n<p class=\"c10\"><span class=\"c7\">reads from standard input and passes selected portions (columns, fields) to standard output<\/span><\/p>\n<\/th>\n<\/tr>\n<tr class=\"c9\" style=\"height: 30px;\">\n<th class=\"c32\" style=\"height: 30px; width: 71.0026px;\" scope=\"row\">\n<pre class=\"c10\"><span class=\"c7\">grep<\/span><\/pre>\n<\/th>\n<th class=\"c13\" style=\"height: 30px; width: 612.852px;\" scope=\"row\">\n<p class=\"c10\"><span class=\"c7\">reads from standard input and prints matching lines to standard output<\/span><\/p>\n<\/th>\n<\/tr>\n<tr class=\"c9\" style=\"height: 28px;\">\n<th class=\"c32\" style=\"height: 28px; width: 71.0026px;\" scope=\"row\">\n<pre class=\"c10\"><span class=\"c7\">head &amp; tail<\/span><\/pre>\n<\/th>\n<th class=\"c13\" style=\"height: 28px; width: 612.852px;\" scope=\"row\">\n<p class=\"c10\"><span class=\"c7\">reads from standard input and prints the first (last) few lines to standard output<\/span><\/p>\n<\/th>\n<\/tr>\n<tr class=\"c9\" style=\"height: 30px;\">\n<th class=\"c32\" style=\"height: 30px; width: 71.0026px;\" scope=\"row\">\n<pre class=\"c10\"><span class=\"c7\">cat <\/span><\/pre>\n<\/th>\n<th class=\"c13\" style=\"height: 30px; width: 612.852px;\" scope=\"row\">\n<p class=\"c10\"><span class=\"c7\">a transparent filter: reads from standard input and prints the same to standard output<\/span><\/p>\n<\/th>\n<\/tr>\n<tr class=\"c9\" style=\"height: 30px;\">\n<th class=\"c32\" style=\"height: 30px; width: 71.0026px;\" scope=\"row\">\n<pre class=\"c10\"><span class=\"c7\">wc<\/span><\/pre>\n<\/th>\n<th class=\"c13\" style=\"height: 30px; width: 612.852px;\" scope=\"row\">\n<p class=\"c10\"><span class=\"c7\">reads from standard input and prints summary information to standard output<\/span><\/p>\n<\/th>\n<\/tr>\n<tr class=\"c9\" style=\"height: 30px;\">\n<th class=\"c32\" style=\"height: 30px; width: 71.0026px;\" scope=\"row\">\n<pre class=\"c10\"><span class=\"c7\">...<\/span><\/pre>\n<\/th>\n<th class=\"c13\" style=\"height: 30px; width: 612.852px;\" scope=\"row\">\n<p class=\"c10\"><span class=\"c7\">not an exhaustive list<\/span><\/p>\n<\/th>\n<\/tr>\n<\/tbody>\n<\/table>\n<h1 id=\"h.6mekb4q7u4cf\" class=\"c0\"><span class=\"c26\">Redirection<\/span><\/h1>\n<p class=\"c15\"><span class=\"c7\">When working interactively in a Unix session, the default setup is to have standard input draw from the keyboard, and standard output (as well as standard error) directed at the screen. \u00a0In other words, a command reading from standard input will wait for keystrokes. A command printing to standard output will have its output appear on the terminal screen.<\/span><\/p>\n<figure style=\"width: 939px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" alt=\"arrow points from keyboard to filter box; two arrows emanate from filter both directed at screen\" src=\"http:\/\/pressbooks.library.ryerson.ca\/opsyshiraki\/wp-content\/uploads\/sites\/330\/2022\/03\/image6.png\" style=\"width: 624.00px; height: 297.33px; margin-left: 0.00px; margin-top: 0.00px;\" width=\"939\" height=\"447\" \/><figcaption class=\"wp-caption-text\">Default association of keyboard and screen to filter.<\/figcaption><\/figure>\n<p class=\"c15\"><span class=\"c7\">While this arrangement works well, there will come situations where the user will want to save the output of a command to a file, or substitute a file for keyboard input. \u00a0This is accomplished through the concept of redirection where one of the inputs or outputs is associated with a file.<\/span><\/p>\n<h2 id=\"h.gss9xc26v69s\" class=\"c41 c11\"><span class=\"c5\">Redirection of standard output ( &gt; operator)<\/span><\/h2>\n<p class=\"c2\"><span class=\"c7\">\u00a0<\/span><span class=\"c7\">The user can save the standard output of any Unix command by redirecting standard output to a file using the &gt; operator. \u00a0Graphically, the concept is illustrated as follows.<\/span><\/p>\n<figure style=\"width: 939px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" alt=\"arrow points from keyboard to filter box; standard output arrow aims at file; standard error arrow aims at screen.\" src=\"http:\/\/pressbooks.library.ryerson.ca\/opsyshiraki\/wp-content\/uploads\/sites\/330\/2022\/03\/image7.png\" style=\"width: 624.00px; height: 297.33px; margin-left: 0.00px; margin-top: 0.00px;\" width=\"939\" height=\"447\" \/><figcaption class=\"wp-caption-text\">Redirection of Standard Output<\/figcaption><\/figure>\n<table class=\"grid\">\n<tbody>\n<tr>\n<th><span class=\"c14 c12\">Operator syntax<\/span><\/th>\n<td>\n<p class=\"c8\">Examples and explanation<\/p>\n<\/td>\n<\/tr>\n<tr class=\"c28\">\n<th class=\"c31\" scope=\"row\">\n<pre class=\"c8\"><span class=\"c14 c12\">cmd &gt; some_file<\/span><\/pre>\n<\/th>\n<td class=\"c29\">\n<p class=\"c8\">e.g.<\/p>\n<pre class=\"c8\"><span class=\"c14 c12\">cat chapter1 &gt; book<\/span><\/pre>\n<ul class=\"c3 lst-kix_eni0n9t8gcec-0 start\">\n<li class=\"c16 c21 li-bullet-0\"><span class=\"c19\">\u00a0<\/span><span class=\"c7\">if the file already exists it will overwrite it<\/span><\/li>\n<\/ul>\n<\/td>\n<\/tr>\n<tr class=\"c39\">\n<th class=\"c31\" scope=\"row\">\n<pre class=\"c8\"><span class=\"c14 c12\">cmd &gt;&gt; some_file<\/span><\/pre>\n<\/th>\n<td class=\"c29\">\n<p class=\"c8\">e.g.<\/p>\n<pre class=\"c8\"><span class=\"c12 c14\">cat chapter3 &gt;&gt; book<\/span><\/pre>\n<ul class=\"c3 lst-kix_5ixwda23g4fh-0 start\">\n<li class=\"c21 c16 li-bullet-0\"><span class=\"c7\">the &gt;&gt; operator appends an existing file<\/span><\/li>\n<\/ul>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"h.sb2v99olmxe6\" class=\"c0\"><span class=\"c5\">Redirecting standard error ( 2&gt; operator)<\/span><\/h2>\n<p class=\"c15\"><span class=\"c7\">The user can save the standard error of any Unix command by redirecting standard error to a file using the 2&gt; operator. Graphically, the concept is illustrated as follows.<\/span><\/p>\n<figure style=\"width: 939px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" alt=\"arrow points from keyboard to filter box; standard output arrow aims at screen; standard error arrow aims at file.\" src=\"http:\/\/pressbooks.library.ryerson.ca\/opsyshiraki\/wp-content\/uploads\/sites\/330\/2022\/03\/image3.png\" style=\"width: 624.00px; height: 297.33px; margin-left: 0.00px; margin-top: 0.00px;\" width=\"939\" height=\"447\" \/><figcaption class=\"wp-caption-text\">Redirection of Standard Error<\/figcaption><\/figure>\n<table class=\"grid\" style=\"width: 486px;\">\n<tbody>\n<tr>\n<th style=\"width: 208.6px;\"><span class=\"c14 c12\">Operator syntax<\/span><\/th>\n<td style=\"width: 249.167px;\">\n<p class=\"c8\">Examples and explanation<\/p>\n<\/td>\n<\/tr>\n<tr class=\"c46\">\n<th class=\"c31\" style=\"width: 208.6px;\" scope=\"row\">\n<pre class=\"c8\"><span class=\"c14 c12\">cmd 2&gt; some_file<\/span><\/pre>\n<\/th>\n<td class=\"c29\" style=\"width: 249.167px;\">\n<pre class=\"c8\">e.g.\r\n<span class=\"c12\">cat chapter1 <\/span><span class=\"c12\">chapter4 <\/span><span class=\"c14 c12\">2&gt; errors<\/span><\/pre>\n<p class=\"c6\"><span class=\"c14 c12\">\u00a0<\/span><\/p>\n<ul class=\"c3 lst-kix_45riwvqygofu-0 start\">\n<li class=\"c10 c16 c48 li-bullet-0\">saves error messages in file errors but standard output is displayed on the screen<\/li>\n<\/ul>\n<\/td>\n<\/tr>\n<tr class=\"c34\">\n<th class=\"c31\" style=\"width: 208.6px;\" scope=\"row\">\n<pre class=\"c8\"><span class=\"c14 c12\">cmd &gt; some_file 2&gt; another_file<\/span><\/pre>\n<\/th>\n<td class=\"c29\" style=\"width: 249.167px;\">\n<pre class=\"c8\">e.g.\r\n<span class=\"c14 c12\">cat chapter1 &gt; book 2&gt; errors<\/span><\/pre>\n<p class=\"c6\"><span class=\"c14 c12\">\u00a0<\/span><\/p>\n<ul class=\"c3 lst-kix_87jixnldtqct-0 start\">\n<li class=\"c10 c48 c16 li-bullet-0\"><span class=\"c7\">saves standard output to file book, and saves standard error messages in file errors<\/span><\/li>\n<\/ul>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"h.shd9x97a7p7v\" class=\"c0\"><span class=\"c5\">Merging two streams ( &gt;&amp; operator)<\/span><\/h2>\n<p class=\"c2\"><span class=\"c7\">\u00a0<\/span><span class=\"c7\">The user can save both standard output and standard error of any Unix command.\u00a0 This is accomplished by first redirecting standard error to a file, and then merging standard error with standard output.\u00a0 The syntax of the merge operator is m&gt;&amp;n where stream m is merged with wherever stream n is already going.\u00a0 \u00a0Graphically, the concept is illustrated as follows.\u00a0 Here stream 2 (standard error) is merged with stream 1 (standard output).<\/span><\/p>\n<p class=\"c2\"><span class=\"c7\">\u00a0<\/span><img loading=\"lazy\" decoding=\"async\" alt=\"arrow points from keyboard to filter box; two arrows emanate from filter both directed to a plus symbol showing a merge; one arrow goes from plus symbol to file.\" src=\"http:\/\/pressbooks.library.ryerson.ca\/opsyshiraki\/wp-content\/uploads\/sites\/330\/2022\/03\/image5.png\" width=\"939\" height=\"447\" style=\"width: 624.00px; height: 297.33px; margin-left: 0.00px; margin-top: 0.00px;\" \/><\/p>\n<p class=\"c2\">Standard Output and Standard Error are merged (combined) and re-directed to a file<\/p>\n<table class=\"grid\" style=\"width: 336px; height: 196px;\">\n<tbody>\n<tr style=\"height: 38px;\">\n<th style=\"width: 164.797px; height: 38px;\"><span class=\"c14 c12\">Operator syntax<\/span><\/th>\n<td style=\"width: 459.828px; height: 38px;\">\n<p class=\"c8\">Examples and explanation<\/p>\n<\/td>\n<\/tr>\n<tr class=\"c45\" style=\"height: 258px;\">\n<td class=\"c40\" style=\"width: 164.797px; height: 258px;\">\n<pre class=\"c8\"><span class=\"c14 c12\">cmd &gt; out_file 2&gt;&amp;1<\/span><\/pre>\n<\/td>\n<td class=\"c47\" style=\"width: 459.828px; height: 258px;\">\n<pre class=\"c8\">e.g.\r\n<span class=\"c14 c12\">cat text1 junk text3 &gt; both 2&gt;&amp;1<\/span><\/pre>\n<ul class=\"c3 lst-kix_6iqymci964mf-0 start\">\n<li class=\"c21 c16 li-bullet-0\"><span class=\"c7\">saves standard output and standard error to file both<\/span><\/li>\n<li class=\"c21 c16 li-bullet-0\"><span class=\"c7\">Explanation: Unix parses left to right. \u00a0First standard output is redirected to file &#8220;both&#8221;. \u00a0Then the merge operator (&gt;&amp;) merges (blends) standard error (stream 2) with where standard output (stream 1) is already going (to file &#8220;both&#8221;).<\/span><\/li>\n<li class=\"c21 c16 li-bullet-0\">Caution: How about\n<pre><span class=\"c14 c12\">cat text1 junk text3 &gt; both 2&gt; both<\/span><\/pre>\n<\/li>\n<\/ul>\n<ul class=\"c3 lst-kix_6iqymci964mf-1 start\">\n<li class=\"c21 c52 li-bullet-0\"><span class=\"c7\">This is an incorrect method of trying to merge two streams. \u00a0This causes a race condition (two streams competing for the same file) and correct results are not guaranteed.<\/span><\/li>\n<\/ul>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"h.9i31k9ol401\" class=\"c0\"><span class=\"c5\">Redirecting standard input ( &lt; operator)<\/span><\/h2>\n<p class=\"c15\"><span class=\"c7\">The user can redirect standard input from a file instead of the keyboard to any Unix command using the &lt; operator. \u00a0Graphically, the concept is illustrated as follows.<\/span><\/p>\n<figure style=\"width: 939px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" alt=\"arrow points from file to filter box; two arrows emanate from filter both directed at screen\" src=\"http:\/\/pressbooks.library.ryerson.ca\/opsyshiraki\/wp-content\/uploads\/sites\/330\/2022\/03\/image1.png\" style=\"width: 624.00px; height: 297.33px; margin-left: 0.00px; margin-top: 0.00px;\" width=\"939\" height=\"447\" \/><figcaption class=\"wp-caption-text\">Redirection of Standard Input from a file<\/figcaption><\/figure>\n<table class=\"grid\" style=\"width: 615px; height: 314px;\">\n<tbody>\n<tr>\n<th style=\"width: 165.875px;\"><span class=\"c14 c12\">Operator syntax<\/span><\/th>\n<td style=\"width: 458.75px;\">\n<p class=\"c8\">Examples and explanation<\/p>\n<\/td>\n<\/tr>\n<tr class=\"c45\">\n<td class=\"c40\" style=\"width: 165.875px;\">\n<pre class=\"c8\"><span class=\"c14 c12\">cmd &lt; some_file<\/span><\/pre>\n<\/td>\n<td class=\"c47\" style=\"width: 458.75px;\">\n<pre class=\"c8\"><span class=\"c7\">cat &lt; appendix<\/span><\/pre>\n<p class=\"c21\"><span class=\"c7\">While it may appear that the &#8220;&lt;&#8221; in the above command does not seem to do anything (works the same without the &#8220;&lt;&#8220;), the reason is that the cat command is smart and knows to look for input on the command line. \u00a0Unless a command is specifically designed to inspect the command line for input arguments, it is necessary to use the &#8220;&lt;&#8221; for redirection of standard input. \u00a0<\/span><\/p>\n<p class=\"c21\"><span class=\"c7\">Consider a more basic example of a simple script requesting input from the keyboard. \u00a0To substitute a file, it would be necessary to issue the following command, where the file &#8220;keystrokes&#8221; contains what the user would have typed.<\/span><\/p>\n<pre id=\"h.p94fyfyj5b4m\" class=\"c21 c11\"><span class=\"c14 c12\">myscript &lt; keystrokes<\/span><\/pre>\n<p class=\"c21 c22\"><span class=\"c7\">Here &#8220;myscript&#8221; represents a user-written Unix script (program), not a Unix command.<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h1 id=\"h.1a28qkewep1a\" class=\"c0\"><span class=\"c26\">Pipes<\/span><\/h1>\n<p class=\"c15\"><span class=\"c7\">It is often the case that a problem in Unix is solved with multiple commands.\u00a0 Typically the output of the first command is saved in a file which is then used as input to a subsequent command. \u00a0The use of a pipe is considered a refinement of this approach potentially simplifying the solution.<\/span><\/p>\n<h2 class=\"c33\"><span class=\"c7\">Problem: To determine the number of entries in a directory<\/span><\/h2>\n<h3>Method 1<\/h3>\n<table class=\"grid\">\n<thead>\n<tr class=\"c9\">\n<th class=\"c18\" colspan=\"1\" rowspan=\"1\" scope=\"col\">\n<p class=\"c1\"><span class=\"c7\">Graphical view<\/span><\/p>\n<\/th>\n<th class=\"c18\" colspan=\"1\" rowspan=\"1\" scope=\"col\">\n<p class=\"c10\"><span class=\"c7\">code<\/span><\/p>\n<\/th>\n<th class=\"c18\" colspan=\"1\" rowspan=\"1\" scope=\"col\">\n<p class=\"c10\"><span class=\"c7\">explanation<\/span><\/p>\n<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr class=\"c9\">\n<td class=\"c18\" colspan=\"1\" rowspan=\"1\">\n<p class=\"c1\">ls -&gt;<br \/>\n<span class=\"c50\">file_list <\/span><br \/>\n<span class=\"c7\">-&gt; wc<\/span><\/p>\n<\/td>\n<td class=\"c18\" colspan=\"1\" rowspan=\"1\">\n<pre id=\"h.ckhvq4jx782t\" class=\"c1 c11\"><span class=\"c14 c12\">ls \/etc &gt; file_list<\/span>\r\n<span class=\"c14 c12\">wc -l file_list<\/span>\r\nrm file_list<\/pre>\n<\/td>\n<td class=\"c18\" colspan=\"1\" rowspan=\"1\">\n<ul class=\"c3 lst-kix_2j8r3h84ts86-0 start\">\n<li class=\"c10 c16 li-bullet-0\"><span class=\"c7\">save output of ls command in file_list<\/span><\/li>\n<li class=\"c10 c16 li-bullet-0\"><span class=\"c7\">run wc command to count lines<\/span><\/li>\n<li class=\"c10 c16 li-bullet-0\"><span class=\"c7\">delete temporary file file_list<\/span><\/li>\n<\/ul>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Method 2<\/h3>\n<table class=\"grid\">\n<thead>\n<tr class=\"c9\">\n<th class=\"c18\" colspan=\"1\" rowspan=\"1\" scope=\"col\">\n<p class=\"c1\"><span class=\"c7\">Graphical view<\/span><\/p>\n<\/th>\n<th class=\"c18\" colspan=\"1\" rowspan=\"1\" scope=\"col\">\n<p class=\"c10\"><span class=\"c7\">code<\/span><\/p>\n<\/th>\n<th class=\"c18\" colspan=\"1\" rowspan=\"1\" scope=\"col\">\n<p class=\"c10\"><span class=\"c7\">explanation<\/span><\/p>\n<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr class=\"c9\">\n<td class=\"c18\" colspan=\"1\" rowspan=\"1\">\n<pre class=\"c33\">ls -&gt;\r\n<span style=\"overflow: hidden; margin: 0.00px 0.00px; border: 0.00px solid #000000; width: 65.31px; height: 20.00px;\">\r\n<img decoding=\"async\" alt=\"\" src=\"http:\/\/pressbooks.library.ryerson.ca\/opsyshiraki\/wp-content\/uploads\/sites\/330\/2022\/03\/image2.png\" style=\"width: 65.31px; height: 20.00px; margin-left: 0.00px; margin-top: 0.00px;\" title=\"\" \/>\r\n<\/span>\r\n<span class=\"c7\">\u00a0-&gt; wc <\/span>\r\n\r\n<span class=\"c7\">\u00a0<\/span><\/pre>\n<\/td>\n<td class=\"c18\" colspan=\"1\" rowspan=\"1\">\n<pre class=\"c17 c36\"><span class=\"c12\">ls \/etc | wc -l<\/span><\/pre>\n<\/td>\n<td class=\"c18\" colspan=\"1\" rowspan=\"1\">\n<ul class=\"c3 lst-kix_2j8r3h84ts86-0\">\n<li class=\"c10 c16 li-bullet-0\"><span class=\"c7\">save output of ls command is sent directly to input of wc command<\/span><\/li>\n<li class=\"c10 c16 li-bullet-0\"><span class=\"c7\">no temporary file needed<\/span><\/li>\n<\/ul>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"c20\"><span class=\"c7\">\u00a0<\/span><\/p>\n<div class=\"textbox\">\n<h2 class=\"c33\"><span class=\"c37\">Definition:<\/span><\/h2>\n<p class=\"c33\"><span class=\"c7\">A pipe connects STDOUT of previous command to STDIN of next command<\/span><\/p>\n<\/div>\n<p class=\"c33\"><span class=\"c7\">You can use a pipe multiple times creating a pipeline.<\/span><\/p>\n<pre class=\"c30\">e.g.\r\n<span class=\"c14 c12\">cmd1 | cmd2 | cmd3 | cmd4<\/span><\/pre>\n<p class=\"c33\"><span class=\"c7\">Building a pipeline should be an iterative process. Condense stepwise as you know the solutions work, otherwise there might be errors that might be difficult to detect from a single pipeline<\/span><\/p>\n<p class=\"c33\"><span class=\"c7\">Start out like this:<\/span><\/p>\n<pre id=\"h.69mc4iy1lwkm\" class=\"c0\"><span class=\"c14 c12\">\r\ncmd1 &gt; out1<\/span>\r\ncmd2 &lt; out1 &gt; out2\r\ncmd3 &lt; out2<\/pre>\n<p>&#8230;<\/p>\n<p class=\"c20\"><span class=\"c7\">\u00a0<\/span><\/p>\n<div class=\"textbox textbox--key-takeaways\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\">Key Takeaways<\/p>\n<\/header>\n<div class=\"textbox__content\">\n<ol>\n<li class=\"c17 c36\"><span class=\"c19\">\u00a0<\/span><span class=\"c37\">Redirection<\/span><span class=\"c7\">: Use between a command and a file<\/span><\/li>\n<li class=\"c17 c36\">\u00a0Pipe<span class=\"c7\" style=\"text-align: initial; font-size: 1em;\">: Use between commands<\/span><\/li>\n<\/ol>\n<\/div>\n<\/div>\n<hr \/>\n<h1 class=\"c2\"><span class=\"c7\">\u00a0<\/span><span class=\"c5\">Making your script executable<\/span><\/h1>\n<ol class=\"c3 lst-kix_w9cpc76ivqci-0 start\" start=\"1\">\n<li class=\"c15 c16 li-bullet-0\"><span class=\"c7\">create file containing unix commands<\/span><\/li>\n<li class=\"c15 c16 li-bullet-0\"><span class=\"c7\">Once per file, type either:<\/span>\n<ul class=\"c3 lst-kix_w9cpc76ivqci-0 start\">\n<li><span class=\"c7\">chmod u+x myscript<\/span><\/li>\n<li><span class=\"c7\">chmod 700 myscript<\/span><\/li>\n<\/ul>\n<\/li>\n<li class=\"c15 c16 li-bullet-0\"><span class=\"c7\">To run, type .\/myscript<\/span><\/li>\n<\/ol>\n<p class=\"c2\"><span class=\"c7\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"author":2,"menu_order":3,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":[],"pb_section_license":""},"chapter-type":[],"contributor":[],"license":[],"class_list":["post-34","chapter","type-chapter","status-publish","hentry"],"part":23,"_links":{"self":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/pressbooks\/v2\/chapters\/34","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/wp\/v2\/users\/2"}],"version-history":[{"count":20,"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/pressbooks\/v2\/chapters\/34\/revisions"}],"predecessor-version":[{"id":104,"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/pressbooks\/v2\/chapters\/34\/revisions\/104"}],"part":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/pressbooks\/v2\/parts\/23"}],"metadata":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/pressbooks\/v2\/chapters\/34\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/wp\/v2\/media?parent=34"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/pressbooks\/v2\/chapter-type?post=34"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/wp\/v2\/contributor?post=34"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/wp\/v2\/license?post=34"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}