2

I am pretty confused right now.

pexpect documentation states the following:

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)

However I am looking at some old code that uses | and * in pexpect.sendline(some command | grep 'something'). So I started to test these command and they all seem to work. It is also worth mentioning that I am not using a modified pexpect module, it is plain old pexpect for python.

How come? why does pexpect mention that meta characters does not work, when it obviously does so?

Community
  • 1
  • 1
theAlse
  • 5,577
  • 11
  • 68
  • 110

1 Answers1

3

pexpect.spawn doesn't interpret shell meta characters, but whatever's running inside pexpect (presumably a shell) clearly does:

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

pexpect is just passing the string to the child process; it's not interpreting it.

ecatmur
  • 152,476
  • 27
  • 293
  • 366
  • the documentation is really unclear then, since it say "Pexpect does NOT interpret shell meta characters" – theAlse Dec 04 '12 at 13:39
  • @theAlse right, pexpect isn't interpreting the meta characters; it's passing them straight to the child process unchanged. The child process is interpreting them. – ecatmur Dec 04 '12 at 13:39
  • but I see another thing, you are actually starting a bash! But in my case I connect to an embedded machine that does not provide a bash, it is just a shell which can interpret some special command received via ssh/telnet plus some other commands such as grep (it does not have a /bash). – theAlse Dec 04 '12 at 13:42
  • @theAlse bash is just an example of a shell; your embedded machine is running a shell. – ecatmur Dec 04 '12 at 13:45