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'
yes | ./myscript
printf "y\n" | ./myscript
/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.
You can use
socatto fake a new pseudo terminal for a child process.
tty.shbe 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
You could try to use autoexpect to generate the wrapper (autoscript) script for you:
autoexpect -f autoscript ./myscript.sh