I'm running a bash script expecting occasional user input to continue processing the script. Is it possible to redirect the script output to a file while keeping the terminal output and keeping the application interactive, i.e. responding to my key presses?
2 Answers
tee
is designed for this purpose. I wrote the script sc
:
##!/bin/bash
while [[ $x != "X" ]] ; do
printf "? "
read x
echo "User wrote: $x"
done
then ran it as follows:
$ ./sc | tee f
? The first line
User wrote: The first line
? another line
User wrote: another line
? X
User wrote: X
$ cat f
? User wrote: The first line
? User wrote: another line
? User wrote: X
Pretty rough and ready, but it shows how to tee
off to a file.
You can run the command under script
, which will capture its entire output in the file typescript
for you. See man script
for other options
script
your_command...
Use Ctrl/D to exit the capture session