paramiko 中的管道命令
如何在 paramiko 中运行管道命令?我正在这样做:
statement = 'grep thing file | grep thing2 | tail -1'
last_msg = conn.execute(statement)
我只得到 grep thing file
的输出。
How do I run piped commands in paramiko? I'm doing this:
statement = 'grep thing file | grep thing2 | tail -1'
last_msg = conn.execute(statement)
and I get the output of grep thing file
only.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为
grep
不知道如何处理|
。准备好进行一些令人讨厌的转义:这会在另一端创建一个 shell,并要求它解释字符串
grep thing file | grep 事物2 |尾-1。单引号是必需的,因为
sh -c
仅接受单个参数。这样,shell 将为您创建管道,运行所有命令。并且您最好确保文件名
file
不包含空格。如果是,请尝试“file”
。正如您所看到的,这很快就会变得非常难看。我建议你将管道放入 shell 脚本中。然后您可以避免引号,只需使用
sh -c script.sh
运行脚本。Because
grep
doesn't know how to handle|
. Get ready for some nasty escaping: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 sincesh -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
.