Piping shasum to grep, but grep returning all lines of piped input, even ones that don't match

StackOverflow https://stackoverflow.com/questions/23228209

  •  07-07-2023
  •  | 
  •  

Question

I'm trying to script the download of the node.js source and corresponding SHASUMS256.txt, checksum it, grep for OK, and return no results just exit code 0 on success using grep's -q flag:

wget http://nodejs.org/dist/latest/node-v0.10.26.tar.gz
wget http://nodejs.org/dist/latest/SHASUMS256.txt
sha256sum -c SHASUMS256.txt|grep -q OK

However, grep is returning a selection of the non-matching lines "no such file or directory" errors (though not all, confusingly):

> sha256sum -c SHASUMS256.txt|grep -q OK 
sha256sum: node-v0.10.26-darwin-x64.tar.gz: No such file or directory
sha256sum: node-v0.10.26-darwin-x86.tar.gz: No such file or directory
sha256sum: node-v0.10.26-linux-x64.tar.gz: No such file or directory
sha256sum: node-v0.10.26-linux-x86.tar.gz: No such file or directory
sha256sum: node-v0.10.26-sunos-x64.tar.gz: No such file or directory
sha256sum: node-v0.10.26-sunos-x86.tar.gz: No such file or directory
sha256sum: node-v0.10.26-x86.msi: No such file or directory
sha256sum: node-v0.10.26.pkg: No such file or directory
sha256sum: node.exe: No such file or directory

Any idea what the problem is here? All I want from this script is return code 0 if the checksum succeeds (eg grep matches OK), or return code non-0 if it fails.

Was it helpful?

Solution

  • When you pipe the output of a command as input to other command, only stdout of first command is passed as stdin to the second command.

  • The lines you see are sent by the sha256sum program to stderr.

  • You can verify that by sending stderr of sha256sum command also to grep by

    sha256sum -c SHASUMS256.txt 2>&1 |grep -q OK
    

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top