简体   繁体   中英

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?

使用子进程调用您的Shell脚本

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM