如何用Python子进程执行长bash序列

2024-09-26 04:49:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我想这么做:

Bash代码:

grub --batch << EOF
root (hd0,1)
find /boot/grub/menu.lst
setup (hd0)
quit
EOF

Python代码:

^{pr2}$

但这不管用。。 现在有人能用另一种方法来解决这个问题吗?在

非常感谢!在


Tags: 方法代码bashbatchsetuprootfindquit
2条回答

解决方案是将脚本作为一个字符串发送:

script = '''
root (hd0,1)
find /boot/grub/menu.lst
setup (hd0)
quit
'''
print subprocess.Popen('grub', stderr=subprocess.STDOUT).communicate(script)[0]

shell=True不应该是必需的。在

你可以做一些可怕的事情,比如:

subprocess.call('echo -e "root (hd0,1)\nfind /boot/grub/menu.lst\nsetup (hd0)\nquit" | grub batch', shell=True)

不过,我相信有更好的办法。在

相关问题 更多 >