spugm1r3
spugm1r3

Reputation: 3649

Scripting item selection in menu that uses arrows

I am attempting to automate some diagnostics using scripts in bash that call programs/scripts that I did not personally write. Many of the scripts have menus based off case switches and read key presses, making it simple to feed in menu selections using a script, like so:

sh foo.sh <<EOF
0
0
1
EOF

However, I have one particular menu that does not appear to be operating on a case switch and instead forces the user to use the arrow keys to select an option indicated with an 'x', followed by the [Enter] key.

When I try to feed in options as above, the menu locks up and I have to ctrl+alt+del to restart this system. The only thing that seems to register is the 'new line', which advances the menu, but only on the first item in the menu. I have tried various escape sequences for the arrow keys, numbers, letters, etc, but to no avail. I have no idea how to create a menu that operates on arrow keys, so I don't have a basis to approach this problem.

To boil this down, I need a way to feed in selections to a menu of this type (whatever type it is), to help prevent the fat-fingering that occurs when simple tasks must be conducted repeatedly, and no feedback exists to ensure correct selection until AFTER a 4 hour test is done.

EDIT: For clarification, this is an older version of Ubuntu running in recovery mode. The program/script under scrutiny is a proprietary diagnostic that is fairly locked down and of an undetermined type or origin.

What I'm hoping to get from here is, if not a solution, a lead in the right direction. As per suggestions, I'm looking into expect/autoexpect, as well as tput.

UPDATE: So after some more investigation, I have discovered this is, in fact, an executable. I don't know if this clarifies my question, or renders what I am trying to do impractical, I'll let the gurus decide. At the very least, I know I'm completely offtrack trying to feed in input the way I originally attempted.

Upvotes: 1

Views: 1965

Answers (1)

leu
leu

Reputation: 2081

Maybe tput can help. It uses terminal capabilities (look at man terminfo) to send those keycodes that you need. As an example: tput kcub1 sends the keycode for the left-arrow key on your terminal.

Taking this, there are chances that you can achieve your goal via

sh foo.sh <<EOF
0
0
$(tput kcub1)
1
$(tput kcud1)
EOF

(This sends "0 0 [left] 1 [down]" to foo.sh)

Upvotes: 1

Related Questions