Search code examples
pythonshellsubprocesspopen

Python: executing shell script with arguments(variable), but argument is not read in shell script


I am trying to execute a shell script(not command) from python:

main.py
-------
from subprocess import Popen

Process=Popen(['./childdir/execute.sh',str(var1),str(var2)],shell=True)

execute.sh
----------

echo $1 //does not print anything
echo $2 //does not print anything

var1 and var2 are some string that I am using as an input to shell script. Am I missing something or is there another way to do it?

Referred: How to use subprocess popen Python


Solution

  • The problem is with shell=True. Either remove that argument, or pass all arguments as a string, as follows:

    Process=Popen('./childdir/execute.sh %s %s' % (str(var1),str(var2),), shell=True)
    

    The shell will only pass the arguments you provide in the 1st argument of Popen to the process, as it does the interpretation of arguments itself. See a similar question answered here. What actually happens is your shell script gets no arguments, so $1 and $2 are empty.

    Popen will inherit stdout and stderr from the python script, so usually there's no need to provide the stdin= and stderr= arguments to Popen (unless you run the script with output redirection, such as >). You should do this only if you need to read the output inside the python script, and manipulate it somehow.

    If all you need is to get the output (and don't mind running synchronously), I'd recommend trying check_output, as it is easier to get output than Popen:

    output = subprocess.check_output(['./childdir/execute.sh',str(var1),str(var2)])
    print(output)
    

    Notice that check_output and check_call have the same rules for the shell= argument as Popen.