<?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: Tag CLI</title>
    <link>http://blog.kineticweb.com/articles/tag/cli</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Musings from a Ruby on Rails development team</description>
    <item>
      <title>Command Line Tips (part I: simple key bindings)</title>
      <description>&lt;p&gt;Working on the command line can seem daunting and 
slow.  With a few handy tips working on the command
line can be productive and efficient.&lt;/p&gt;


	&lt;p&gt;This entry is the first of a few to cover some of the command line techniques I use to be more productive on the command line.&lt;/p&gt;


	&lt;p&gt;Before we get started, there are two things I should  cover.&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;It&amp;#8217;s all bash/ sh.  Other shells are great, but
   I don&amp;#8217;t know them.  Bash is pretty much the
   default in Linux distros these days, which is
   probably why I wound up learning it.&lt;/li&gt;
		&lt;li&gt;This is geared toward the beginner, and is aimed
   at interactive use as opposed to programming.&lt;/li&gt;
		&lt;li&gt;[ctrl] commands are a simultaenous press.  Press both
  keys at the same time.&lt;/li&gt;
		&lt;li&gt;[esq] commands require that you press escape, then let off,
  then press the next key.&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;The most general key bindings used in bash are for
history navigation and command editing.  The ones
I commonly use are:&lt;/p&gt;


	&lt;table&gt;
		&lt;tr&gt;
			&lt;td&gt;[arrow up] or [ctrl]-p &lt;/td&gt;
			&lt;td&gt; Move back in command history &lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;[arrow down] or [ctrl]-n&lt;/td&gt;
			&lt;td&gt; Move forward in command history&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;[ctrl]-a&lt;/td&gt;
			&lt;td&gt;move cursor to begining of line&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;[ctrl]-e&lt;/td&gt;
			&lt;td&gt;move cursor to end of line&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;[ctrl]-k&lt;/td&gt;
			&lt;td&gt;erase to end of line&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;[esq]-b&lt;/td&gt;
			&lt;td&gt;skip back one word&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;[esq]-f&lt;/td&gt;
			&lt;td&gt;skip forward one word&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;[esq]-d&lt;/td&gt;
			&lt;td&gt;erase word to right of cursor&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;[ctrl]-r&lt;/td&gt;
			&lt;td&gt;backward history search*&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;




	&lt;p&gt;The [ctrl]-r requires some explanation.&lt;/p&gt;


	&lt;p&gt;When you hit [ctrl]-r, you are in a history search mode,
typing further characters performs an auto-complete style
search of your command history.&lt;/p&gt;


	&lt;p&gt;By typing &amp;#8216;if&amp;#8217; at this prompt, I find that I have a match
&amp;#8217;/sbin/ifconfig -a&amp;#8217;, which I&amp;#8217;d recently executed.&lt;/p&gt;


&lt;pre&gt;
(reverse-i-search)`': 
(reverse-i-search)`if': /sbin/ifconfig -a
&lt;/pre&gt;

	&lt;p&gt;Pressing enter then executes the command.  Other keys like cursor navigation will bring you back to regular command prompt mode, and allow you to edit the command.&lt;/p&gt;


	&lt;p&gt;Good luck and have fun!&lt;/p&gt;</description>
      <pubDate>Sun, 22 Jul 2007 11:26:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:0689e0a6-9f88-436c-b8f7-01704caadb09</guid>
      <author>Andy Libby</author>
      <link>http://blog.kineticweb.com/articles/2007/07/22/command-line-tips-part-i-simple-key-bindings</link>
      <category>CLI</category>
      <trackback:ping>http://blog.kineticweb.com/articles/trackback/48</trackback:ping>
    </item>
    <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>
  </channel>
</rss>
