Run a string as a command within a Bash script

前端 未结 8 904
清歌不尽
清歌不尽 2020-12-07 09:08

I have a Bash script that builds a string to run as a command

Script:

#! /bin/bash

matchdir=\"/home/joao/robocup/runner_workdir/mat         


        
相关标签:
8条回答
  • 2020-12-07 09:40

    To see all commands that are being executed by the script, add the -x flag to your shabang line, and execute the command normally:

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

    Then if you sometimes want to ignore the debug output, redirect stderr somewhere.

    0 讨论(0)
  • 2020-12-07 09:43

    ./me casts raise_dead()

    I was looking for something like this, but I also needed to reuse the same string minus two parameters so I ended up with something like:

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

    This is something I use to monitor openstack heat stack creation. In this case I expect two conditions, an action 'CREATE' and a status 'COMPLETE' on a stack named "Somestack"

    To get those variables I can do something like:

    ACTION=$(my_exe action Somestack)
    STATUS=$(my_exe status Somestack)
    if [[ "$ACTION" == "CREATE" ]] && [[ "$STATUS" == "COMPLETE" ]]
    ...
    
    0 讨论(0)
  • 2020-12-07 09:55

    Here is my gradle build script that executes strings stored 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
    

    The lone ")" are kind of ugly. But I have no clue how to fix that asthetic aspect.

    0 讨论(0)
  • 2020-12-07 09:57
    your_command_string="..."
    output=$(eval "$your_command_string")
    echo "$output"
    
    0 讨论(0)
  • 2020-12-07 10:02

    don't put your commands in variables, just run it

    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 讨论(0)
  • 2020-12-07 10:04

    You can use eval to execute a string:

    eval $illcommando
    
    0 讨论(0)
提交回复
热议问题