I have a command-line program that normally gets its parameters from the keyboard after the program is run. Something like this:

Enter parameter 1? 3
Enter parameter 2? 2.6
Calculate something y/n? y

Rewriting the program to take command-line parameters is not possible.

I'd like to time how long it takes to execute the program with a batch file that is similar to the following:

@echo %time%
program.exe < params.txt
@echo %time%

The problem is that for some reason, the final parameter does not get accepted.

Note that the final parameter is a y/n, and I have added an empty line at the end so there is a newline after the y/n.

The input file, params.txt:

*empty line*   

You can automate interactive command line programs using expect

Here's an example (for telnet) from the Wikipedia article

# Assume $remote_server, $my_user_id, $my_password, and $my_command were read in earlier 
# in the script.
# Open a telnet session to a remote server, and wait for a username prompt.
spawn telnet $remote_server
expect "username:"
# Send the username, and then wait for a password prompt.
send "$my_user_id\r"
expect "password:"
# Send the password, and then wait for a shell prompt.
send "$my_password\r"
expect "%"
# Send the prebuilt command, and then wait for another shell prompt.
send "$my_command\r"
expect "%"
# Capture the results of the command into a variable. This can be displayed, or written to disk.
set results $expect_out(buffer)
# Exit the telnet session, and wait for a special end-of-file character.
send "exit\r"
expect eof

It should be clear that you can use this approach for your program as you know the prompt strings and you know what responses to feed it.

  • Note that apparently, in order to use this answer, you may need to have additional software (TCL?, Expect?) installed in your environment. Oct 30 '14 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.