我有一个像这样的config_file.yml文件:
sample:
sql: "select * from dbname.tableName where sampleDate>='2018-07-20';"
config: {'hosts': [!!python/tuple ['192.162.0.10', 3001]]}
sample2:
sql: "select * from dbname.tableName where sampleDate<='2016-05-25';"
config: {'hosts': [!!python/tuple ['190.160.0.10', 3002]]}
我的python代码是:
data_config = yaml.load(config_file)
for dataset, config in data_config.items():
args = [config]
cmd = ['./execute_something.sh']
cmd.extend(args)
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).communicate()
execute_something.sh:
#!/bin/bash
echo $1
data_config=$1
echo $data_config
所以基本上我想将{'sql': "select * from dbname.tableName where sampleDate>='2018-07-20';", config: {'hosts': [!!python/tuple ['190.160.0.10', 3002]]}}
整个字符串作为参数传递给Shell脚本。
问题:1)select *
最终列出了当前目录中的所有文件,而不是完全以字符串形式传递2)即使我传递了一个简单的字符串,说它args="hi"
仍然行不通!
我不明白我在这里想念的是什么。请帮助。谢谢!
shell=True
。data_config = yaml.load(config_file)
for dataset, config in data_config.items():
cmd = ['./execute_something.sh', str(config)]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()
运行时shell=True
,您将sh -c
进入字面量参数列表。在这种情况下,将执行以下操作(将转义添加为单引号文字):
sh -c './execute_something.sh' '{'"'"'sql'"'"': "select * from dbname.tableName where sampleDate>='"'"'2018-07-20'"'"';", config: {'"'"'hosts'"'"': [!!python/tuple ['"'"'190.160.0.10'"'"', 3002]]}}'
那不行 如果愿意,可以在shell中手动尝试。为什么呢 因为以开头的参数{
没有传递给./execute_something.sh
而是被传递到执行的shell中sh -c
。
什么会工作,如果你真的坚持维持shell=True
?比较以下内容:
sh -c './execute_something.sh "$@"' _ '{'"'"'sql'"'"': "select * from dbname.tableName where sampleDate>='"'"'2018-07-20'"'"';", config: {'"'"'hosts'"'"': [!!python/tuple ['"'"'190.160.0.10'"'"', 3002]]}}'
在此,紧随其后的参数-c
是一个shell脚本,它查看其参数并将这些参数传递给execute_something.sh
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句