0

I am trying to run a Windows (XP) command line program which prompts for 'Enter' or 'y' to continue. Currently, I can respond to the 'y's by running the program as: echo y | name_of_binary in a batch file.

I can't figure out how to tell the shell to respond with 'Enter' or 'y' when this is required. If it gets to a 'Press Enter to continue' sort of prompt (of which there are not very many, but enough to cause problems) and echoes 'y', it get stuck in a weird loop and won't accept any input (it spews thousands of 'press enter to continue's). If I could echo first an Enter and then a 'y' in sequence, that might work, but none of the methods I tried for echoing an 'Enter' keypress worked.

I am ultimately calling this batch file through os.system() in Python. If there is a way to get Python to run the binary (through os.system(name_of_binary)?) AND respond to the prompts, that would be ideal. I have already tried os.system(echo y | name_of_binary) which behaved the same way as the batch file (as it should). Should I be using a different approach, or can I solve this by modifying the 'echo ...' command I'm currently using?

dave35
  • 31
  • 1
  • 2
  • 3
  • 1
    use pexpect python module, you can send your desired response to the batch file when it prompts – avasal Nov 03 '11 at 06:14
  • Agreed, pexpect (the 'expect' python module) would probably be a better fit since OP is already using python. In a nutshell, expect is the right way to do this. It was built for such purposes. – bdutta74 Nov 04 '11 at 12:31
  • @dave35: If you are still interested in this topic, see [this question](http://stackoverflow.com/questions/8192318/why-does-delayed-expansion-fail-when-inside-a-piped-block-of-code/) that show a way to emulate pexpect Phyton module in Batch. Look for Aacini's answer. – Aacini Nov 21 '11 at 00:50

2 Answers2

2

The standard way to output an Enter in Batch is echo/ in a line with no additional spaces. To output an Enter followed by 'Y' you may try this: (echo/&echo Y) | name_of_binary (don't insert spaces in echo/&).

If that not works would be because the LineFeed characters that are inserted both after the first Enter and after the Y. The exact sequence of bytes generated by (echo/&echo Y) is: CrLfYCrLf where Cr is a Carriage Return and Lf is a Line Feed.

If previous method have problems, you may create a file with just CrY bytes with this Batch file:

@echo off
echo/> EnterY.txt
(
echo e101
echo 59
echo w
echo q
) | debug EnterY.txt > nul

and then execute your command this way: name_of_binary < EnterY.txt.

PS - Please note that echo y | name_of_binary generate y CrLf, that is, an space after the y; that should be echo y| name_of_binary.

Neel
  • 2,100
  • 5
  • 24
  • 47
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • +1, although I dont like the debug technic, as it's no longer supported – jeb Nov 03 '11 at 18:17
  • @jeb: Although you are right, anyone may get DEBUG from [this site](http://www.ibiblio.org/pub/micro/pc-stuff/freedos/files/distributions/1.0/pkgs/debugx.zip). – Aacini Nov 04 '11 at 04:00
0

Check out Expect for Windows.

That is all that you need. There are several books on Expect, and many tutorials (including samples). Your favourite search engine is your best friend, but the Activestate site URL given in above, has everything you'd need. While what you may find in terms of tutorials and books may be Unix centric, which is where expect originated, the Windows expect isn't too different.

Expect will allow you to automate the command-line interaction within batch scripts.

OGHaza
  • 4,795
  • 7
  • 23
  • 29
bdutta74
  • 2,798
  • 3
  • 31
  • 54
  • Excuse me. If I correctly understood, Expect can provide the responses required as input by a remote computer session, but I think it can not be used for a local command. How the bi-directional comunication is established between Expect and the command? This would require a bi-directional pipe and concurrent execution. Perhaps I missed something? – Aacini Nov 03 '11 at 17:03
  • @Aacini, expect will emulate (roughly speaking) user interactivity, as if input is being provided by locally attached keyboard. Have used it extensively on Unix command-line for very similar activity, so I am pretty sure. Only thing to check would be to see if it's capabilities on Windows side are similar, and I have no reason to suspect that it does :-) – bdutta74 Nov 04 '11 at 12:28
  • To do that with a DOS command-line program that will provide the input for a second program in Windows, the first program must establish a bi-directional pipe and execute the second one via START, otherwise the second program will run sequentially until the first program ends. As far as I know, there is no way to establish even the usual input pipe to a START command. Are you sure that a Python program with pexpect module can do that? Could you show us an example of how to control a second program this way in Unix? Thanks. – Aacini Nov 06 '11 at 04:42
  • I found the answer in Wikipedia: "**Expect** is a Unix automation tool... It uses Unix pseudo terminals to wrap up subprocesses transparently..." "In Unix, a **pseudo-terminal** is a pseudo-device pair that provides a text terminal interface without associated device. Instead, a process assumes the role of the hardware for the pseudo terminal session". No equivalent concept exists in Windows. This site: http://www.noah.org/wiki/Pexpect confirm this conclusion: "Pexpect does not currently work on the standard Windows Python". – Aacini Nov 06 '11 at 05:48