$?
is the overall or final exit status. What you need to look at is a bash array named:
PIPESTATUS
That gives you status of individual piped command.
So for you case:
read str < <(grep g09 tor.sh | cut -d ' ' -f2`)
echo ${PIPESTATUS[0]}
1
Here 1
means failure of first grep command in pipeline.
PS: Please note that we're using process substitution, instead of command substitution to be able to set PIPESTATUS
correctly in current shell.
Example:
read str < <(grep 'bar' <<< 'foo bar baz' | cut -d ' ' -f2)
echo ${PIPESTATUS[0]}
0
read str < <(grep 'cat' <<< 'foo bar baz' | cut -d ' ' -f2)
echo ${PIPESTATUS[0]}
1
In the first example grep
runs successfully hence we get ${PIPESTATUS[0]}
as 0 but 1
in 2nd example when grep
fails to find anything.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…