Python: subprocess and running a bash script with multiple arguments
Asked Answered
P

7

44

How do I go about running a bash script using the subprocess module, to which I must give several arguments?

This is what I'm currently using:

subprocess.Popen(['/my/file/path/programname.sh', 'arg1 arg2 %s' % arg3], \
    shell = True)

The bash script seems not to be taking any of the parameters in. Any insights are greatly appreciated!

Poulson answered 21/6, 2013 at 19:23 Comment(0)
C
53

Pass arguments as a list, see the very first code example in the docs:

import subprocess

subprocess.check_call(['/my/file/path/programname.sh', 'arg1', 'arg2', arg3])

If arg3 is not a string; convert it to string before passing to check_call(): arg3 = str(arg3).

Chronology answered 21/6, 2013 at 19:57 Comment(4)
If all of my args are variables, do I not set them as strings?Poulson
@user2510173: I don't understand the question. Here's an example: arg3 = 1, to convert it, you could call str(arg3) that returns '1'.Chronology
Unfortunately, the example in the code does not answer the original question, since they chose a too-simple case of just a single argument. It is unclear whether multiple arguments should be passed as a single string, or as multiple ones, unless you read well down the page to the details behind POpen. Even then, it only says, "Providing a sequence of arguments is generally preferred" - not, "you must supply multiple arguments as a list"Kauffman
@SteveBroberg: do you mean the example in the official Python documentation? You can report it as a documentation bug at bugs.python.org (patches are welcome but a simple report that points out a possible issue could be also useful).Chronology
N
16
subprocess.Popen(['/my/file/path/programname.sh arg1 arg2 %s' % arg3], shell = True).

If you use shell = True the script and its arguments have to be passed as a string. Any other elements in the args sequence will be treated as arguments to the shell.

You can find the complete docs at http://docs.python.org/2/library/subprocess.html#subprocess.Popen.

Ningpo answered 21/6, 2013 at 19:48 Comment(0)
D
3

One more example, which is not included in all the above ones,

subprocess.Popen(['/your/script.sh %s %s %s' %(argument1,argument2,argument3)], shell = True)

Please note that, when you type %(argument1,argument2,argument3), there should not be any space between % and (, eg % (argument1,argument2,argument3) is not valid.

Dupaix answered 11/10, 2017 at 10:39 Comment(0)
G
2

Hi I know that this is solution is quite late, but could help someone.

example:

import subprocess
pass_arg=[]
pass_arg[0]="/home/test.sh"
pass_arg[1]="arg1"
pass_arg[2]="arg2"

subprocess.check_call(pass_arg)

The above example provides arg1 and arg2 as parameters to the shell script test.sh

Essentially, subprocess expects an array. So you could populate an array and provide it as a parameter.

Garrido answered 23/10, 2016 at 4:2 Comment(0)
C
1

You forgot to add args name.

subprocess.Popen(args=['./test.sh', 'arg1 arg2 %s' % arg3], shell=True)
Cowcatcher answered 21/6, 2013 at 19:33 Comment(1)
so the file path needs to be in the args set as well?Poulson
T
1

This is how it worked for me

import subprocess
subprocess.Popen(["path/to/file.sh", arg1, arg2, arg3])
Tit answered 20/5, 2022 at 20:13 Comment(0)
S
0

Since Python 3.5, most use cases are fine using subprocess.run() instead of Popen() (which of course can still be used).

See also this answer: https://mcmap.net/q/143076/-what-is-the-difference-between-subprocess-popen-and-subprocess-run

The fix for this question remains "use either a list of arguments or just one string", as stated in this section of the documentation.

Sheepwalk answered 27/5, 2024 at 12:48 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.