Variable notation when running python commands with arguments in a bash script

Robert Jones

I have a bash script which is running a bunch of python script all with arguments. In order to have a clean code, I wanted to use variables along the scripts

#!/bin/bash
START=0
SCRIPT_PATH="/opt/scripts/"
IP="192.168.1.111"

if [ "$START" = "0" ]; then
    printf "%s: Starting\n" "$DATE_TIME"

    PORT=1234
    TEST_FILE="$SCRIPT_PATH/Test Scripts/test.TXT"
    SCRIPT="$SCRIPT_PATH/script1.py"
    ARGS="-P $SCRIPT_PATH/script2.py -n 15 -p $PORT -i $IP"

    python "$SCRIPT" ${ARGS} -f "${TEST_FILE}" > ./out.log 2>&1 &

fi

This code is actually working but few things I don't understand :

  1. Why, if I add quotes around ${ARGS}, the arguments are not parsed correctly by python ? What would be the best way to write this ?
  2. What is the best method to add -f "${TEST_FILE}" to the ARGS variable without python blocking on the whitespace and throwing the error: "$SCRIPT_PATH/Test " not found
Marc Sances

When you wrap quotes around an argument list, the argument vector receives a single argument with everything that is wrapped in quotes and so, the argument parser fails to do its job properly and you have your issue.

Regarding your second question, it is not easy to embed the quotes into the array, because the quotes will be parsed before being stored in the array, and then when you perform the array expansion to run the command, they will be missing and fail. I have tried this several times with no success.

An alternative approach would mean that you modify a little your script to use a custom internal field separator (IFS) to manually tell what should be considered an argument and what not:

#!/bin/bash
START=0
SCRIPT_PATH="/opt/scripts/"
IP="192.168.1.111"

if [ "$START" = "0" ]; then
    printf "%s: Starting\n" "$DATE_TIME"

    PORT=1234
    TEST_FILE="$SCRIPT_PATH/Test Scripts/test.TXT"
    SCRIPT="$SCRIPT_PATH/script1.py"
    OLD_IFS=$IFS
    IFS=';'
    ARGS="$SCRIPT;-P;$SCRIPT_PATH/script2.py;-n;15;-p;$PORT;-i;$IP;-f;$TEST_FILE"
    python ${ARGS} > ./out.log 2>&1 &
    IFS=$OLD_IFS
fi

As you can see, I replace the spaces in ARGS with semicolons. This way, TEST_FILE variable contents will be considered as a single argument for bash and will be properly populated in argument vector. I'm also moving the script to the argument vector for simplicity, otherwise, Python will not get the proper script path and fail, due to this modification we did to IFS.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Block all default commands while a bash script are running?

分類Dev

How to change bash script output when the script is running?

分類Dev

Running VB Script file with arguments

分類Dev

Commands Working In Terminal, But Not In Bash Script

分類Dev

Multiple Fortran arguments in bash script

分類Dev

Bash Script - check arguments - files in for

分類Dev

bash arguments in a variable and then loop through

分類Dev

Send data and user commands to running Puppeteer script

分類Dev

Running bash script in a kubernetes pod

分類Dev

Shell script wrapper to prevent running command with no arguments?

分類Dev

Why combine commands on a single line in a Bash script?

分類Dev

Combine sed commands into one command in bash script

分類Dev

Custom powershell backgroundcolor reverts when running commands?

分類Dev

Propagate all arguments in a bash shell script

分類Dev

how to loop through arguments in a bash script

分類Dev

Bash script that runs a command with arguments and redirects

分類Dev

Is it possible to pass arguments into a Python script?

分類Dev

Running a command in a new terminal instance in a bash script

分類Dev

Running a shell script with and without "bash" command

分類Dev

b2 command finds ICU when called directly but not from indirect bash script variable

分類Dev

How run python script with arguments via Python?

分類Dev

Replicate shell environment in python for executing bash commands

分類Dev

How to show result of python script in a console when running from ST editor?

分類Dev

How to run PowerShell core commands in bash script directly?

分類Dev

Why does this shell script fail in bash, but the commands work in SSH?

分類Dev

How to parse commands from a text file to a bash script in the CLI

分類Dev

Multiple find -exec commands in one bash script doesn't work?

分類Dev

How to execute shell commands in a loop from within a bash script?

分類Dev

Bash script does not execute commands until after delay

Related 関連記事

  1. 1

    Block all default commands while a bash script are running?

  2. 2

    How to change bash script output when the script is running?

  3. 3

    Running VB Script file with arguments

  4. 4

    Commands Working In Terminal, But Not In Bash Script

  5. 5

    Multiple Fortran arguments in bash script

  6. 6

    Bash Script - check arguments - files in for

  7. 7

    bash arguments in a variable and then loop through

  8. 8

    Send data and user commands to running Puppeteer script

  9. 9

    Running bash script in a kubernetes pod

  10. 10

    Shell script wrapper to prevent running command with no arguments?

  11. 11

    Why combine commands on a single line in a Bash script?

  12. 12

    Combine sed commands into one command in bash script

  13. 13

    Custom powershell backgroundcolor reverts when running commands?

  14. 14

    Propagate all arguments in a bash shell script

  15. 15

    how to loop through arguments in a bash script

  16. 16

    Bash script that runs a command with arguments and redirects

  17. 17

    Is it possible to pass arguments into a Python script?

  18. 18

    Running a command in a new terminal instance in a bash script

  19. 19

    Running a shell script with and without "bash" command

  20. 20

    b2 command finds ICU when called directly but not from indirect bash script variable

  21. 21

    How run python script with arguments via Python?

  22. 22

    Replicate shell environment in python for executing bash commands

  23. 23

    How to show result of python script in a console when running from ST editor?

  24. 24

    How to run PowerShell core commands in bash script directly?

  25. 25

    Why does this shell script fail in bash, but the commands work in SSH?

  26. 26

    How to parse commands from a text file to a bash script in the CLI

  27. 27

    Multiple find -exec commands in one bash script doesn't work?

  28. 28

    How to execute shell commands in a loop from within a bash script?

  29. 29

    Bash script does not execute commands until after delay

ホットタグ

アーカイブ