Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

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.

share|improve this answer
add comment

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.

share|improve this answer
 
Thanks for the suggestion. Can you provide a simple example basis on my example? It will help me to understand better. I am little rusty in Python and bash scripting.. –  SSH Dec 24 at 9:18
 
subprocess.call('echo "$foo"', env={'foo': '42'}, shell=True) –  Ignacio Vazquez-Abrams Dec 24 at 9:19
 
Sorry.. :( still not able to understand.. How I will use this in my Python code.. –  SSH Dec 24 at 9:21
 
Can you help me here with a simple example which can help me understand better? I am sorry as my understanding is not that much with Python and subprocess module... –  SSH Dec 24 at 10:08
 
There is no example simpler than the one I gave you. I cannot think for you. –  Ignacio Vazquez-Abrams Dec 24 at 10:28
add comment

Try this command:

    subprocess.call(j['script', str(var1), str(var2)], shell=True)
share|improve this answer
 
what is var1 and var2 here? Can you provide full example? –  SSH Dec 24 at 9:24
 
That's a comment not an answer. –  Kobi K Dec 24 at 9:25
 
@ssh subprocess.call(j['script' , testing], shell=True) –  arun Dec 24 at 9:27
 
Throwing together random bits of syntax rarely has a correct (or even coherent) result. –  Ignacio Vazquez-Abrams Dec 24 at 9:27
 
@arun: and how my shell script will look like? –  SSH Dec 24 at 9:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.