简体   繁体   中英

How to store bash shell script in the JSON document effectively for Python script

I am working on Python along with bash shell script. I have a separate program which is going to store the bash shell script inside the JSON document (in zookeeper node) and that JSON document will look something like this -

{"script":"#!/bin/bash\\necho Hello world 1\\n"}

Now from my Python program, I need to read the above JSON document (from the zookeeper node) and then extract the script portion of it and then execute the shell script.

As you can see in my above json string, I need to store my bash shell script in such a way so that I can execute it successfully while retrieving the data from JSON string in Python script.

Meaning, I need to escape certain things and add \\\\n as well in the bash shell script in the JSON string to make it work.

Now from the Python script, I can simply do like this -

jsonStr = '{"script":"#!/bin/bash\\necho Hello world 1\\n"}' # get it from zookeeper
j = json.loads(jsonStr)

shell_script = j['script']

print "start"
proc = subprocess.Popen(shell_script, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = proc.communicate()
if stderr:
   print "Shell script gave some error"
   print stderr
else:
   print stdout
   print "end" # Shell script ran fine.

Now my question is-

  1. How do I structure my bash shell script in the JSON string in such a way so that no extra effort is needed while storing it in zookeeper node from a different program which is written in Java.
  2. And also no extra effort needed while executing the shell script by extracting it from the JSON string from the Python program.?

By no extra effort I mean, what's the best way to store the big bash shell script in the JSON document? And how easily execute it from Python script?

I might have around 60-70 lines of shell script as well.

UPDATE:-

I get the JSON document from the Zookeeper like this -

data, stat = zk.get(node)
jsonString = data.decode("utf-8")
jStr = json.loads(jsonString)
shell_script = j['script']

I am using Kazoo API for Zookeeper which is in Python. And this is the method -

http://kazoo.readthedocs.org/en/latest/api/client.html#kazoo.client.KazooClient.get

Then how do I represent this shell script as a shell script in the JSON document.. Below shell script accepts certain parameters from the Python program. I have those parameters in my Python program and I am sending it through os.env

#!/bin/bash

hello=$jj1

echo $hello

echo $jj1
echo $jj2

for el1 in $jj3
do
    echo "$el1"
done

for el2 in $jj4
do
    echo "$el2"
done

for i in $( ls ); do
    echo item: $i
done

So that I can execute it successfully from the Python script?

This is how I represented it and I will write this JSON String from the Java program as it is to zookeeper node -

{"script":"#!/bin/bash \n hello=$jj1 \n echo $hello \n echo $jj1 \n echo $jj2 \n for el1 in $jj3 \n do \n echo "$el1" \n done \n for el2 in $jj4 \n do \n   echo "$el2" \n done \n for i in $( ls ); do \n echo item: $i \n done"}

When I ran below command -

   jsonStr = '{"script":"#!/bin/bash hello=$jj1 echo $hello echo $jj1 echo $jj2 for el1 in $jj3 do echo \"$el1\" done for el2 in $jj4 do echo \"$el2\" done for i in $( ls ); do echo item: $i done"}'

print repr(jsonStr)

this got printed out on the console -

'{"script":"#!/bin/bash hello=$jj1 echo $hello echo $jj1 echo $jj2 for el1 in $jj3 do echo "$el1" done for el2 in $jj4 do echo "$el2" done for i in $( ls ); do echo item: $i done"}'

And this is a valid JSON String..

使用json

json.encoder.encode_basestring_ascii('{"script":"#!/bin/bash\\necho Hello world 1\\n}')

You don't need to do anything if you revert to earlier revision of your question where \\n is correctly used in the json document instead of \\\\n . Here's an example .

You might be confused by double-escaping that is required when you specify json document as a Python string literal that you don't need to (and should not) do in this case.

data contains literal newlines inside json string. It is not valid json. No json generator would produce such text. Don't generate json text manually in you Java code, use any json library instead.

Your latest example contains unescaped quotes inside json string. It makes it invalid json. Replace

"a "$b" c"

-like strings with

"a \"$b\" c"

json string. print repr(json_string) would look like:

'"a \\"$b\\" c"'

ie, the slash is double-escaped. Do not double escape unless you are specifying a json string using Python string literal. Meaning, if your json comes from zookeeper you should not do it.

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