This blog is about the Linux Command Line Interface (CLI), with an occasional foray into GUI territory.
Instead of just giving you information like some man page, I hope to illustrate each command in real-life scenarios.

Search This Blog

Sunday, March 30, 2008

bash is a very powerful scripting language. However, there is a learning curve that frustrates many.

In a series of bash quicksand blog entries, I hope to post some warning signs about what to avoid. I want to write them down because after one gets used to bash syntax, one tends to forget about the initial hurdles. Hopefully, no late comers will be trapped in bash quicksand.

Let's begin.

Say you want to assign the text string myvalue to some variable myvariable. What is more simple than that?

$ myvariable = myvaluebash: myvariable: command not found$

Oops. And you vowed that it worked before.

What transpired was that bash attempts to execute the command myvariable with the arguments = and myvalue.

Saturday, March 22, 2008

If you write bash scripts a lot, you are bound to run into a situation where you want to extract some lines from a file. Yesterday, I needed to extract the first line of a file, say named somefile.txt.

$ cat somefile.txt
Line 1
Line 2
Line 3
Line 4

This specific task can be easily done with this:

$ head -1 somefile.txt
Line 1

For a more complicated task, like extract the second to third lines of a file. head is inadequate.

So, let's try extracting lines using sed: the stream editor.

My first attempt uses the p sed command (for print):

$ sed 1p somefile.txt
Line 1
Line 1
Line 2
Line 3
Line 4

Note that it prints the whole file, with the first line printed twice. Why? The default output behavior is to print every line of the input file stream. The explicit 1p command just tells it to print the first line .... again.

To fix it, you need to suppress the default output (using -n), making explicit prints the only way to print to default output.

$ sed -n 1p somefile.txt
Line 1

Alternatively, you can tell sed to delete all but the first line.

$ sed '1!d' somefile.txt
Line 1

'1!d' means if a line is not(!) the first line, delete.

Note that the single quotes are necessary. Otherwise, the !d will bring back the last command you executed that starts with the letter d.

To extract a range of lines, say lines 2 to 4, you can execute either of the following:

$ sed -n 2,4p somefile.txt

$ sed '2,4!d' somefile.txt

Note that the comma specifies a range (from the line before the comma to the line after).
What if the lines you want to extract are not in sequence, say lines 1 to 2, and line 4?

$ sed -n -e 1,2p -e 4p somefile.txt
Line 1
Line 2
Line 4

If you know some different ways to extract lines in a file, please share with us by filling out a comment.
P.S. Related articles from this blog:

Thursday, March 6, 2008

My last blog entry explains how to use xmodmap to remap the Caps Lock key to the Escape key in X. That takes care of the keyboard mapping when you are in X. What about when you are in a virtual console window? You need to follow the steps below. Make sure that you sudo root before you execute the following commands.

Find out the keycode of the key that you want remapped.

Execute the showkey command as root in a virtual consolde:

$ showkeykb mode was UNICODE

press any key (program terminates after 10s of last keypress)...0x9c

Hit the Caps Lock key, wait 10 seconds (default timeout), and the showkey command will exit on its own.

$ showkeykb mode was UNICODE

press any key (program terminates after 10s of last keypress)...0x9c0x3a 0xba

The keycode for the Caps Lock key is 0x3a in hex, or 58 in decimal.

Find out the symbolic name (key symbol) of the key that you want to map to. You can list all the supported symbolic names by dumpkeys -l and grep for esc:

Thanks to cjwatson who pointed me to prepending the keymaps statement from dumpkeys. The keymaps statement is a shorthand notation defining what key modifiers you are defining with the key. See man keymaps(5) for more info.

To make the new key mapping permanent, you need to put the loadkeys command in a bootup script.

Monday, March 3, 2008

Caps Lock is in my opinion one of the most useless keys on a keyboard. Unless you own one of those keyboards specifically designed for Linux, your keyboard most likely has a Caps Lock key located in a prime real estate area: right above the Shift key.

How do we remap the Caps Lock key to something more useful, say the Esc key? Why the Esc key? See my earlier article.

First, specify the new key mapping in the file ~/.Xmodmap:

$ cat >> ~/.Xmodmapremove Lock = Caps_Lockkeysym Caps_Lock = Escape

Note that the file ~/.Xmodmap may not pre-exist in your distro. The above command will create the file.

Next, execute those new mappings by:

$ xmodmap ~/.Xmodmap

Test it out by opening a command window in X. Type in some words on the command line. Press the new Esc key(formerly Caps Lock), and then the key b. This should move the cursor back a word.

The above will do the key remapping for your X-Window environment. The key mapping is not changed for your console windows. We will save that topic for another day.