将bash用作Python命令命名空间函数的首选Shell
我需要使用一个适合bash风格的命令行,比如:
diff x <(cat y | tail +2)
但是,使用sh -c来执行上面的命令时会出错,因此上面的commands.getoutput也失败了。不过,使用bash -c执行上面的命令就能达到我想要的效果。有人能建议我怎么让python使用bash吗?我知道可以用subprocess来实现,但我有很多命令行需要处理,我只想用commands.getoutput。
谢谢。
1 个回答
3
你可以用 commands.getoutput("bash -c 'diff x <(tail +2 y)'")
这个命令来实现,可能会有效果(注意,cat
这个命令其实用不上)。
不过,commands.getoutput('tail +2 y | diff x -')
这个命令就不需要用到 Bash 了。