简体   繁体   English

运行命令的Bash脚本

[英]Bash Script for running commands

I am trying to write a Bash script for running simulations and saving the output file in different directories. 我正在尝试编写一个Bash脚本来运行模拟并将输出文件保存在不同的目录中。 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. 我是bash脚本的新手。 Can someone point out what the problem could be? 有人可以指出是什么问题吗? Thanks. 谢谢。

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. 这是因为您在${benchmark[1]}中的重定向被视为命令参数,就像被引用一样,而不是真正的重定向。 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). 您的第二个程序不会终止,因为它会永远等待从stdin中读取某些内容,由于重定向不起作用,您必须键入一些内容(键入Ctrl-D关闭stdin,脚本将继续)。

PS: remember that eval is evil and should be avoided. PS:请记住, 评估是邪恶的 ,应该避免。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM