I am trying to write a script that would run a series of programs many times. Namely, there will be about 4 programs run in series, each taking about 1 hour. I want to carry this out about 40 times with different input files, variable values, etc.
Also, I am running this on an 8-processor computer, so I would like to carry these jobs out in parallel, by running 8 jobs at a time, then the next 8 jobs, then the next 8 jobs, and so on.
If I were running a single command, then I could
Code:
for (( j = 1; j<=40; j+=1))
do
if [ $(( $j % 8 )) -eq 0 ]
then
backgroundchar="&"
else
backgroundchar=""
fi
eval command1 $backgroundchar
done
The reason for including
Code:
eval command1 $backgroundchar
is to make sure that the shell interprets the $backgroundchar as an actual "&".
Otherwise, if it is run as simply "command1 $backgroundchar", an error will be reported before the variable is expanded (I think this is the reason for the error).
But what if I want to run multiple
subshells, using the
( ... ) & construction? What I would like to do is this:
Code:
for (( j = 1; j<=40; j+=1))
do
if [ $(( $j % 8 )) -eq 0 ]
then
backgroundchar="&"
else
backgroundchar=""
fi
(
command1
command2
command3
) $backgroundchar
done
But this gives an error. Using "eval" does not work either:
Code:
$ av="&"
$ eval ( echo "a"; sleep 1; echo "b"; sleep 1; echo "c"; sleep 1; echo "d" ) $av
-bash: syntax error near unexpected token `echo'
$
Is there a way I can begin a subshell, using a variable value to determine whether to run it as
( ... ) as opposed to
( ... ) & ?