我现在很困惑。
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.spawn
不会解释 shell 元字符,但 pexpect 内运行的任何内容(可能是 shell)显然可以解释:
child = pexpect.spawn('/bin/bash')
child.sendline('echo hello | cat')
pexpect 只是将字符串传递给子进程;它没有解释它。
关于python - pexpect 和 shell 元字符(>、| 或 *),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13701797/