代码之家  ›  专栏  ›  技术社区  ›  João Portela

在bash脚本中以命令的形式运行字符串

  •  115
  • João Portela  · 技术社区  · 15 年前

    我有一个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
    

    这似乎不能正确地为 $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:”之后的输出中)它工作正常。

    5 回复  |  直到 7 年前
        1
  •  210
  •   Arne Burmeister    15 年前

    你可以使用 eval 要执行字符串:

    eval $illcommando
    
        2
  •  23
  •   Ola    15 年前

    我通常把命令放在括号里 $(commandStr) ,如果这不帮助我发现bash调试模式很好,请运行脚本 bash -x script

        3
  •  7
  •   ghostdog74    15 年前

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

    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
  •  5
  •   Takman    7 年前
    your_command_string="..."
    output=$(eval "$your_command_string")
    echo "$output"
    
        5
  •  1
  •   cmyster    9 年前

    /me施法提高死亡人数

    我在寻找类似这样的东西,但我也需要重用相同的字符串减去两个参数,所以最终得到如下的结果:

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

    这是我用来监视OpenStack热堆栈创建的工具。在这种情况下,我期望两个条件,一个操作“create”和一个名为“somestack”的堆栈上的状态“complete”

    为了得到这些变量,我可以做如下的事情:

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