【发布时间】:2013-10-21 23:13:10
【问题描述】:
我想从 python 脚本中执行一个带有 3 个参数的 shell 脚本。 (如此处所述:Python: executing shell script with arguments(variable), but argument is not read in shell script)
这是我的代码:
subprocess.call('/root/bin/xen-limit %s %s %s' % (str(dom),str(result),str('--nosave'),), shell=True)
变量 dom 和 result 包含字符串。
这是输出:
/bin/sh: --nosave: not found
更新:
即变量“结果”:
c1 = ['/bin/cat', '/etc/xen/%s.cfg' % (str(dom))]
p1 = subprocess.Popen(c1, stdout=subprocess.PIPE)
c2 = ['grep', 'limited']
p2 = subprocess.Popen(c2, stdin=p1.stdout, stdout=subprocess.PIPE)
c3 = ['cut', '-d=', '-f2']
p3 = subprocess.Popen(c3, stdin=p2.stdout, stdout=subprocess.PIPE)
c4 = ['tr', '-d', '\"']
p4 = subprocess.Popen(c4, stdin=p3.stdout, stdout=subprocess.PIPE)
result = p4.stdout.read()
之后,变量 result 包含一个带有 mbit 的数字(例如 16mbit)
而dom是一个类似“myserver”的字符串
【问题讨论】:
-
subprocess.call('/root/bin/xen-limit %s %s %s' % (str(dom),str(result),'--nosave'), shell=True)怎么样? -
@yakiang 得到同样的错误:/
-
result的值是多少?听起来它包含一些终止命令的 shell 元字符,使--nosave看起来像第二个命令,而不是xen-limit的选项。 -
@chepner 它包含一个带 mbit 的数字(例如 16mbit)我已经更新了我的第一篇文章。
-
如果你把
--nosave放在前面,你会得到同样的错误吗?您在下面的评论中“回显 3 个参数”是什么意思?
标签: python linux bash arguments execute