about | contact | disclaimer | home   

S.KRAUSE

Computing with Krause #1

What I did not have to miss after giving my Linux-running Inspiron to my brother and purchasing my iBook G4 a while back was the joy of of a bash shell. Or rather, just bash, since otherwise it would be Bourne Again Shell shell. ATM machine, PIN number ... the the tar tar pits.

Composite of women by Paul Gillon, French illustrator / comic artist, borrowed/taken from The Rules of Attraction
by Paul Gillon

The relative merits of bash, sh, ksk, zsh, csh, tcsh and the rest can be argued, although as far as I'm concerned it comes down to bash and zsh, since zsh replaces ksh, bash replaces sh, and arguing for the interactive use of csh or tcsh is a sign of mental illness.

I use bash, I admit, because it is what I know; the admirer of nifty technology in me whispers in my ear about zsh, but I haven't yet found a good enough reason to expend effort in learning it and thus switch.

alias dush="du -sh"
alias dfh="df -h"

The next step after using the shell for day-to-day computing is first putting a few aliases in .profile and second creating a growing directory of short scripts that, like aliases, function as shorthand for common operations.

seq:
!/bin/bash

# A shell-script utility for printing a sequence of integers.
# Features include:
# -- if the 2nd number is larger than the 1st, an increasing sequence
# -- if the 2nd number is less than the 1st, a decreasing sequence
# -- padding of zeroes

LOWER=$1
UPPER=$2
LENGTH=${#UPPER}

while [ $LOWER -le $UPPER ]
do
          printf "%0${LENGTH}d " "$LOWER"
          LOWER=$((LOWER + 1))
done

Many of mine are just recorded and often used for i in ... loops, such as resizing a bunch of digital photos, creating a directory of thumbnails, or doing batch text edits.

fib:
#!/bin/sh

if (( $# > 0 ))
then
          BOUND=$1
else
          BOUND=5
fi

LIMIT="1"
FIBA="1"
FIBB="0"

while [ $LIMIT -le $BOUND ]
do
          FIBC="$FIBA"
          FIBA=`echo "$FIBA+$FIBB" | bc`
          echo "$LIMIT $FIBA"
          LIMIT=$((LIMIT + 1))
          FIBB="$FIBC"
done

A couple waste-of-time ones are simple factorial and Fibonacci number generators. The joy of these is that they are rather simple math, but at the same time the particular algorithms used, while straight forward, are rather inefficient.

factorial:
#!/bin/sh

if (( $# > 0 ))
then
          BOUND=$1
else
          BOUND=5
fi

LIMIT="1"
FACT="1"

while [ $LIMIT -le $BOUND ]
do
          FACT=`echo "$LIMIT*$FACT" | bc`
          echo "$LIMIT" "$FACT"
          LIMIT=$((LIMIT + 1))
done

I have this desire to write more complex scripts, mostly for fun. Practical examples and applications, including some specific to Mac OS X, can be found in Dave Taylor's Wicked Cool Shell Scripts (San Francisco: No Starch Press, 2004), though while I've read the book cover-to-cover I haven't actually used any of his projects. On the other hand I do have a need to write some more complex scripts, mostly for the sake of easing file management.

for i in [file set]
do
          mv "$i" "`echo "$i" | sed -e 's/[0-9]... //' [more sed] | tr A-Z a-z`"
done

In particular I need to improve my knowledge of regular expressions; I can do the absolute basics, but the more complex pattern matching is stuff I haven't played with.

The other day I realized that tree $music_directory | grep ".mp3" | wc -l was a quick and dirty way to count how many mp3 files you have in a directory tree.

I need to refresh my memory regarding the commandline tools for extracting meta data from image files (identify $file_name is a beginning, piping to awk to collect only the relevant information) and for altering the creation date for a file (I believe it is touch—I have some images from my digital camera that have the wrong timestamps because I forgot for a while to reset the date/time on the camera when I returned to the U.S. from Berlin), as well as for altering the meta data within a jpg (in particular creation date/time).

—January 18 2007