How to execute long bash sequence with Python subprocess
I wanna do this:
Bash code:
grub --batch << EOF
root (hd0,1)
find /boot/gru开发者_如何学JAVAb/menu.lst
setup (hd0)
quit
EOF
Python code:
subprocess.call('grub --batch << EOF', shell=True)
subprocess.call('root (hd0,1)', shell=True)
subprocess.call('find /boot/grub/menu.lst', shell=True)
subprocess.call('setup (hd0)', shell=True)
subprocess.call('quit', shell=True)
subprocess.call('EOF', shell=True)
But this doesn't work.. Somebody now the alternative way to solve this problem?
Thanks a lot!
The solution is to send the script as one string:
script = '''
root (hd0,1)
find /boot/grub/menu.lst
setup (hd0)
quit
'''
print subprocess.Popen('grub', stderr=subprocess.STDOUT).communicate(script)[0]
shell=True
should not be necessary.
You could do something horrible like:
subprocess.call('echo -e "root (hd0,1)\nfind /boot/grub/menu.lst\nsetup (hd0)\nquit" | grub --batch', shell=True)
I'm sure there is a better way to do this though.
精彩评论