Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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:

3
2.6
y
*empty line*   
share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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