#!/bin/bash # # parallel-script-demo.sh v0.1.1 # Copyright 2010, Nikita Melnichenko [http://nikita.melnichenko.name] # License: GPL-2 (http://opensource.org/licenses/gpl-license.php) # # Parallel processing script demo. # process a single item function process_item { local n let n="$RANDOM%5+1" echo "> $1 ($n sec)" sleep $n echo "< $1" } # generate and process the list of items function process_list { dir="$2" if [ -z "$dir" ]; then dir=/; fi find "$dir" -mindepth 1 -maxdepth 1 -print0 | sort -z | xargs -0 -l1 -P"$1" "$0" 0 } # handle the first argument of the script function process { default_np=4 np="$1"; shift if [ -z "$np" ]; then np=$default_np; fi if [ "$np" == "0" ]; then process_item "$@"; else process_list "$np" "$@"; fi } process "$@"