2012-12-04 27 views
2

我現在很困惑。pexpect和shell元字符(>,| |或*)

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提及元字符不工作,當它明顯這樣做?

+0

代碼傳遞給'pexpect.sendline()'是什麼? – ecatmur

+0

@ecatmur,機器特定的命令,但通常使用|在命令或|之後的grep和*。請參閱上面的修訂代碼 – theAlse

回答

3

pexpect.spawn不解釋shell元字符,但不管裏面Pexpect的運行(大概一個shell)顯然不:

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

Pexpect的只是路過串子進程;這不是解釋它。

+0

該文檔實際上還不清楚,因爲它說「Pexpect不解釋shell元字符」 – theAlse

+0

@ theAlse權利,pexpect不解釋元字符;它將它們直接傳遞給子進程。兒童進程正在解釋他們。 – ecatmur

+0

但我看到另一件事,你實際上開始了一個bash!但在我的情況下,我連接到一個不提供bash的嵌入式機器,它只是一個shell,它可以解釋通過ssh/telnet收到的一些特殊命令以及一些其他命令,如grep(它沒有a/bash)。 – theAlse

相關問題