2010-03-01 5 views
83

Tengo un script bash que genera una cadena para funcionar como un comandoEjecutar una cadena como un comando dentro de un script Bash

Guión:

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

que no parece suministrar los argumentos correctamente al $serverbin.

salida Guión:

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' 

si acabo de pegar el comando /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' (en la salida después de "Runnning:") que trabaja muy bien.

+0

Debido a http://mywiki.wooledge.org/BashFAQ/050 – tripleee

+0

Tenga en cuenta que en algunos casos, debe hacer: 'echo | whateverCommands' en lugar de simplemente 'whateverCommands' (por ejemplo, tuve que hacerlo así:' | tail - \ 'echo | whateverCommands \' ') – Andrew

Respuesta

162

puede utilizar eval para ejecutar una cadena:

eval $illcommando 
+1

¿Eval pasa el valor devuelto del comando (** valor de retorno **, no salida de cadena)? –

+2

Sí, el código de retorno de eval es el del comando –

+3

el shell es una magia ... siempre no hay ley a seguir. – suiwenfeng

15

por lo general me coloque comandos entre paréntesis $(commandStr), si esto no soluciona el encuentro el modo de depuración fiesta grande, ejecutar el script como bash -x script

+3

No estoy seguro de a qué se refiere commandStr, pero al menos esto no lo hizo t trabajo para mi Es mejor si usa ejemplos completos de trabajo. –

+0

@RobinManoli Mejorado la claridad para ti. – Andrew

3

no ponga sus órdenes en las variables, sólo ejecutarlo

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' 
+0

hizo exactamente eso. pero donde tenía variables que deberían ir solo como un solo argumento, hice '" $ {arg} "'. ejemplo: server :: team_l_start = "$ {teamAComm}" –

0

./me arroja raise_dead()

que estaba buscando algo como esto, pero también necesitaba volver a utilizar la misma cadena menos dos parámetros, así que acabé con algo como:

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

Esto es algo que utilizo para controlar openstack creación del bloque de calor . En este caso espero dos condiciones, una acción 'crear' y un estado de 'completo' en una pila llamado "Somestack"

Para obtener aquellas variables que puedo hacer algo como:

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