I'm not sure what your goal is, but trying to get in between the terminal and an interactive command can be somewhat tricky. A tool that's designed to do this is script(1), from util-linux.
Let's say you have a bridge.sh
containing:
#!/bin/sh
read -p "What… is your name? " NAME
read -p "What… is your quest? " QUEST
read -p "What… is your favourite colour? " COLOUR
echo "Go on. Off you go."
You can capture the terminal session with:
script -c ./bridge.sh bridge_script
Since you're only interested in the user's input, you can use:
script -c ./bridge.sh --log-in bridge_input_script
The manual page warns:
script places everything in the log file, including linefeeds and backspaces. This is not what the naive user expects.
And indeed, you can see in the input log that there's a bare CR at the end of each of my inputs. (Which makes sense; I hit ⏎, it logged CR.)
> cat -A bridge_input_script
Script started on 2023-05-24 09:46:19-04:00 [TERM="xterm-256color" TTY="/dev/pts/4" COLUMNS="124" LINES="40"]$
My name is Sir Lancelot of Camelot.^MTo seek the Holy Grail.^MBlue.^M$
Script done on 2023-05-24 09:46:32-04:00 [COMMAND_EXIT_CODE="0"]$
(This is really the best it can do without knowing more about the command under instrumentation.)
This is what I usually do, to make sure I have all the data I might need for future analysis:
script -c ./bridge.sh --log-timing bridge_timing_log --log-io bridge_io_log
And I use scriptreplay(1) play back the session:
scriptreplay --log-io bridge_io_log --log-timing bridge_timing_log -m 0.01
(Since I don't care about the timing much, I pass -m 0.01
to cap the maximum delay at 10ms.)
script
has a few more tricks up its sleeve, including scriptlive(1), which actually re-runs your session from the logs. (You can really shoot yourself in the foot with this. RTFM and be careful!)
tee stdin_file | script
? (Instead ofscript
you could also have multiple commands enclosed in curly braces or parentheses. Depending on your script you might get buffering issues if the input is not a terminal but a pipe.script
automatically. You got an answer that usesscript
, so maybe you will find the linked answer useful.