RJP
RJP

Reputation: 405

Executing a shell script with arguments from a python script

My Shell script is executed like this from the command line

./pro.sh "Argument1"

I am calling it from my python script currently like this

subprocess.call(shlex.split('bash pro.sh "Argument1"'))

How do I pass the value of Argument1 as a variable. My argument to the script can be any string. How do I achieve this?

Upvotes: 0

Views: 10170

Answers (2)

Dan Lowe
Dan Lowe

Reputation: 56538

You can use

subprocess.Popen(["bash", "pro.sh", "Argument1"])

If your string argument is multiple words, it should work fine.

subprocess.Popen(["bash", "pro.sh", "Argument with multiple words"])

As long as the multiple words are in one string in the list passed to subprocess.Popen(), it is considered one argument in the argument list for the command.

You should not use shell=True unless you have a good reason. It can be a security problem if you aren't very careful how it is used.

Upvotes: 3

bigbounty
bigbounty

Reputation: 17368

use subprocess to call your shell script

subprocess.Popen(['run.sh %s %s' % (var1, var2)], shell = True)

Upvotes: 4

Related Questions