在Bash脚本中作为命令运行一个string

我有一个Bash脚本构build一个string作为命令运行

脚本:

#! /bin/bash matchdir="/home/joao/robocup/runner_workdir/matches/testmatch/" teamAComm="`pwd`/a.sh" teamBComm="`pwd`/b.sh" include="`pwd`/server_official.conf" serverbin='/usr/local/bin/rcssserver' cd $matchdir illcommando="$serverbin include='$include' server::team_l_start = '${teamAComm}' server::team_r_start = '${teamBComm}' CSVSaver::save='true' CSVSaver::filename = 'out.csv'" echo "running: $illcommando" # $illcommando > server-output.log 2> server-error.log $illcommando 

这似乎并没有正确地向$serverbin提供参数。

脚本输出:

 running: /usr/local/bin/rcssserver include='/home/joao/robocup/runner_workdir/server_official.conf' server::team_l_start = '/home/joao/robocup/runner_workdir/a.sh' server::team_r_start = '/home/joao/robocup/runner_workdir/b.sh' CSVSaver::save='true' CSVSaver::filename = 'out.csv' rcssserver-14.0.1 Copyright (C) 1995, 1996, 1997, 1998, 1999 Electrotechnical Laboratory. 2000 - 2009 RoboCup Soccer Simulator Maintenance Group. Usage: /usr/local/bin/rcssserver [[-[-]]namespace::option=value] [[-[-]][namespace::]help] [[-[-]]include=file] Options: help display generic help include=file parse the specified configuration file. Configuration files have the same format as the command line options. The configuration file specified will be parsed before all subsequent options. server::help display detailed help for the "server" module player::help display detailed help for the "player" module CSVSaver::help display detailed help for the "CSVSaver" module CSVSaver Options: CSVSaver::save=<on|off|true|false|1|0|> If save is on/true, then the saver will attempt to save the results to the database. Otherwise it will do nothing. current value: false CSVSaver::filename='<STRING>' The file to save the results to. If this file does not exist it will be created. If the file does exist, the results will be appended to the end. current value: 'out.csv' 

如果我只是粘贴命令/usr/local/bin/rcssserver include='/home/joao/robocup/runner_workdir/server_official.conf' server::team_l_start = '/home/joao/robocup/runner_workdir/a.sh' server::team_r_start = '/home/joao/robocup/runner_workdir/b.sh' CSVSaver::save='true' CSVSaver::filename = 'out.csv' (在“runnning:”之后的输出中)它工作正常。

你可以使用eval来执行一个string:

 eval $illcommando 

我通常将命令放在圆括号$(commandStr) ,如果这样做不能帮助我findbashdebugging模式,那么运行脚本作为bash -x script

不要把你的命令放在variables中,只要运行它

 matchdir="/home/joao/robocup/runner_workdir/matches/testmatch/" PWD=$(pwd) teamAComm="$PWD/a.sh" teamBComm="$PWD/b.sh" include="$PWD/server_official.conf" serverbin='/usr/local/bin/rcssserver' cd $matchdir $serverbin include=$include server::team_l_start = ${teamAComm} server::team_r_start=${teamBComm} CSVSaver::save='true' CSVSaver::filename = 'out.csv' 

./me casts raise_dead()

我正在寻找这样的东西,但我也需要重复使用相同的string减去两个参数,所以我结束了这样的事情:

 my_exe () { mysql -sN -e "select $1 from heat.stack where heat.stack.name=\"$2\";" } 

这是我用来监视OpenStack热堆创build的东西。 在这种情况下,我期待两个条件,一个名为“Somestack”的栈上的一个“CREATE”和一个状态“COMPLETE”

为了得到这些variables,我可以做一些事情:

 ACTION=$(my_exe action Somestack) STATUS=$(my_exe status Somestack) if [[ "$ACTION" == "CREATE" ]] && [[ "$STATUS" == "COMPLETE" ]] ...