Archive for June, 2009

Bash Shell Script to Loop Over Set of Files

I’m often asked how to write a shell script that loops over a set of files in the current directory and perform some actions on them. Really, nothing is simpler than that, it’s just that most administrators or developers don’t take the time to get into shell scripting.

The simplest form is this, simply change the wildcard characters to match your requirements:

#!/bin/bash
FILES="*.txt"
for f in $FILES
do
  echo "Processing $f ..."
  # perform some action on each file. $f stores the current file name
  echo "Done processing $f ..."
done

INSERT IF NOT EXISTS in MySql

INSERT IGNORE is the syntax that does mimic INSERT IF NOT EXISTS, as there is no direct command as stated in the title, at least not in the current release of MySql.
The statement INSERT IGNORE (and to some extent REPLACE INTO) does essentially the same thing, inserting a record if that given record does not exists.
See the following samples:


(more…)