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

Joã*_*ela 133 bash shell command-line-arguments

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

脚本:

#! /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
Run Code Online (Sandbox Code Playgroud)

这似乎没有正确地提供参数$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'
Run Code Online (Sandbox Code Playgroud)

如果我只是粘贴命令/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:"之后的输出中)它工作正常.

Arn*_*ter 238

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

eval $illcommando
Run Code Online (Sandbox Code Playgroud)

  • `eval` 在所有编程语言中都是一个邪恶的命令,因此请谨慎使用。 (4认同)
  • `eval "$illcommando"`,**带引号**,以免命令在运行之前被破坏。尝试使用带引号和不带引号的值 `illcommando='printf "%s\n" " * "'` 进行 `eval` 来查看差异。 (4认同)
  • eval 是否传递命令返回值(**返回值**,而不是字符串输出)? (2认同)
  • 是的,eval的返回码是命令的返回码 (2认同)

小智 27

我通常将命令放在括号中$(commandStr),如果这对我发现bash调试模式没有帮助,那么运行脚本为bash -x script

  • 我不确定commandStr指的是什么,但至少这对我不起作用.如果你使用完整的工作示例,那就更好了. (4认同)

Tak*_*man 9

your_command_string="..."
output=$(eval "$your_command_string")
echo "$output"
Run Code Online (Sandbox Code Playgroud)


gho*_*g74 8

不要将命令放在变量中,只需运行它即可

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'
Run Code Online (Sandbox Code Playgroud)