return to OCLUG Web Site
A Django site.
June 10, 2009

Bart Trojanowski
Bart's Blog
» how would you read a file into an array of lines

I was working on a shell script that needed to look at some lines in a bunch of files and perform some data mining. I started it in bash, so I am writing it in bash even though dave0 notes that I should have started in in perl. Point taken... I suffer for my mistake.

After a quick google I learned that a nice way to do what the topic of this post describes can be done using

    declare -a foo=( $(cat $file) )

Which is great! Right?

[Read More]

March 21, 2009

Bart Trojanowski
Bart's Blog
» shrinking URLs

I wrote a short script to shrink URLs:

    % shorturl

    % shorturl
    Type in some urls and I'll try to shrink them for you...

I am doing this as part of my new addiction^W usage and extending GregKH's command line micro blogging tool.

June 2, 2008

» Emacs Keybindings in Bash

Or, How to Be a Command-Line Commando

Does it surprise you to learn that I’m a Linux guy? I’ve been using Linux, to the exclusion more-or-less of everything else, since about 1999. In the past, I’ve done a little programming and some junior system administration. I’m even LPI-certified.

With this background, I’m quite comfortable working in the shell (AKA the command-line), the natural habitat of the sysadmin[1]. I frequently open a shell to do some quick work, and when I do, I use GNU’s Bash, which is the default on most Linux distributions. (I believe it’s also the default shell in Mac OS X.)

One of Bash’s features is editable command-line history, which makes your current command-line and its entire history available to you as an editable buffer. That offers a great way to streamline your work in the shell.

I suspect, however, that many shell users don’t even know about this better way. And it baffles me that many SAs I have seen in action — including some of Pythian’s own — don’t use this. They almost seem to prefer unnecessary effort — smashing away at their keyboards, repeating themselves, deleting with the Backspace key, scrolling, forwarding their cursor one character at a time, copying and pasting with the mouse, and so on. That’s a lot of elbow grease.

With Bash, or any other shell that uses the GNU readline library, you can use the following Emacs-like key-chords to make your life better. The point of this (as with so many things sysadmins and programmers do) is to save you effort, viz. typing. These aren’t all of them; they’re the ones I use:

Keys Effect
CTRL-P go to the Previous command in your history
CTRL-N go to the Next command in your history
CTRL-R Reverse-search through your history
CTRL-S Search forward through your history
CTRL-A Move the cursor to the beginning of the line
CTRL-E Move the cursor to the end of the line
CTRL-W delete a Word backwards
ALT-D delete a word forwards
CTRL-F move the cursor Forward 1 character
CTRL-B move the cursor Backward 1 character
ALT-F move the cursor Forward 1 word
ALT-B move the cursor Backward 1 word
ALT-_ undo

It takes a little learning to get these under your fingers, but it’s worth it. Too often, easy-to-use GUIs get us started quickly, and then leave us handicapped in our work. Spending the time to learn how to use capable tools pays off.

If you don’t want the default Emacsisms, there’s a vi-mode too — try set -o vi. I am a vi guy (in addition to a Linux guy), so I don’t mind modal editing, except that in the shell, there’s no way to tell which mode I’m in at any moment. Some commando out there might have put together a fancy custom PS1 prompt that shows vi-modes, but I really haven’t gone searching.

If you’d like a more thorough read about this, try the Bash Emacs Editing Mode (readline) Cheat Sheet or “man readline”.

1. As it happens, knowing how to use Unix text tools such as grep, sed, and wc (to name just three) is very helpful to a writer, too.

March 21, 2008

» Saying What You Mean

Ah, the perils of working in a shared, client environment. One client has us using a login that is not exclusive to us. I prefer using bash; the client is set to use zsh. This is not a problem in and of itself.

However, there is a section in the .profile that is causing me issues:

if [ -f /usr/bin/ksh ]; then
        /usr/bin/ksh -o vi

So, “If ksh exists, run it with some options to edit history with vi-like commands”. Except what we really want is “If you’re using the ksh as a shell, . . . .”

So I added a modification, and now all is fine.

if [ -f /usr/bin/ksh ]; then
        if [ "$SHELL" = "/usr/bin/ksh" ]; then
                /usr/bin/ksh -o vi

(not all my problems are MySQL related!)