titan titan - 4 days ago 5
Bash Question

Bash Script for running commands

I am trying to write a Bash script for running simulations and saving the output file in different directories. The code I have so far is:

mainDirCfg="/home/software/simplesim/simplesim-3.0/sim-outorder -config ../$1"
outFile="-redir:sim"
declare -a benchmark=("bzip2_base.i386-m32-gcc42-nn dryer.jpg" "equake_base.pisa_little <inp.in> inp.out")
declare -a directory=("bzip2" "equake")
i=0
for d in "${directory[@]}"
do
cd $d
cmdRun="$mainDirCfg $outFile $2 ${benchmark[$i]}"
# above is the command to be run
$cmdRun
cd ..
((i++))
done


The above script runs properly for the first iteration for not for the second one. However, on running the commands individually at the command prompt, I get the expected output. The command that I run for the second iteration is as follows:

/home/software/simplesim/simplesim-3.0/sim-outorder -config ../tmp.cfg -redir:sim tmp9.out equake_base.pisa_little <inp.in> inp.out


I am new to bash scripting. Can someone point out what the problem could be? Thanks.

Answer

Change

cmdRun="$mainDirCfg $outFile $2 ${benchmark[$i]}"
$cmdRun

To

eval "$mainDirCfg $outFile $2 ${benchmark[$i]}"

This is because your redirections in ${benchmark[1]} are seen as command arguments, as if they where quoted, not as true redirections. Your second program does not terminate because it waits forever for something to read from stdin, something you have to type since the redirection does not work (type Ctrl-D to close stdin and your script will continue).

Comments