首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Bash脚本中将字符串作为命令运行

在Bash脚本中将字符串作为命令运行
EN

Stack Overflow用户
提问于 2010-03-01 18:36:23
回答 8查看 465.9K关注 0票数 182

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

脚本:

代码语言:javascript
复制
#! /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

脚本输出:

代码语言:javascript
复制
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:“之后的输出中),它就能正常工作。

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2010-03-01 18:52:19

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

代码语言:javascript
复制
eval $illcommando
票数 319
EN

Stack Overflow用户

发布于 2018-06-02 08:42:03

代码语言:javascript
复制
your_command_string="..."
output=$(eval "$your_command_string")
echo "$output"
票数 29
EN

Stack Overflow用户

发布于 2010-03-01 18:39:36

我通常将命令放在括号$(commandStr)中,如果这样还不能帮助我发现bash调试模式很棒,那么以bash -x script的身份运行脚本

票数 28
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2355148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档