I am executing:
Command1 | tee >(grep sth) || Command2
I want Command2 to be executed based on the exit status of grep, while in the current configuration it is being executed based on the result of tee.
As far as I know pipefail and pipestatus are not working here (please correct me if I am wrong).
Modification to the Origian question based on Alexej Answer
I also tried Command1 | tee >(grep sth || Command2)
, which works for my original question, but as I am trying to set the status of my test in the subshell; ex, Command 1 | tee>(grep sth || Result="PASSED")
and later have access to the Result in other chunks of my code. So I still have problem.
Thanks
Change your script to:
Command1 | tee >(grep sth || Command2)
to achieve the desired result.
>(....)
is a subshell. Anything and everything that you do within that subshell (except for the exit status of said subshell) is completely isolated from the outside world: (a=1); echo $a
will never echo the number 1
, because a
only has meaning within the subshell where it is defined.
I don't entirely understand why, but when you do redirection to a subshell, it seems to invert the exit status of that subshell, so that a failure will return true
and a success will return false
.
echo 'a' >(grep 'b') && echo false
# false
(exit 1) || echo false
# false
So if my first suggestion isn't working out for you, then try re-writing your script thusly:
Command1 | tee >(grep sth) && Command2
a=1 # `a` now equals `1`
# if I run `exit`, $a will go out of scope and the terminal I'm in might exit
(exit) # $a doesn't go out of scope because `exit` was run from within a subshell.
echo $a # $a still equals `1`
Set a parent shell's variable from a subshell
Pass variable from a child to parent in KSH
Variables value gets lost in subshell
http://www.tldp.org/LDP/abs/html/subshells.html
http://mywiki.wooledge.org/SubShell
http://wiki.bash-hackers.org/syntax/expansion/proc_subst