Monday, January 12, 2009

Bash has C-like for loops?!

My life just got a lot easier finding this out.

We have an OCR package and 65 directories of tiffs we want to extract the text of, and we want to run a few jobs in parallel to get the work done faster and take advantage of all this multi-core business, but obviously we don't want to run all 65 processes at once, just 5 or so.

The poor bastard who runs our scanners had a script where he had just copied and pasted the command line over and over with the different directory names.

Here's a script that does the same thing in a nested 'for' loop.


#!/bin/bash
LIMIT=65

for ((i=1; i <= LIMIT ; i = i + 5)) # Double parentheses, and "LIMIT" with no "$".
do
  for ((j=0; j < 5; j++))
  do
    imageList=" "
    for image in islmagfull/$[i + j]/*; do
      # Get all the files in the directory and build the image list to pass to the command line.
      imageList="$imageList -if $image"
    done

    ./CLI $imageList -f PDF -pem ImageOnText -pfpf Automatic -pfq 85 -pfpr 200 -of "/usr/local/fedora/abbyy/${image%.*}.pdf" & # Ampersand means run the process in the background.
  done
  wait # 'wait' is awesome, it just pauses until all child processes are done. I love Unix.
done # A construct borrowed from 'ksh93'.


I should start a wiki to store code snipits, just using the blog as a scratch pad for now.

Labels: , , ,

By al - 11:32 AM |

    follow me on Twitter

    al's del.icio.us Links

    • www.flickr.com
      This is a Flickr badge showing public photos from dragonofsea. Make you own badge here.
    •  
    • (al)



    • Powered by Blogger