Send key code to command line on OS X

I want to make a script that runs a program and then sends its key input. In the psuedo-script:

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

      

The program works, so if there are additional commands in the script, they will not be achieved unless control-z is specified to terminate the program.

Is it possible? From what I found, I thought it might require keycodes to do this, but I could be wrong.

+2


source to share


4 answers


You may be looking for expect

(from http://expect.nist.gov/ ). This is due to the complexities of pseudo-tty, which makes the program seem like the input from the script (in this case, the program expect

) is coming from the terminal.



Alternatively, you can use echo

or cat

and print its output to a program - it depends on the program.

+1


source


I think this is probably a better solution than "expecting" as it can be done in a native bash script, I will be interested to see what you think.

Using

`printf "character code here"` 

      

pay attention to the back marks

So, for example, I wrote a script that manages a gnu remote session, the following line opens window 2 and gives out the key combination ctrl-c

ssh -t user@$host screen -p 2 -X stuff `printf "\003"`

      

  • The -t option simulates terminal login on a remote computer.
  • -p allows us to specify the name or number of the window we are connecting to in the screen session.
  • \ 003 is the bash character format of character code 0x03


See here for a complete link to the codes.

To find the code of some key entry you can use

printf "%#x\n" "'X"
0x58

      

  • If X is the key you want to find the code
  • To find non-literal codes, you can use ctrl-v (makes bash add the following key to the command line, not intepret) and then enter the key combination, so if I wanted to find the key code for ctrl-c I would remove X. by pressing ctrl-v and then pressing ctrl-c.

The last thing the ascii code link above is listing 0x13 as carriage return, but in the screen manual they list 0x15 as enter key code, does anyone know why? Ive tested on local screen and when I press enter 0x13 is produced, but when sending commands via ssh to the remote screen 0x13 does not work, but 0x15 does.

Hope it helps

Pier

+2


source


If you want the program to start in the background, just do

#!/bin/bash
./program&

      

0


source


If you intend to use the program, use:

./program &    # The & sends the command to the background
echo commands here are executed while program is in the background
…
wait           # Wait for the completion of background commands
echo commands here are executed after background program has completed

      

Edit: If you intend to stop the program (since ctrl-Z often runs in * nix shells), you can send a STOP signal to it:

kill -STOP pid

      

To resume execution, send it a CONT signal:

kill -CONT pid

      

Each of these examples pid

has a program process ID. If you run it in a script, it's easy to get with a variable $!

eg.

./prog &
echo prog started in the background
pid_of_prog=$!
kill -STOP $pid_of_prog
echo prog stopped
kill -CONT $pid_of_prog
echo prog continues
wait
echo prog finished

      

Edit 2: If your program is the one that exits when it receives the ctrl-Z character, then remember that the control characters have the numeric value of the letter of the position in the alphabet (i.e. Ctrl-A is 1, Ctrl-B is 2 etc.). To send this symbol to the program, you can:

echo -e "\032" | ./prog

      

( 032

equals 26, ie ^ Z, in octal. Of course, you can create the same character any way you want, perhaps by appending it to the end of another input, eg ( cat inputfile ; echo -e "\032" ) | ./prog

.

But that might not work; the program must be designed to recognize this character from the input (which it probably won't); usually the shell catches it. Again, most programs reading input from stdin

just exit when the input ends, so redirecting any final input (even </dev/null

) should result in it terminating.

And finally, if the goal was to stop the execution of the program when some other event happened (detected elsewhere in the script), you can just kill

...

0


source







All Articles