在Bash脚本中作为命令运行一个字符串。

时间:2021-11-13 22:12:07

I have a Bash script that builds a string to run as a command

我有一个Bash脚本,它构建一个作为命令运行的字符串。

Script:

脚本:

#! /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

which does not seem to supply the arguments correctly to the $serverbin.

这似乎不能正确地将参数提供给$serverbin。

Script output:

脚本输出:

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'

if I just paste the command /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' (in the output after "runnning: ") it works fine.

如果我只是粘贴命令/usr/local/bin/rcssserver包含='/home/joao/robocup/runner_workdir/server_official。配置的服务器::team_l_start = / home /若昂/ / runner_workdir举办的一。sh的服务器::team_r_start = / home /若昂/杯赛/ runner_workdir / b。::保存='true' CSVSaver::filename =' out。csv(在“runnning:”之后的输出中)工作正常。

4 个解决方案

#1


167  

You can use eval to execute a string:

可以使用eval来执行字符串:

eval $illcommando

#2


16  

I usually place commands in parentheses $(commandStr), if that doesn't help I find bash debug mode great, run the script as bash -x script

我通常在括号$(commandStr)中放置命令,如果这不能帮助我找到bash调试模式,那么运行脚本作为bash -x脚本。

#3


4  

don't put your commands in variables, just run it

不要把命令放在变量中,只需要运行它。

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'

#4


0  

./me casts raise_dead()

。/我投raise_dead()

I was looking for something like this, but I also needed to reuse the same string minus two parameters so I ended up with something like:

我在找这样的东西,但我也需要重复使用相同的字符串减去两个参数,所以最后我得到了这样的东西:

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

This is something I use to monitor openstack heat stack creation. In this case I expect two conditions, an action 'CREATE' and a status 'COMPLETE' on a stack named "Somestack"

这是我用来监视openstack热堆栈创建的东西。在这种情况下,我希望有两个条件,一个动作“创建”,一个状态“完成”在一个名为“Somestack”的堆栈上

To get those variables I can do something like:

为了得到这些变量,我可以这样做:

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

#1


167  

You can use eval to execute a string:

可以使用eval来执行字符串:

eval $illcommando

#2


16  

I usually place commands in parentheses $(commandStr), if that doesn't help I find bash debug mode great, run the script as bash -x script

我通常在括号$(commandStr)中放置命令,如果这不能帮助我找到bash调试模式,那么运行脚本作为bash -x脚本。

#3


4  

don't put your commands in variables, just run it

不要把命令放在变量中,只需要运行它。

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'

#4


0  

./me casts raise_dead()

。/我投raise_dead()

I was looking for something like this, but I also needed to reuse the same string minus two parameters so I ended up with something like:

我在找这样的东西,但我也需要重复使用相同的字符串减去两个参数,所以最后我得到了这样的东西:

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

This is something I use to monitor openstack heat stack creation. In this case I expect two conditions, an action 'CREATE' and a status 'COMPLETE' on a stack named "Somestack"

这是我用来监视openstack热堆栈创建的东西。在这种情况下,我希望有两个条件,一个动作“创建”,一个状态“完成”在一个名为“Somestack”的堆栈上

To get those variables I can do something like:

为了得到这些变量,我可以这样做:

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