J’ai un script Bash qui construit une chaîne à exécuter en tant que commande
Scénario:
#! /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
qui ne semble pas fournir correctement les arguments à la $serverbin
.
Sortie de 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= 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='' 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 je viens de coller la commande /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'
(dans la sortie après “runnning:”) cela fonctionne correctement.
Vous pouvez utiliser eval
pour exécuter une chaîne:
eval $illcommando
Je mets généralement les commandes entre parenthèses $(commandStr)
, si cela ne $(commandStr)
pas, je trouve le mode bash debug génial, lancez le script en tant que script bash -x script
ne mettez pas vos commandes dans des variables, lancez-le simplement
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'
your_command_ssortingng="..." output=$(eval "$your_command_ssortingng") echo "$output"
./me projette raise_dead ()
Je cherchais quelque chose comme ça, mais j’avais aussi besoin de réutiliser la même chaîne moins deux parameters alors j’ai fini avec quelque chose comme:
my_exe () { mysql -sN -e "select $1 from heat.stack where heat.stack.name=\"$2\";" }
C’est quelque chose que j’utilise pour surveiller la création de stack de chaleur openstack. Dans ce cas, j’attends deux conditions, une action ‘CREATE’ et un statut ‘COMPLETE’ sur une stack nommée “Somestack”
Pour obtenir ces variables, je peux faire quelque chose comme:
ACTION=$(my_exe action Somestack) STATUS=$(my_exe status Somestack) if [[ "$ACTION" == "CREATE" ]] && [[ "$STATUS" == "COMPLETE" ]] ...