Tuesday, August 25, 2009

linux: creating and using arrays in shell scripts

Bash arrays have numbered indexes only, but they are sparse, i.e. you don't have to define all the indexes.

An entire array can be assigned by enclosing the array items in parenthesis:
arr=(Hello World)

Individual items can be assigned with the familiar array syntax (unless you're used to Basic or Fortran):
arr[0]=Hello
arr[1]=World


But it gets a bit ugly when you want to refer to an array item:
echo ${arr[0]} ${arr[1]}

To quote from the man page:

The braces are required to avoid conflicts with pathname expansion.

In addition the following funky constructs are available:
${arr[*]};  # All of the items in the array
${!arr[*]}; # All of the indexes in the array
${#arr[*]}; # Number of items in the array
${#arr[0]}; # Length of item zero

The ${!arr[*]} is a relatively new addition to bash, it was not part of the original array implementation.

source

Monday, August 24, 2009

vim: commenting-out lines of code esaily

For short ranges of basic prefixing comments (# or //), I've become fond of vim's visual block mode and block insert. To do this:
  1. start visual block mode (with Ctrl-V)
  2. adjust the selection with movement keys as needed. (marks can be used here)
  3. once the selections are made, start block insert with 'I' (or append with 'A')
    NOTE: don't use the usual lowercase 'i' or 'a'
  4. type the comment text (e.g. "#")
  5. when you hit escape, comments will be added to all selected lines

source

Tuesday, August 11, 2009

nice quote about indesign

Adobe has always said that InDesign is “the future of page layout”—but we think they’re selling themselves a bit short. With InDesign, the future is here today.

And, to our eyes, at least, it looks pretty cool.

source: "Real World Adobe InDesign CS4" (Kvern and Blatner)

Monday, August 3, 2009

linux: rename multiple files with one command

We're going to do this simple task with a simple regex find+replace in sed:

for i in *.avi
do
    j=`echo $i | sed 's/mymovies/myflicks/g'`
    mv "$i" "$j"
done

Can also be written on a single line as:
for i in *.avi; do j=`echo $i | sed 's/find/replace/g'`; mv "$i" "$j"; done

source