2010-03-01 9 views
83

Ho uno script Bash che costruisce una stringa per l'esecuzione come un comandoEseguire una stringa come un comando all'interno di uno script 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 

che non sembra fornire gli argomenti correttamente allo $serverbin.

uscita Script:

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' 

se ho appena incollare il 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' (in uscita dopo "Runnning:") funziona benissimo.

+0

Perché http://mywiki.wooledge.org/BashFAQ/050 – tripleee

+0

Si noti che in alcuni casi è necessario: 'echo | WhateverCommands' invece di solo 'WhateverCommands' (ad esempio, dovevo farlo in questo modo:' | tail - \ 'echo | whateverCommands \ '') – Andrew

risposta

162

È possibile utilizzare eval per eseguire una stringa:

eval $illcommando 
+1

Eval inoltra il comando valore di ritorno (** valore di ritorno **, non stringa di output)? –

+2

Sì, il codice di ritorno di eval è quello del comando –

+3

shell è una magia ... sempre nessuna legge da seguire. – suiwenfeng

15

Io di solito posizionare i comandi tra parentesi $(commandStr), se questo non aiuta trovo modalità di debug bash grande, eseguire lo script come bash -x script

+3

Non sono sicuro di cosa si riferisca commandStr, ma almeno questo non ha funzionato lavoro per me. È meglio se usi esempi di lavoro completi. –

+0

@RobinManoli Migliorata la chiarezza per ya. – Andrew

3

non mettere i comandi in variabili, basta eseguirlo

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

ha fatto proprio questo. ma dove ho avuto variabili che dovrebbero andare come un singolo argomento ho fatto "$ {arg}" '. esempio: server :: team_l_start = "$ {teamAComm}" –

0

./me getta raise_dead()

ero alla ricerca di qualcosa di simile, ma ho anche bisogno di riutilizzare la stessa stringa meno due parametri così ho finito con qualcosa di simile:

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

questa è una cosa che uso per monitorare OpenStack creazione di stack di calore . In questo caso mi aspetto due condizioni, un'azione 'CREATE' e uno status 'completo' su una pila di nome "Somestack"

Per ottenere tali variabili che posso fare qualcosa di simile:

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