Search code examples
bashmacosscriptingkeypresskeycode

Send key code to command line program on OS X


I want to make a script that starts a program and then sends it key input. In psuedo-script:

#!/bin/bash
./program << (PRESS CONTROL-Z)

The program is running so if there were additional commands in the script they will not be reached unless say control-z terminates the program.

Is this possible? From what I've found I thought it might require key codes but I could be wrong.


Solution

  • You might be looking for expect (from http://expect.nist.gov/). This deals with the complexities of pseudo-ttys that make it appear to the program that the input from the script (in this scenario, the expect program) is coming from a terminal.

    Alternatively, you might be able to use echo or cat and pipe the output of that into the program - it depends on the program.