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.

6
  • Where / how are you incrementing the counter (i)? Commented Dec 2, 2016 at 0: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. Commented Dec 2, 2016 at 0: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. Commented Dec 2, 2016 at 0: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. Commented Dec 2, 2016 at 0: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) Commented Dec 2, 2016 at 0:37

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@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.
Right, I have added the obligatory warning in PS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.