I have a shell script in my JSON document jsonStr
which I am able to execute it using Python subprocess module and it works fine.
Below is my Python script which works fine if I execute the shell script without passing anything -
import subprocess
import json
testing = "HelloWorld"
jsonStr = '{"script":"#!/bin/bash \\n STRING=\'Hello World\' \\n echo $STRING \\n"}'
j = json.loads(jsonStr)
print "start"
subprocess.call(j['script'], shell=True)
print "end"
Now is there any way to pass a variable value to my shell script in the json document from the Python script? Meaning I need to pass testing
value to my shell script and then print out from the shell script the value of testing
after getting executed from the subprocess module.
Is this possible to do?
Like the docs say, supply environment variables to the subprocess in the env
argument. Note that supplying the argument replaces the entire environment; you will need to supply the existing values if you want them available.
试试这个命令:
subprocess.call(j['script', str(var1), str(var2)], shell=True)
Can't you use this?
import subprocess
import json
testing = "HelloWorld"
jsonStr = '{"script":"#!/bin/bash \\n STRING=\'%VP%\' \\n echo $STRING \\n"}'.replace('%VP%', testing)
j = json.loads(jsonStr)
print "start"
subprocess.call(j['script'], shell=True)
print "end"
In the JSON you can a have placeholder and you can replace that placeholder instead of passing a Variable to the dynamically.
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.