2010-03-01 15 views
83

मैं एक आदेश के रूप में चलाने के लिए एक बैश स्क्रिप्ट कि एक स्ट्रिंग बनाता है के भीतर एक आदेश के रूप में एक स्ट्रिंग भागोएक बैश स्क्रिप्ट

स्क्रिप्ट:

#! /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' पेस्ट यह ठीक काम करता है।

+0

क्योंकि http://mywiki.wooledge.org/BashFAQ/050 – tripleee

+0

ध्यान दें कि कुछ मामलों में, आपको यह करना होगा: 'echo | जो कुछ भी 'कॉमैंड्स' की बजाय 'व्हाट्सैंड्स' (उदाहरण के लिए, मुझे इसे ऐसा करना था: 'पूंछ - \ 'गूंज | जो भी कमांड \' ') – Andrew

उत्तर

162

आप eval उपयोग कर सकते हैं एक स्ट्रिंग निष्पादित करने के लिए:

eval $illcommando 
+1

क्या eval कमांड वापसी मान (** वापसी मूल्य **, स्ट्रिंग आउटपुट नहीं) आगे भेजता है? –

+2

हां, eval का रिटर्न कोड –

+3

खोल का एक जादू है ... हमेशा पालन करने के लिए कोई कानून नहीं है। – suiwenfeng

15

मैं आमतौर पर कोष्ठक $(commandStr) में आदेश जगह, कि अगर मदद नहीं करता है मुझे लगता है बैश डिबग मोड महान,, के रूप में bash -x script

+3

मुझे यकीन नहीं है कि कमांड एसआरटी क्या संदर्भित करता है, लेकिन कम से कम यह नहीं किया गया' मेरे लिए काम नहीं करते। यदि आप पूर्ण कार्य उदाहरणों का उपयोग करते हैं तो बेहतर होगा। –

+0

@RobinManoli के लिए स्पष्टता में सुधार हुआ। – Andrew

3

स्क्रिप्ट चलाने के चर में अपने आदेश डाल नहीं बस इसे

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

बस यही किया। लेकिन जहां मेरे पास चर थे जो एक ही तर्क के रूप में जाना चाहिए, मैंने '" $ {arg} "' 'किया था। उदाहरण: सर्वर :: team_l_start = "$ {teamAComm}" –

0

./me raise_dead डाले()

मैं कुछ इस तरह के लिए देख रहा था, लेकिन मैं भी एक ही स्ट्रिंग शून्य से दो पैरामीटर तो मैं की तरह कुछ के साथ समाप्त हो गया पुन: उपयोग करने की जरूरत:

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

यह कुछ मैं गर्मी ढेर निर्माण ओपनस्टैक नजर रखने के लिए इस्तेमाल करते हैं । इस मामले में मैं दो की स्थिति, एक कार्रवाई 'बनाएं' और एक स्थिति 'पूरा' नाम "Somestack" एक ढेर पर उम्मीद

वे चर मैं की तरह कुछ कर सकते हैं पाने के लिए:

ACTION=$(my_exe action Somestack) 
STATUS=$(my_exe status Somestack) 
if [[ "$ACTION" == "CREATE" ]] && [[ "$STATUS" == "COMPLETE" ]] 
... 
संबंधित मुद्दे