How can I automate a bash script that is 'read'-ing from /dev/tty?
I have a script (that I cannot modify) that I must run regularly that has the following construct:
read -r response < /dev/tty
if [ "$response" = 'y' ]; then
...
I want to wrap this script such that I always send a "y" character to it. I've tried running the script with the standard tricks for sending a yes:
echo y | '/bin/bash /path/to/script'
and
yes | ./myscript
and
printf "y\n" | ./myscript
and
/bin/bash /path/to/script < /path/to/y-file
None of these work. I also tried expect.
It's not clear if Mac OS X' built-in expect is working; with expect diagnostic information enabled, expect is properly detecting to the prompt and responding, but the script then terminates.
#!/bin/bash
set timeout -1
SCRIPT="~/myscript"
expect -d <<EOF
spawn $SCRIPT
expect "prompt string"
send "y\r"
EOF
Also, to clarify, this is on Mac OS X Monterey.
I'd appreciate any help. Again, I cannot modify the original script.
2 answers
-
answered 2022-01-25 17:37
ceving
You can use
socat
to fake a new pseudo terminal for a child process.Let
tty.sh
be the following script:#! /bin/bash read -r response < /dev/tty if [ "$response" = 'y' ]; then echo yes else echo no fi
Then you can connect stdin to the new pty of the child process this way:
echo y | socat stdio exec:./tty.sh,pty,setsid,echo=0
-
answered 2022-01-25 18:38
phranz
You could try to use autoexpect to generate the wrapper (autoscript) script for you:
autoexpect -f autoscript ./myscript.sh
do you know?
how many words do you know