Voer een tekenreeks uit als een opdracht binnen een BASH-script

Ik heb een bash-script dat een tekenreeks bouwt om te rennen als een opdracht

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

die de argumenten niet correct lijken te leveren aan de $serverbin.

Script Output:

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'

als ik gewoon het commando /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 de uitvoer na “runnning: “) werkt het prima.


Antwoord 1, autoriteit 100%

Je kunt evalgebruiken om een string uit te voeren:

eval $illcommando

Antwoord 2, autoriteit 9%

Ik plaats meestal opdrachten tussen haakjes $(commandStr), als dat niet helpt, vind ik de bash-foutopsporingsmodus geweldig, voer het script uit als bash -x script


Antwoord 3, autoriteit 9%

your_command_string="..."
output=$(eval "$your_command_string")
echo "$output"

Antwoord 4, autoriteit 4%

zet je commando’s niet in variabelen, voer het gewoon uit

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'

Antwoord 5

./ ME casts heft_dead ()

Ik was op zoek naar zoiets, maar ik moest ook dezelfde reeks hergebruiken minus twee parameters, dus ik eindigde met zoiets:

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

Dit is iets dat ik gebruik om de Creatie van OpenSpack Heat Stack-creatie te controleren. In dit geval verwacht ik twee voorwaarden, een actie ‘Create’ en een status ‘compleet’ op een stapel met de naam “SOMSTACK”

Om die variabelen te krijgen, kan ik iets doen als:

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

Antwoord 6

Hier is mijn gradle-build-script dat snaren wordt opgeslagen die zijn opgeslagen in Heredocs :

current_directory=$( realpath "." )
GENERATED=${current_directory}/"GENERATED"
build_gradle=$( realpath build.gradle )
## touch because .gitignore ignores this folder:
touch $GENERATED
COPY_BUILD_FILE=$( cat <<COPY_BUILD_FILE_HEREDOC
    cp 
        $build_gradle 
        $GENERATED/build.gradle
COPY_BUILD_FILE_HEREDOC
)
$COPY_BUILD_FILE
GRADLE_COMMAND=$( cat <<GRADLE_COMMAND_HEREDOC
    gradle run
        --build-file       
            $GENERATED/build.gradle
        --gradle-user-home 
            $GENERATED  
        --no-daemon
GRADLE_COMMAND_HEREDOC
)
$GRADLE_COMMAND

De eenzame “)” zijn nogal lelijk. Maar ik heb geen idee hoe ik dat esthetische aspect moet oplossen.


Antwoord 7

Als u alle opdrachten wilt zien die door het script worden uitgevoerd, voegt u de vlag -xtoe aan uw shabang-regel en voert u de opdracht normaal uit:

#! /bin/bash -x
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
$serverbin include="$include" server::team_l_start="${teamAComm}" server::team_r_start="${teamBComm}" CSVSaver::save='true' CSVSaver::filename='out.csv'

Als u de foutopsporingsuitvoer soms wilt negeren, moet u stderrergens anders heen leiden.


Antwoord 8

Voor mij echo XYZ_20200824.zip | grep -Eo '[[:digit:]]{4}[[:digit:]]{2}[[:digit:]]{2}'
werkte prima, maar kon de uitvoer van de opdracht niet opslaan in een variabele.
Ik had hetzelfde probleem. Ik probeerde evalmaar kreeg geen uitvoer.

Hier is het antwoord op mijn probleem:
cmd=$(echo XYZ_20200824.zip | grep -Eo '[[:digit:]]{4}[[:digit:]]{2}[[:digit:]]{2}')

echo $cmd

Mijn uitvoer is nu 20200824

Other episodes