How to get stdout to console and pipe to next command at the same time?
I've tried using the Read command as suggested here which worked to get the output of Grep from a Tail of a log file to a variable and then to a log or email, but I'd still like to get the output to stdout console as well: https://unix.stackexchange.com/a/365222/346155
I've tried using Tee as here: https://unix.stackexchange.com/a/47936/346155
I'm using the --line-buffered flag just in case from here: https://stackoverflow.com/a/7162898/4240654
I may be missing something simple about the sdtin logic, but the case from the first link suggests that Bash may not have this simple capability. And that variables cannot read from a subshell.
The fact that echo 'hello' | echo $(</dev/stdin)
works, suggests it might be possible. Another way to look at it is, how can I stdout to console within each pipe segment. That should help to debug a long chain of commands, before committing it to a bash script.
EDITS:
Something like echo 'hello' | echo $(</dev/stdin) >/dev/stout
or echo 'hello' | tee >/dev/stdout | echo 2nd $(</dev/stdin)
, the latter should output 'hello' twice, but only does so once.
tee
? – Arkadiusz Drabczyk Mar 25 '20 at 17:12echo hello | tee >(grep -o h)
? – Arkadiusz Drabczyk Mar 25 '20 at 17:28(tail log) | grep "keyword" | tee>*OutToConsole* | (read var | echo var | mail )
so I can see the alert line also before it emails.....YES, that was what I was missing.. Grep in a subshell does stsout to console Thanks! .. for the record, so does Echo. – alchemy Mar 25 '20 at 17:32tail log | grep "keyword" | tee >(mail)
? – Arkadiusz Drabczyk Mar 25 '20 at 17:37generateOutput | tee >(grep "keyword") | thenDoOtherStuff
works. The linked answer regarding Tee I think was too specific to abstract to a general case. – alchemy Mar 25 '20 at 17:40grep
, check:echo stuff | tee >(grep -o s) | wc -c
– Arkadiusz Drabczyk Mar 25 '20 at 18:04>/dev/stdout
-- redirect stdout to stdout. – ctrl-alt-delor Mar 25 '20 at 18:08echo 'hello' | tee >(echo hello) | echo 2nd $(</dev/stdin)
gives 2 hellos, but in a different order. Grep in place of Echo does the same. I think using Tee as with Kamil's answer is probably the most direct. – alchemy Mar 26 '20 at 03:09