<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>has_many :thoughts: Pipes and commands</title>
    <link>http://blog.kineticweb.com/articles/2007/06/08/un-x-stdio</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Musings from a Ruby on Rails development team</description>
    <item>
      <title>Pipes and commands</title>
      <description>&lt;p&gt;I received a question from a co-worker the other day about why &amp;#8220;&lt;code&gt;ls | grep [something]&lt;/code&gt;&amp;#8221; 
returns single file names.  When ls is run by itself in a terminal window, it shows output in columns (multiple files per line).  The &lt;code&gt;grep&lt;/code&gt; command works at the line level, not the field level.  Why does &lt;code&gt;ls | grep&lt;/code&gt; not return a line with multiple file names?&lt;/p&gt;


	&lt;p&gt;Commands like ls can detect if the input or output streams (standard input and 
standard output) are pipes.  Armed with this information they alter their output to be more sensible for the context in which they are run.&lt;/p&gt;


	&lt;p&gt;The following example C program demonstrates:&lt;/p&gt;


&lt;pre&gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;sys/types.h&amp;gt;
#include &amp;lt;sys/stat.h&amp;gt;
#include &amp;lt;unistd.h&amp;gt;

int main() {
    struct stat *st = malloc(sizeof stat);
    if(fstat(1, st) != 0) {
        fprintf(stderr, "Could not stat standard output...\n");
        exit(1);
    }

    if(S_ISFIFO(st-&amp;gt;st_mode)) {
        printf("Standard output is a pipe.\n");
    } else {
        printf("Standard outpt is not a pipe.\n");
    }
}

&lt;/pre&gt;

	&lt;p&gt;I then compiled and ran the program both with and without
a pipe:&lt;/p&gt;


&lt;pre&gt;
alibby@alibby-laptop:~$ gcc -o pipedetect pipedetect.c
alibby@alibby-laptop:~$ ./pipedetect 
Standard outpt is not a pipe.
alibby@alibby-laptop:~$ ./pipedetect | cat
Standard output is a pipe.
alibby@alibby-laptop:~$ 

&lt;/pre&gt;

	&lt;p&gt;So &lt;code&gt;ls&lt;/code&gt; and other commands are nice enough to detect when a pipe is being used on 
standard out and present one line.  In this case the &lt;code&gt;ls&lt;/code&gt; command presents one file 
per line (which is what we want when &lt;code&gt;grep&lt;/code&gt;ping).&lt;/p&gt;


	&lt;p&gt;&lt;span class="caps"&gt;GNU&lt;/span&gt; &lt;code&gt;ls&lt;/code&gt; is nice enough to provide the &lt;code&gt;--format=single&lt;/code&gt; option to provide this behavior
when a pipe is not used.&lt;/p&gt;


	&lt;p&gt;For more information about what&amp;#8217;s going on in the C app above, you can consult
manual pages for &lt;a href="http://www.linuxmanpages.com/man2/stat.2.php"&gt;&lt;code&gt;stat(2)&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;</description>
      <pubDate>Fri, 08 Jun 2007 11:52:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:e612c58d-e844-4e28-a874-51e13c8f062b</guid>
      <author>Andy Libby</author>
      <link>http://blog.kineticweb.com/articles/2007/06/08/un-x-stdio</link>
      <category>CLI</category>
      <trackback:ping>http://blog.kineticweb.com/articles/trackback/41</trackback:ping>
    </item>
    <item>
      <title>"Pipes and commands" by Colin</title>
      <description>@Justin: Exactly. I wanted to &lt;code&gt;grep&lt;/code&gt; through the output of an &lt;code&gt;ls&lt;/code&gt; and I was surprised when this worked. I figured that, since &lt;code&gt;ls&lt;/code&gt; would spit out multiple file names per line, the &lt;code&gt;grep&lt;/code&gt; wouldn't be that useful. But alas, I am proven wrong once again by resourcefulness of open source.</description>
      <pubDate>Sun, 10 Jun 2007 18:24:34 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:b289ca82-b54d-4ec5-a645-bfefee330960</guid>
      <link>http://blog.kineticweb.com/articles/2007/06/08/un-x-stdio#comment-62</link>
    </item>
    <item>
      <title>"Pipes and commands" by Justin</title>
      <description>The reason thats the default behavior is because its easier to parse through other command line scripting, correct?</description>
      <pubDate>Fri, 08 Jun 2007 14:24:20 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:0941d3ae-610f-45c7-aaf3-b7b7d2de83e5</guid>
      <link>http://blog.kineticweb.com/articles/2007/06/08/un-x-stdio#comment-61</link>
    </item>
  </channel>
</rss>
