#!/bin/bash proc printHelp { echo "Usage: $0 -p 3 -i inputDirectory/ -x \"command -to run/\"" echo -e "\t-p The maximum number of processes to start concurrently" echo -e "\t-i The directory containing the files to run the command on" echo -e "\t-x The command to run that the file will be appended to" exit 1 } while getopts "p:x:i:" opt { match $opt { with p setglobal procs = $OPTARG with x setglobal command = $OPTARG with i setglobal inputdir = $OPTARG with ? printHelp } } if [[ -z $procs ]] || [[ -z $command ]] || [[ -z $inputdir ]] { echo "Invalid arguments" printHelp } setglobal total = $[ls $inputdir | wc -l] setglobal files = $[ls -Sr $inputdir] for k in [$[seq 1 $procs $total]] { for i in [$[seq 0 $procs]] { if test $shExpr('i+k') -gt $total { wait exit 0 } setglobal file = $[echo $files | sed $[expr $i + $k]"q;d] echo "Running $command $inputdir/$file" $command "$inputdir/$file"& } wait }