I want to send output of a shell script, including user-entered text, to the terminal and a logfile.
I thought some combination of
exec might do it, but I’ve had no luck so far. I know
tee by itself can echo and capture what the user enters in the terminal:
$ tee logfile Hello (I entered this at runtime) Hello (I entered this at runtime) ^C $ cat logfile Hello (I entered this at runtime)
But I need to see (on both terminal and in the logfile) what the user enters in response to commands invoked within the shell script.
tee doesn’t seem to be able to do that consistently.
$ read message 2>&1 | tee logfile Hello (I entered this at runtime) $ cat logfile
Nothing was captured there. I expected to see
Hello (I entered this at runtime) in the file just like before.
I also tried combining
exec in the shell script like so:
$ cat test.bash #!bin/bash # Note: in this simplified version of this file, I’m not looking at $1, $2, or anything else passed in, but will need to eventually rm -f logfile exec &> >(tee -a logfile) echo “Say \”Hello\”” 2>&1 read -p “> “ 2>&1
exec did not help:
$ ./test.bash Say “Hello” > Hello (I entered this at runtime) $ cat logfile Say “Hello” >
As you can see, it captured the output of the
echo command and the
read command, but not what I entered into the terminal in response to the
Is there a way to do it?
I know the
script command (“make typescript of terminal session”) can capture everything on the screen and put it in a logfile. But the
script command can’t be invoked in a useful way from within a shell script. (Can it?)
script needs to be invoked first, and then the user has to invoke the desired shell script. But I want the user to only have to invoke one command, with its parameters, and then have the command take care of running everything else and logging everything.
Then there’s all that “extra” information (e.g. color codes, backspaces) script captures that makes it hard to read the resulting logfile in an arbitrary text editor.
I just want to see the “human-readable” characters in the logfile. And I don’t want to see if the user corrected a spelling error. I just want to see that they had “Hello” on the screen when they finished editing and hit Enter. Although I suppose the extra information could be stripped out after capture.