Makefile: add pickrand
[cmccabe-bin] / dripfeed.sh
1 #!/bin/bash
2
3 #
4 # dripfeed.sh
5 #
6 # A script to perform several actions at a time from a file.
7 #
8 # Colin McCabe
9 #
10
11 die() {
12     echo $1
13     exit 1
14 }
15
16 usage() {
17     cat <<EOF
18 dripfeed.sh: read actions from a file and do them.
19 -f [file]: set input file
20 -h: this help
21 -p [number of processes]: set number of processes to use
22 EOF
23 }
24
25 dripfeed() {
26     while true; do
27         for (( nline=1; nline <= ${nlines}; nline++ )) do 
28             nline_text=$(printf '%03d' ${nline})
29             [ $? -ne 0 ] && die "printf failed for ${nline}"
30             mkdir ${nline_text} &>/dev/null && break
31         done
32         echo "nline = $nline, nlines = $nlines"
33         [ $nline -gt $nlines ] && exit 0
34         line=$(head -n ${nline} ${input_file} | tail -n 1)
35         echo $line
36         $line
37     done
38 }
39
40 input_file=""
41 num_procs=1
42 while getopts  "f:hp:" flag; do
43     case $flag in
44     f)  input_file=$OPTARG;;
45     h)  usage; exit 0;;
46     p) num_procs=$OPTARG;;
47     *)  echo "getopts error"
48         echo
49         usage
50         exit 1;;
51     esac
52     #echo "$flag" $OPTIND $OPTARG
53 done
54 [ x${input_file} == x ] && die "you must supply an input file with -f"
55 nlines=$(wc -l "${input_file}" | awk '{ print $1 }')
56 [ $? -ne 0 ] && die "wc -l ${input_file} failed"
57
58 for (( idx=0; idx < ${num_procs}; idx++ )) do 
59     { dripfeed; } &
60 done
61 wait
62
63 exit 0