yPhil
yPhil

Reputation: 8367

grep output of a command sometimes works.. Sometimes not?

please consider the following:

$ cd ~/.emacs.d/lisp/tabbar
$ git pull
Already up-to-date.

$ git pull | grep -q "Already" ; echo $?
0

Still with me? Now:

$ cd ~/src/emacs-tmp/trunk
$ bzr up                           
Tree is up to date at revision 108837 of branch /home/px/src/emacs-tmp/trunk

$ bzr up | grep -q "Tree" ; echo $?     
Tree is up to date at revision 108837 of branch /home/px/src/emacs-tmp/trunk
1

Questions :

Upvotes: 0

Views: 279

Answers (3)

Christian Stieber
Christian Stieber

Reputation: 12496

  • Why is the return code of grep "0" (found) in the 1st case and "1" (not found) in the 2nd ?

Probably because in the first case the output is on stdout, and in the second case its on stderr. You can add a "2>&1" to the commands to throw everything to stadout.

  • Why is the output of the first (git pull) command hidden when grepping for it ?

Because you said "-q", which tells grep to shut up :-)

Upvotes: 1

lynxlynxlynx
lynxlynxlynx

Reputation: 1433

Looks like bzr is outputting to the standard error device (/dev/stderr), while grep is only inspecting the standard input. You can confirm or deny this guess by trying to redirect stderr to stdin:

bzr up 2>&1 | grep -q "Tree" ; echo $?   

Upvotes: 1

LSerni
LSerni

Reputation: 57408

I think that your "visible" command is output'ting to stderr, not stdout. grep only looks in stdout, unless you redirect with 2>&1.

Therefore, try

bzr up 2>&1 | grep -q "Tree" ; echo $?

Upvotes: 1

Related Questions