{"id":113,"date":"2022-05-07T20:09:15","date_gmt":"2022-05-07T20:09:15","guid":{"rendered":"https:\/\/pipeawk.com\/?p=113"},"modified":"2022-05-13T17:45:44","modified_gmt":"2022-05-13T17:45:44","slug":"standard-input-stdin-output-stdout-and-error-stderr-explained-part-2","status":"publish","type":"post","link":"https:\/\/pipeawk.com\/index.php\/2022\/05\/07\/standard-input-stdin-output-stdout-and-error-stderr-explained-part-2\/","title":{"rendered":"Standard Input (STDIN), Output (STDOUT), and Error (STDERR) Explained (part 2)"},"content":{"rendered":"<p>Ok, after the long part 1, we continue our journey with Standard Input or <strong>STDIN<\/strong>. Following the concepts we discussed in part 1, we know that <strong>STDIN<\/strong> has a standard file descriptor of 0. In other words, every process created by the kernel in UNIX, has file descriptor 0 assigned to <strong>STDIN<\/strong>.<\/p>\r\n<p><strong>STDIN<\/strong> is the way a process or a command reads the provided input to perform some action, to then, produce <strong>STDOUT<\/strong>, <strong>STDERR<\/strong>, or sometimes nothing (In UNIX nothing often means success).<!--more--><\/p>\r\n<p>Let&#8217;s dig into a practical example by continuing to expand the use of the <strong>ls<\/strong> command, but this time, we are going to use the STDOUT of the <strong>ls<\/strong> command as STDIN to another wonder UNIX command called &#8220;<strong>grep<\/strong>&#8220;. Do not worry much about what grep does, I will write a post about &#8220;<strong>grep<\/strong>&#8221; and the amazing things you can do with it. For now, imagine you can use &#8220;<strong>grep<\/strong>&#8221; to capture and display the input which contain a particular string.<\/p>\r\n<p>The command we are going to investigate is:<\/p>\r\n<blockquote>\r\n<p>ls -l \/ | grep &#8220;tmp&#8221;<\/p>\r\n<\/blockquote>\r\n<p>This command simple lists the expanded content of the root directory &#8220;\/&#8221;, and forwards the output by using the <strong>pipe<\/strong> &#8220;|&#8221;, to the &#8220;<strong>grep<\/strong>&#8221; command. The &#8220;<strong>grep<\/strong>&#8221; command uses STDIN to read the output provided by the &#8220;<strong>ls<\/strong>&#8221; command, and checks every line if it contains the string &#8220;<strong>tmp<\/strong>&#8220;, if the line contains the string, is printed, otherwise, it is discarded.<\/p>\r\n<p>When you execute the command, you should get something like:<\/p>\r\n<blockquote>\r\n<div>\r\n<div>drwxrwxrwt 1 root root 512 May 6 21:59 tmp<\/div>\r\n<\/div>\r\n<\/blockquote>\r\n<div>\r\n<div>Now you would say, what&#8217;s the big deal? Well, to understand the concept better, let&#8217;s eliminate the pipe and the &#8220;grep&#8221; command, so let&#8217;s only type the following command:<\/div>\r\n<blockquote>\r\n<div>ls -l \/<\/div>\r\n<\/blockquote>\r\n<div>Now we get a much larger output from the command:<\/div>\r\n<blockquote>\r\n<div>\r\n<div>\r\n<pre>total 620<br \/>lrwxrwxrwx 1 root root 7 Mar 24 17:40 bin -&gt; usr\/bin<br \/>drwxr-xr-x 1 root root 512 Mar 24 17:47 boot<br \/>drwxr-xr-x 1 root root 512 May 7 15:55 dev<br \/>drwxr-xr-x 1 root root 512 May 7 15:55 etc<br \/>drwxr-xr-x 1 root root 512 Apr 9 16:31 home<br \/>-rwxr-xr-x 1 root root 632096 Apr 9 10:52 init<br \/>lrwxrwxrwx 1 root root 7 Mar 24 17:40 lib -&gt; usr\/lib<br \/>lrwxrwxrwx 1 root root 9 Mar 24 17:40 lib32 -&gt; usr\/lib32<br \/>lrwxrwxrwx 1 root root 9 Mar 24 17:40 lib64 -&gt; usr\/lib64<br \/>drwxr-xr-x 1 root root 512 Mar 24 17:40 media<br \/>drwxr-xr-x 1 root root 512 Apr 9 16:27 mnt<br \/>dr-xr-xr-x 9 root root 0 May 7 15:55 proc<br \/>drwx------ 1 root root 512 Apr 11 11:14 root<br \/>drwxr-xr-x 1 root root 512 May 7 15:55 run<br \/>lrwxrwxrwx 1 root root 8 Mar 24 17:40 sbin -&gt; usr\/sbin<br \/>drwxr-xr-x 1 root root 512 Mar 24 17:42 snap<br \/>drwxr-xr-x 1 root root 512 Mar 24 17:40 srv<br \/>dr-xr-xr-x 12 root root 0 May 7 15:55 sys<br \/>drwxrwxrwt 1 root root 512 May 6 21:59 tmp<br \/>drwxr-xr-x 1 root root 512 Mar 24 17:41 usr<br \/>drwxr-xr-x 1 root root 512 Mar 24 17:42 var<\/pre>\r\n<\/div>\r\n<\/div>\r\n<\/blockquote>\r\n<div>\r\n<p>Do you see the difference? By adding the pipe &#8220;|&#8221; and the &#8220;grep&#8221; command which filters the presence on the string &#8220;tmp&#8221;, the rest of the output is discarded.<\/p>\r\n<p>To conclude, grep used STDIN to read the larger output produce by the &#8220;ls -l \/&#8221; command, read it, and excluded all the lines not containing &#8220;tmp&#8221;.<\/p>\r\n<p>STDIN can be feed to a program also by applying the &#8220;&lt;&#8221; redirection. Remember in part 1? We used &#8220;&gt;&#8221; to redirect the output to a file? This does exactly the inverse, reads from the file, and redirects the content of the file to STDIN. For example:<\/p>\r\n<blockquote>\r\n<p>grep &#8220;tmp&#8221; &lt; \/tmp\/filetoparse.txt<\/p>\r\n<\/blockquote>\r\n<p>Hope by now, STDIN, STDOUT, and STDERR are clearer. The best way to expand your knowledge on the subject is by practicing with the commands.<\/p>\r\n<p>Thanks for reading and type away!!!<\/p>\r\n<\/div>\r\n<\/div>\r\n","protected":false},"excerpt":{"rendered":"<p>Ok, after the long part 1, we continue our journey with Standard Input or STDIN. Following the concepts we discussed in part 1, we know that STDIN has a standard file descriptor of 0. In other words, every process created by the kernel in UNIX, has file descriptor 0 assigned to STDIN. STDIN is the &hellip; <a href=\"https:\/\/pipeawk.com\/index.php\/2022\/05\/07\/standard-input-stdin-output-stdout-and-error-stderr-explained-part-2\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Standard Input (STDIN), Output (STDOUT), and Error (STDERR) Explained (part 2)&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-113","post","type-post","status-publish","format-standard","hentry","category-unix"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/pipeawk.com\/index.php\/wp-json\/wp\/v2\/posts\/113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pipeawk.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pipeawk.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pipeawk.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pipeawk.com\/index.php\/wp-json\/wp\/v2\/comments?post=113"}],"version-history":[{"count":6,"href":"https:\/\/pipeawk.com\/index.php\/wp-json\/wp\/v2\/posts\/113\/revisions"}],"predecessor-version":[{"id":131,"href":"https:\/\/pipeawk.com\/index.php\/wp-json\/wp\/v2\/posts\/113\/revisions\/131"}],"wp:attachment":[{"href":"https:\/\/pipeawk.com\/index.php\/wp-json\/wp\/v2\/media?parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pipeawk.com\/index.php\/wp-json\/wp\/v2\/categories?post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pipeawk.com\/index.php\/wp-json\/wp\/v2\/tags?post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}