从JSON文档执行Python中的shell脚本

从JSON文档执行Python中的shell脚本,python,json,bash,shell,Python,Json,Bash,Shell,我正在尝试使用子流程模块在Python中执行shell脚本 下面是我的shell脚本,名为testing.sh #!/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 现在我尝试用Python执行上面的shell脚本,所以我喜欢这样- subprocess.call(['.

我正在尝试使用子流程模块在Python中执行shell脚本

下面是我的shell脚本,名为
testing.sh

#!/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
现在我尝试用Python执行上面的shell脚本,所以我喜欢这样-

subprocess.call(['./testing.sh'])
[0, 3, 5, 7, 9]
[1, 2, 4, 6, 8]
start
Hello World 1
Hello World 2
0
3
5
7
9
1
2
4
6
8
end
而且效果很好。现在我想在JSON文档中添加上面的脚本,就像这样,然后执行它-

json_script = '{"script":"above testing.sh script here"}'
j = json.loads(json_script)
shell_script = j['script']
subprocess.call(shell_script, shell=True)
但每次我尝试时,它都会给我一个错误-

下面是我的完整Python脚本,其中包含JSON文档中的上述
testing.sh
shell脚本-

#!/usr/bin/python

import subprocess
import json
import socket
import os

jsonData = '{"pp": [0,3,5,7,9], "sp": [1,2,4,6,8]}'
jj = json.loads(jsonData)

print jj['pp']
print jj['sp']

os.putenv( 'jj1',  'Hello World 1')
os.putenv( 'jj2',  'Hello World 2')
os.putenv( 'jj3', ' '.join( str(v) for v in jj['pp']  ) )
os.putenv( 'jj4', ' '.join( str(v) for v in jj['sp']  ) )

print "start"

jsonDataaa = '{"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"}'
j = json.loads(jsonDataaa)

shell_script = j['script']
print "start"
subprocess.call(shell_script, shell=True)
print "end"
下面是我得到的错误-

 File "C:\Python27\lib\json\__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "C:\Python27\lib\json\decoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python27\lib\json\decoder.py", line 381, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting , delimiter: line 1 column 113 (char 112) 
我的预期输出应该是这样的-

subprocess.call(['./testing.sh'])
[0, 3, 5, 7, 9]
[1, 2, 4, 6, 8]
start
Hello World 1
Hello World 2
0
3
5
7
9
1
2
4
6
8
end
更新:-


如果我的Jsondaaa是这样的

jsonDataaa = '{"script":"#!/bin/bash \\n hello=$jj1 \\n echo $hello \\n echo $jj1 \\n echo $jj2 \\n"}'

然后它工作得很好。。。我能够正确地执行它。。但如果我的jsonDataaa是我在问题中提到的,那么它只会给我一个错误。我想可能有一些语法错误,我无法理解

这部分没有什么意义:

jsonDataaa = '{"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"}'
j = json.loads(jsonDataaa)

shell_script = j['script']
print "start"
subprocess.call(shell_script, shell=True)
这里所做的是将文本字符串作为参数传递给subprocess.call(),但它需要的是程序名,而不是程序文本。因此,您有两种选择:您可以将
shell\u脚本的内容写入一个命名的临时文件并执行该文件,或者您可以在子流程中启动管道
bash
,并将其stdin作为字符串从
shell\u脚本
馈送。我更喜欢后一种方法,您可以在这里获得更多帮助:


注意:如果使用三重引号字符串,可以使JSON看起来更漂亮,多行等等。

这部分没有太大意义:

jsonDataaa = '{"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"}'
j = json.loads(jsonDataaa)

shell_script = j['script']
print "start"
subprocess.call(shell_script, shell=True)
这里所做的是将文本字符串作为参数传递给subprocess.call(),但它需要的是程序名,而不是程序文本。因此,您有两种选择:您可以将
shell\u脚本的内容写入一个命名的临时文件并执行该文件,或者您可以在子流程中启动管道
bash
,并将其stdin作为字符串从
shell\u脚本
馈送。我更喜欢后一种方法,您可以在这里获得更多帮助:


注意:如果使用三重引号字符串,可以使JSON看起来更漂亮,多行等等。

这部分没有太大意义:

jsonDataaa = '{"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"}'
j = json.loads(jsonDataaa)

shell_script = j['script']
print "start"
subprocess.call(shell_script, shell=True)
这里所做的是将文本字符串作为参数传递给subprocess.call(),但它需要的是程序名,而不是程序文本。因此,您有两种选择:您可以将
shell\u脚本的内容写入一个命名的临时文件并执行该文件,或者您可以在子流程中启动管道
bash
,并将其stdin作为字符串从
shell\u脚本
馈送。我更喜欢后一种方法,您可以在这里获得更多帮助:


注意:如果使用三重引号字符串,可以使JSON看起来更漂亮,多行等等。

这部分没有太大意义:

jsonDataaa = '{"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"}'
j = json.loads(jsonDataaa)

shell_script = j['script']
print "start"
subprocess.call(shell_script, shell=True)
这里所做的是将文本字符串作为参数传递给subprocess.call(),但它需要的是程序名,而不是程序文本。因此,您有两种选择:您可以将
shell\u脚本的内容写入一个命名的临时文件并执行该文件,或者您可以在子流程中启动管道
bash
,并将其stdin作为字符串从
shell\u脚本
馈送。我更喜欢后一种方法,您可以在这里获得更多帮助:

注意:如果使用三重引号字符串,可以使JSON看起来更漂亮,多行等等。

您还可以:

subprocess.call(["/bin/sh", "-c", shell_script])
将整个脚本作为单个命令行参数传递给sh。 即使shell_脚本中有换行符,这也应该有效;但是如果它超过一定的大小(16K字节?类似的东西),它就不能放在命令行中

编辑-通过使用
subprocess.call(shell\u script,shell=True)
它实际上与我建议的差不多(可能其他选项的效果可能不同)。因此,现在我猜测,正如您所暗示的,etc的引用可能不正确-请参阅下面的评论。

您也可以这样做:

subprocess.call(["/bin/sh", "-c", shell_script])
将整个脚本作为单个命令行参数传递给sh。 即使shell_脚本中有换行符,这也应该有效;但是如果它超过一定的大小(16K字节?类似的东西),它就不能放在命令行中

编辑-通过使用
subprocess.call(shell\u script,shell=True)
它实际上与我建议的差不多(可能其他选项的效果可能不同)。因此,现在我猜测,正如您所暗示的,etc的引用可能不正确-请参阅下面的评论。

您也可以这样做:

subprocess.call(["/bin/sh", "-c", shell_script])
将整个脚本作为单个命令行参数传递给sh。 即使shell_脚本中有换行符,这也应该有效;但是如果它超过一定的大小(16K字节?类似的东西),它就不能放在命令行中

编辑-通过使用
subprocess.call(shell\u script,shell=True)
它实际上与我建议的差不多(可能其他选项的效果可能不同)。因此,现在我猜测,正如您所暗示的,etc的引用可能不正确-请参阅下面的评论。

您也可以这样做:

subprocess.call(["/bin/sh", "-c", shell_script])
将整个脚本作为单个命令行参数传递给sh。 即使shell_脚本中有换行符,这也应该有效;但是如果它超过一定的大小(16K字节?类似的东西),它就不能放在命令行中


编辑-通过使用
subprocess.call(shell\u script,shell=True)
它实际上与我建议的差不多(可能其他选项的效果可能不同)。所以现在我猜,正如您所暗示的那样,etc的引用可能是不正确的-请参阅下面的注释。

由于您的json字符串无效,因此会出现此错误。具体来说,它是持续的