[英]How do I pass Python arrays to shell script as an Array?
I have a shell script (test.sh) in which I am using bash arrays like this - 我有一个shell脚本(test.sh),其中我正在使用bash数组-
#!/bin/bash
...
echo $1
echo $2
PARTITION=(0 3 5 7 9)
for el in "${PARTITION[@]}"
do
echo "$el"
done
...
As of now, I have hardcoded the values of PARTITION array in my shell script as you can see above.. 到目前为止,如上所示,我已经在shell脚本中对PARTITION数组的值进行了硬编码。
Now I have a Python script as mentioned below from which I am calling test.sh shell script by passing certain parameters such as hello1
and hello2
which I am able to receive as $1
and $2
. 现在,我有了一个如下所述的Python脚本,通过传递某些参数(例如
hello1
和hello2
,我可以从其中调用test.sh shell脚本,我可以将其作为$1
和$2
接收。 Now how do I pass jj['pp']
and jj['sp']
from my Python script to Shell script and then iterate over that array as I am doing currently in my bash script? 现在如何将
jj['pp']
脚本中的jj['pp']
和jj['sp']
从我的Python脚本传递到Shell脚本,然后遍历该数组?
Below script doesn't work if I am passing jj['pp']
如果我传递
jj['pp']
则以下脚本不起作用
import subprocess
import json
import os
hello1 = "Hello World 1"
hello2 = "Hello World 2"
jsonData = '{"pp": [0,3,5,7,9], "sp": [1,2,4,6,8]}'
jj = json.loads(jsonData)
print jj['pp']
print jj['sp']
# foo = (0, 3, 5, 7, 9)
# os.putenv('FOO', ' '.join(foo))
print "start"
subprocess.call(['./test.sh', hello1, hello2, jj['pp']])
print "end"
UPDATE:- 更新: -
Below JSON document is going to be in this format only - JSON文件下方将仅采用这种格式-
jsonData = '{"pp": [0,3,5,7,9], "sp": [1,2,4,6,8]}'
so somehow I need to convert this to bash arrays while passing to shell script.. 所以我需要在传递给shell脚本的同时将其转换为bash数组。
Python 蟒蛇
import os
import json
import subprocess
hello1 = "Hello World 1"
hello2 = "Hello World 2"
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( 'jj', ' '.join( str(v) for v in jj['pp'] ) )
print "start"
subprocess.call(['./test.sh', hello1, hello2 ])
print "end"
bash 庆典
echo $1
echo $2
for el in $jj
do
echo "$el"
done
Taken from here: Passing python array to bash script (and passing bash variable to python function) 从此处获取:将python数组传递给bash脚本(并将bash变量传递给python函数)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.