Piped commands in paramiko

放肆的年华 提交于 2019-12-05 10:45:19

Because grep doesn't know how to handle |. Get ready for some nasty escaping:

statement = """sh -c 'grep thing file | grep thing2 | tail -1'"""

This creates a shell on the other side, and asks it to interpret the string grep thing file | grep thing2 | tail -1. The single quotes are necessary since sh -c accepts only a single argument.

That way, a shell will create the pipe for you, running all the commands. And you better be sure that the filename file doesn't contain spaces. If it does, try "file".

As you can see, this quickly gets very ugly. I suggest you put the pipeline into a shell script. Then you can avoid the quotes and just run the script with sh -c script.sh.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!