【问题标题】:pexpect and shell meta characters (>, |, or *)pexpect 和 shell 元字符(>、| 或 *)
【发布时间】:2012-11-22 00:19:33
【问题描述】:

我现在很困惑。

pexpect documentation 声明如下:

Remember that Pexpect does NOT interpret shell meta characters such as
redirect, pipe, or wild cards (>, |, or *). This is a common mistake.
If you want to run a command and pipe it through another command then
you must also start a shell. For example::

child = pexpect.spawn('/bin/bash -c "ls -l | grep LOG > log_list.txt"')
child.expect(pexpect.EOF)

但是,我正在查看一些在 pexpect.sendline(some command | grep 'something') 中使用 |* 的旧代码。 所以我开始测试这些命令,它们似乎都可以工作。还值得一提的是,我没有使用修改过的 pexpect 模块,它是 python 的普通 pexpect。

怎么会?为什么 pexpect 提到 元字符不起作用,而它显然是这样做的?

【问题讨论】:

  • 代码传递给pexpect.sendline()的是什么?
  • @ecatmur,特定于机器的命令,但通常带有 |命令后的 grep 或 |和 *。见上面修改过的代码。

标签: python ssh pexpect metacharacters


【解决方案1】:

pexpect.spawn 不解释 shell 元字符,但是 pexpect 中运行的任何东西(可能是 shell)显然可以:

child = pexpect.spawn('/bin/bash')
child.sendline('echo hello | cat')

pexpect 只是将字符串传递给子进程;它不是在解释它。

【讨论】:

  • 当时的文档真的不清楚,因为它说“Pexpect 不解释 shell 元字符”
  • @theAlse 对,pexpect 没有解释元字符;它直接将它们传递给子进程不变。子进程正在解释它们。
  • 但我看到另一件事,你实际上是在开始 bash!但在我的情况下,我连接到不提供 bash 的嵌入式机器,它只是一个 shell,可以解释通过 ssh/telnet 接收到的一些特殊命令以及一些其他命令,例如 grep(它没有 /bash)。
  • @theAlse bash 只是一个 shell 的例子;你的嵌入式机器正在运行一个 shell。
猜你喜欢
  • 2020-04-17
  • 1970-01-01
  • 1970-01-01
  • 2013-08-09
  • 2017-06-23
  • 1970-01-01
  • 2014-04-02
  • 1970-01-01
相关资源
最近更新 更多