2

I have this command command that at some point, returns: Press Enter to continue.

I would like to write a script that calls command, reads the so-far command output, does something with it when Press Enter to continue arrives, and simulates an enter key-press when this is done.

Any chance I can achieve that? :-D

Something like:

myscript | command > output

with myscript

#!/bin/bash

cp output output2 # output2 should only contain the output until Press Enter to continue
echo -ne '\n'

except it doesn't work! :-)

Augustin Riedinger
  • 20,909
  • 29
  • 133
  • 206
  • 1
    `echo -ne '\n'` is amazingly bizarre. That is effectively the same as `echo` only less portable and more to type and read. – Etan Reisner Apr 08 '16 at 17:04
  • That said what about this attempt doesn't work? Does it not confirm the prompt? Is the prompt reading from standard input or from the tty directly? I also didn't understand the bit about the output file. You want to copy the output file right before "hitting enter"? – Etan Reisner Apr 08 '16 at 17:06
  • 3
    look for a tutorial on the `expect` program? (AND it's unclear what you mean by "I have this command `> command`". Is the `>` the cmd prompt? If so, you don't need to include it. If meant to indicate file redirection, you're creating, or wiping out the file `command` in your current dir). Please try to make a small, reproducible test case we can follow along with. Good luck. – shellter Apr 08 '16 at 17:09
  • `echo -ne '\n'` comes from [there](http://stackoverflow.com/questions/6264596/simulating-enter-keypress-in-bash-script). `cp output output2` is only an example, I'll actually `sed` something and create another file out of it. Yes `>` was the command prompt, I removed it! My example actually works correctly, except it does't receive the input right before `Press Enter to continue`, which *is* the one I need... I tried to add some `sleep`, without success... – Augustin Riedinger Apr 08 '16 at 17:17

1 Answers1

1

Ideally you should use expect command to achieve that, example code:

#!/usr/bin/expect
set timeout 600
spawn command
expect "Press Enter to continue" { send "\r" }

Note: Replace command with your command.

then save it into the script, make it executable and run it.

Check: man expect for further information.

On OS X install via: brew install expect.

kenorb
  • 155,785
  • 88
  • 678
  • 743