In bash, I want to be able to parse stdin more or less "out of range" when copying to stdout without moving it through tmp files, variables, or explicitly called fifos.
Two similar examples:
while read foo; do somefunc $foo; echo "$foo"; done
tee >(grep -qs bar && do_something_but_i_am_trapped_in_a_process_substitution_shell)
line-by-line will not be the end of the world, but I would prefer something cleaner.
What I would like to do is something like: exec, redirecting file descriptors and tee, so that I can do something like:
hasABar=$(grep -qs bar <file descriptor magic> && echo yes || echo no)
... then do something based on whether I have a bar, but in the end, stdout is still a copy of stdin.
UPDATE: from Kugelman’s answer below, I adapted to the following: both work.
(
exec 3>&1
myVar=$(tee /dev/fd/3 | grep -qs bar && echo yes || echo no)
echo "$myVar" > /tmp/out1
)
source
share