0

At the moment, I have this in a wrapper script:

2>&1 ./update.sh | ts | tee -a ./update.log

and this in update.sh:

if apt full-upgrade -y | grep linux-headers
then
    echo
    echo Need to Fix the Capture Driver!
fi

(there's some additional logic to fix the driver automatically, but I think you get the idea)

It works, except that I don't get the full output of apt full-upgrade in the terminal or in the logfile. I only get what grep matches.

I could probably tee the full output of apt full-upgrade to a temp file, before the if, and then grep and rm the temp file, but is there a better way than that to get a complete terminal and log, and keep the logic?

1 Answer 1

2

You can't use /dev/stdout here, since that will point to the pipeline. One way might be to take a copy of the script's original stdout, and have tee print into that (I didn't test this):

exec 9>&1    # make fd 9 a copy of the current stdout
apt full-upgrade -y | tee /dev/fd/9 | grep linux-headers
exec 9>&-    # close fd 9

If you wanted the full output to always go to a terminal, you could use /dev/tty:

apt full-upgrade -y | tee /dev/tty | grep linux-headers

but that would make the output go to the terminal regardless of what redirections are applied to the whole script.


In some cases, could also use process substitution to redirect the output to a command in addition to stdout:

apt full-upgrade -y | tee >(grep linux-headers)

but this doesn't really work here since you need the exit status of grep and the process substitution doesn't make it easy to get it.

1
  • I tested it with some "sandbox" scripts (random 0-9, then look for 0 or a to control grep's exit status), and your first method with fd 9 does seem to work. So I guess I'll put it in the "production code". Thank you! Commented May 3, 2024 at 19:01

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.