I'm trying to save my entered commands and their respective outputs in a file.
What I'm currently doing:
mylog() {
echo "----------------" >> ${PWD}/cmds.log
echo "$@" >> ${PWD}/cmds.log
# read foo # Tried reading the piped value in to
# echo "$foo" >> ${PWD}/cmds.log
# MYCMD="${@}"
# echo "$MYCMD" >> ${PWD}/cmds.log
"$@" | tee -a ${PWD}/cmds.log
}
It currently functions like: mylog cat file.txt | mylog sort
And the output in cmds.log
is:
----------------
cat file.txt
----------------
sort
....
output from cat
...
output from sort
...
What I'd like is:
----------------
cat file.txt
....
output from cat
...
----------------
sort
...
output from sort
...
In the code, you can see I tried using read and following other method described here.
I also tried doing VAR=${@}
but this just saved the command twice and doesn't execute it.
Any idea of how I can accomplish my goal?
Slightly related, I'm manually saving the terminals output like this, and not using script
because the interactive shell causes lots of escape characters and such to be caught.