Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: subprocess and running a bash script with multiple arguments

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!

like image 523
user2510173 Avatar asked Jun 21 '13 19:06

user2510173


People also ask

How do I pass multiple arguments to a shell script?

To pass multiple arguments to a shell script you simply add them to the command line: # somescript arg1 arg2 arg3 arg4 arg5 … To pass multiple arguments to a shell script you simply add them to the command line: # somescript arg1 arg2 arg3 arg4 arg5 …

How many arguments can be passed to bash script?

NUM} ) in the 2^32 range, so there is a hard limit somewhere (might vary on your machine), but Bash is so slow once you get above 2^20 arguments, that you will hit a performance limit well before you hit a hard limit.

Why are shells true in subprocess?

Setting the shell argument to a true value causes subprocess to spawn an intermediate shell process, and tell it to run the command. In other words, using an intermediate shell means that variables, glob patterns, and other special shell features in the command string are processed before the command is run.

Can you pass in an argument to a bash script?

You can pass the arguments to any bash script when it is executed. There are several simple and useful ways to pass arguments in a bash script. In this article guide, we will let you know about some very easy ways to pass and use arguments in your bash scripts.


1 Answers

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).

like image 101
jfs Avatar answered Oct 08 '22 20:10

jfs