0

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.

titan
  • 122
  • 10
  • Where / how are you incrementing the counter (i)? – Steve Harrington Dec 02 '16 at 00:20
  • 2
    The first problem is you fail to explain how you program fails, the second one is you changed the posted script without notice. – jlliagre Dec 02 '16 at 00:31
  • I am sorry about that. The program fails for the second iteration in the sense that it doesn't terminate. `$cmdRun` is the line where it gets stuck. – titan Dec 02 '16 at 00:35
  • How does it not work? If you don't think the simulator invocation is correct, try changing `$cmdRun` to something like `echo "$cmdRun"` to see what the invocation command actually is. If your simulator doesn't terminate, it might an issue with your simulator or how it is being called. – e0k Dec 02 '16 at 00:36
  • Your edit where you added `cd ..` is fragile; if your directory list becomes something like `("tests/bzip2" "tests/equake")` then `..` is wrong. Since you say this is a bash script you should (1) begin it with `#!/bin/bash`, and (2) then use `pushd` and `popd` which are built-ins in bash (so guaranteed to be there) – Stephen P Dec 02 '16 at 00:37
  • Read [BashFAQ #50: I'm trying to put a command in a variable, but the complex cases always fail!](http://mywiki.wooledge.org/BashFAQ/050). The first rule is: don't put commands in variables; variables are for holding data, not executable code. – Gordon Davisson Dec 02 '16 at 00:54

1 Answers1

2

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 were 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).

PS: remember that eval is evil and should be avoided.

Community
  • 1
  • 1
xhienne
  • 5,738
  • 1
  • 15
  • 34
  • 1
    @titan Please avoid using `eval` if at all possible; it's a huge bug magnet if you don't fully understand bash syntax (and from the question, I guarantee you don't understand bash syntax). With `eval`, it's much to easy to make something that works at first, but then fails bizarrely under slightly different circumstances. – Gordon Davisson Dec 02 '16 at 00:52
  • Right, I have added the obligatory warning in PS – xhienne Dec 02 '16 at 01:05