bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Passing the results of GREP into a Bash Variable


From: Philip Lijnzaad
Subject: Re: Passing the results of GREP into a Bash Variable
Date: 11 Jul 2001 17:15:23 +0100

[ note: this is not the right forum, use gnu.utils.help ] 

On Wed, 11 Jul 2001 01:29:36 GMT, 
"Robert" == Robert A Adkins <address@hidden> wrote:


Robert> Hello,
Robert>     I am working on a script that requires the result of a grep to be
Robert> piped or entered into a variable 

a=`grep foo bar` # let's hope that's a short word


Robert> for an if, then, else check. 

why need that? Does following do what you want ? 

  if grep foo bar >/dev/null ; then 
    echo found foo
  else
    did not find foo
  fi

which is the same as

  grep foo bar >/dev/null && echo found foo || echo did not find foo

Unix commands (like grep) have an exit status (echo $?) that is typically 0
if all went fine, non-zero otherwise. The if, while, ||, && etc. commands
actually look at the exit status. 0 counts as 'true', non-zero as 'false' in
this context.  The /dev/null bit is to get rid of the stdout, which is prolly
not too interesting. If you want to get rid of stderr as well, do 

 some-comand >/dev/null 2>&1

Hope this helps,

                                                                      Philip
-- 
If you have a procedure with 10 parameters, you probably missed some. (Kraulis)
-----------------------------------------------------------------------------
Philip Lijnzaad, address@hidden \ European Bioinformatics Institute,rm A2-08
+44 (0)1223 49 4639                 / Wellcome Trust Genome Campus, Hinxton
+44 (0)1223 49 4468 (fax)           \ Cambridgeshire CB10 1SD,  GREAT BRITAIN




reply via email to

[Prev in Thread] Current Thread [Next in Thread]