unix shell, getting exit code with piped child

后端 未结 5 473
南旧
南旧 2021-01-18 07:21

Let\'s say I do this in a unix shell

$ some-script.sh | grep mytext

$ echo $?

this will give me the exit code of grep

5条回答
  •  -上瘾入骨i
    2021-01-18 08:15

    There are multiple solutions, it depends on what you want to do exactly.

    The easiest and understandable way would be to send the output to a file, then grep for it after saving the exit code:

    tmpfile=$(mktemp)
    ./some-script.sh > $tmpfile
    retval=$?
    grep mytext $tmpfile
    rm tmpfile
    

提交回复
热议问题