Friday, March 2, 2007

Fun Times in the CLI

I discovered a few things in the last day that I found to be preponderously fun. Many familiar with the CLI may know them, but I did not, and so I shall share.

The first thing I still don't completely understand, but it works. If you run a script that has lots of requests for input with defaults that you want to accept, there is a better way to run it than "./" and hitting enter a lot. Imagine if it was a script you wanted to run on 20 remote machines to configure something. We need a better way. And it comes in the form of linux's own black hole: /dev/null.

If you redirect from /dev/null to a running script (and to other things, although this is all I have tried), it somehow supplies an equivalent to pressing the Enter key. So you can start the script like "./ < /dev/null", and any prompts will be answered with the defaults. I have not been able to get a good explanation of how this works, aside from something about it providing STDIN to the thing it pipes to, but it does work. Beautiful Black Magic.

Another trick is less spine-tingling, but arguably more helpful. Sometimes after typing in a long command, I decide I need to do something else, change the middle of the command, etc. Basically, sometimes I want what I have typed gone, and fast. Usually, I press the Up arrow, hoping to Linus that I entered ls recently, and thus only have to press backspace twice. If I am unlucky, I must resort to holding backspace. Dumb. But, again, there is a better way! Namely, press CTRL+a, then k. You can either hold CTRL+a+k, or hold CTRL+a, release a, and press k with CTRL held. If you release CTRL and a, k will just be entered as a character, so don't do that. Another, shorter, method is CTRL+u. This does the same thing as CTRL+a, k on most systems. I have found that it does not work on certain older distros, so knowing the CTRL+a, k is useful as well. In addition, I found that CTRL+w erases the last word, as defined by a block of characters without a space or tab. This can be helpful if you just botched the last part of a command.

One last mot(e) of coolness. "-A" is a VERY helpful option for grep. If you invoke grep with "-A SOMENUMBER", it will print not only the line matching your text you are grepping for, but also the next SOMENUMBER lines. If there are multiple matches, each match and the specified number of context lines will be separated by a "--" line. I found this so helpful because in a lot of things grep, I don't just want the line the matched string is on, but a little context as well. Options to the rescue.


Blogger by Immortal Curiosity said...

Update: I did come across a shorter way to clear the entire line, from this wonderful primer. CTRL + u was what I was looking for. It clears the entire line.

March 18, 2007 at 8:05 PM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home