1

I need to send a standard input or a keycode right after the program starts.

For example

$ program | F3
2
  • Which is it, if you need to send key strokes you'll probably need something like expect, if you want the program to read from stdin you can put it on the other side, pipes go left to right, so stdout on the left gets connected to stdin on the right, e.g., echo Input | program will send the string input to stdin of program Commented Sep 27, 2016 at 16:29
  • Related (but not answered) question: unix.stackexchange.com/q/311049/135943 Commented Sep 28, 2016 at 6:57

1 Answer 1

6

You'd need to define starts there.

You can insert the sequences of characters sent by your terminal when you press F3 into the terminal device input buffer before running your program with the TIOCSTI ioctl() on some systems like Linux:

{
   perl -le 'require "sys/ioctl.ph";
   ioctl(STDIN, &TIOCSTI, $_) for split "","@ARGV"' "$(tput kf3)"
   program
}

Or you can use something like expect to run your program in a faked terminal layer, wait for it to output some prompt (or any indication that it is started up to the point you expect it to), and send that output then:

expect -c 'spawn -noecho program; expect {>} {send [exec tput kf3]}; interact'

(here expecting a > prompt, replace by some string that program outputs when it's ready to read input).

You can also replace its stdin with a pipe, but if that application expects things like F3 key presses, it's most likely an interactive application so will probably not like it when it's stdin is not a terminal. You can always give it a try anyway:

tput kf3 | program

However then, after program has read the output of tput, it will see end-of-file (nothing more to be read), which it may cause it to exit. With:

{ tput kf3; cat; } | program

We send the output of kf3 and then use cat to forward everything you type on the terminal to program. Then again, it's unlikely to work as most probably program would put the terminal in a mode where the input is sent as soon as you type and disable the echo which it can no longer do as its input is not the terminal.

You could and do that same setting yourself:

saved_settings=$(stty -g)
stty -icanon -echo min 1 time 0
{ tput kf3; cat; } | program
stty "$saved_settings"

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.