-1

Currently trying to pass a parameter from my python script to a bash script I created. How can I can get user input from my python script to my bash script?

This is the code for my python script 'passingParameters.py' which I used to try and send a variable (loop) to my bash script. I have tested this python script (after I adjusted the code) by sending the output to another python script which I used to read the input.

loop = str(sys.argv[1])
subprocess.check_call( ["./test.sh", loop], shell=True)

This is the code for my bash script 'test.sh'. I have tested this script by itself to confirm that it does receive user input when I just call the bash script from the command line.

echo "This number was sent from the passParameters.py script: " $1
Mark O'Sullivan
  • 10,138
  • 6
  • 39
  • 60
  • 1
    Pretty related: [How do I pass a Python Variable to Bash?](http://stackoverflow.com/questions/2796932/how-do-i-pass-a-python-variable-to-bash) – fedorqui Aug 05 '14 at 13:44
  • 1
    The code should work. Do you get an error? Or why do you think the parameter isn't passed? – Aaron Digulla Aug 05 '14 at 13:57
  • @AaronDigulla no error, just the value I input into the command line doesn't show up – Mark O'Sullivan Aug 05 '14 at 14:27
  • @MOS182 See my comment to [crono's answer](http://stackoverflow.com/a/25140918/1126841). – chepner Aug 05 '14 at 14:32
  • Possible duplicate of [How to pass variables from python script to bash script](https://stackoverflow.com/q/4257098/608639) – jww Sep 23 '18 at 00:34

1 Answers1

4

If you use shell=True then the executable is /bin/sh, which then calls test.sh but never sees the loop variable. You can either set shell=False and add the #!/bin/sh to the shell script,

#! /bin/sh
echo "This number was sent from the passParameters.py script: " $1

and

subprocess.check_call( ["./test.sh", loop], shell=False)

or pass the variable as a string:

subprocess.check_call( ["./test.sh %s" % loop], shell=True)

shell=True is not recommended anyway.

cronos
  • 2,268
  • 16
  • 17
  • +1 It's not well documented, but combining your list of arguments with `shell=True` will result in the following command: `sh -c ./test.sh `. This will still run `./test.sh`, but the argument `loop` is passed to `sh` as `$0` rather than to `./test.sh` as `$1`. – chepner Aug 05 '14 at 14:31
  • Thanks for your help! This first suggestion works fine. I tried the second suggestion with shell=True and it didn't work, says invalid syntax for the % beside loop. – Mark O'Sullivan Aug 05 '14 at 14:35
  • @chepner that's what I thought might have explained why it wasn't displayed so instead of having $1 in the bash script, I tried $0 and it still didn't print out anything. – Mark O'Sullivan Aug 05 '14 at 14:38
  • When you pass the variable as a string, the list is just superfluous. `subprocess.check_call("./test.sh %s" % loop, shell=True)`. But as you note, you really want to avoid `shell=True` if you can. See also [Actual meaning of `shell=True` in `subprocess`](https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess) – tripleee Aug 07 '21 at 07:42