2

I have an interactive application that takes input from the keyboard.

Whenever I run the application I always write the same things in the beginning. So I was thinking I could make a file with those things and redirect them to the application with:

myapplication < myinput.txt

The problem is that when I am done with that I want to give control back to the keyboard so I can type the rest of the input. Is it possible? I don't mind using a bash script.

1
  • Your app must handle that. After program start, when it hits EOF on fd 0, it should close it and open /dev/tty for reading. Then you can start consuming lines from the keyboard.
    – ott--
    Dec 26, 2012 at 16:24

1 Answer 1

3

I'm not sure about that one, but if the command you want to use is a simple non-interactive one, and you just want to types things into it (and prepend those with the content of 'myinput.txt', try:

cat myinput.txt - | command

ex:

cat myinput.txt - | grep something

But if you need more complicated interaction, you should probably use expect (which is not trivial to use at first, but very powerful).

4
  • Thanks. However I want firstly the myinput.txt to be sent to the command and after that be able and type things.
    – Pithikos
    Dec 26, 2012 at 16:06
  • The command I show above will : first output content of "myinput.txt" and then output content of "-" (which, here, for cat, means the standard input, ie what you'll type next). Try if it works, but as I said, it's limited and not very friendly (especially if the command you type into is something like a visual editor, etc). ex: cat myinput.txt - | ssh user@host works more or less. Finish by typing "exit" and return, and it should exit the ssh... (put : ls -al followed by a return, in myinput.txt, for example) Dec 26, 2012 at 16:23
  • You're right. It actually works. Thanks a dozen :)
    – Pithikos
    Dec 26, 2012 at 16:42
  • Enjoy :) (and note that the '-' notation is used in many unix utilities, for example tar, grep, etc., and is usefull for many things) Dec 27, 2012 at 11:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .