Python: pexpect 不执行 `backtick` 命令
Python: pexpect does not execute `backtick` command
我正在尝试 运行 这个命令:
foo=`ls /`
在 bash 上完美运行,但如果我通过 pexpect 执行它则不行:
p = pexpect.spawn("foo=`ls /`").interact()
// Gives error command was not found or not executable: foo=`ls
这是什么原因,我该如何解决?我什至尝试过转义 ` 但它似乎不起作用。
您尝试执行的命令需要 bash
。 pexpect
不会通过 bash
传递您的命令,而是像 shell.
一样直接调用您的可执行文件
来自the docs:
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)
虽然文档没有提到它们,但这当然也适用于反引号。因此,编写代码以显式调用 bash
:
p = pexpect.spawn('/bin/bash -c "foo=`ls /`"').interact()
我正在尝试 运行 这个命令:
foo=`ls /`
在 bash 上完美运行,但如果我通过 pexpect 执行它则不行:
p = pexpect.spawn("foo=`ls /`").interact()
// Gives error command was not found or not executable: foo=`ls
这是什么原因,我该如何解决?我什至尝试过转义 ` 但它似乎不起作用。
您尝试执行的命令需要 bash
。 pexpect
不会通过 bash
传递您的命令,而是像 shell.
来自the docs:
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)
虽然文档没有提到它们,但这当然也适用于反引号。因此,编写代码以显式调用 bash
:
p = pexpect.spawn('/bin/bash -c "foo=`ls /`"').interact()