[kwlug-disc] Outsmarted by bash && if

Lori Paniak ldpaniak at fourpisolutions.com
Wed Apr 7 22:42:25 EDT 2010


OK, I think I've found the problem.  Here is a test script running on a
machine without a ppp0 interface:


> #!/bin/bash
> 
> STATUS=$( /sbin/ifconfig ppp0 | /bin/grep not )
> echo $STATUS
> 
> if [ -n "$PATH" ] && [ -n "$STATUS" ]; then
> echo "$PATH"
> fi


The output is:
ppp0: error fetching interface information: Device not found


My error is that $STATUS is empty.  I thought the grep was catching the
"not" in "Device not found".  Except that message is an error.
Redirecting the error to STDOUT "fixes" it:


> #!/bin/bash
> 
> STATUS=$( /sbin/ifconfig ppp0 2>&1 | /bin/grep not )
> echo $STATUS
> 
> if [ -n "$PATH" ] && [ -n "$STATUS" ]; then
> echo "$PATH"
> fi
> 

Output:
ppp0: error fetching interface information: Device not found
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games


It also gives an example of why physicists are (in)famous for their
programming skills.  I should have wrote it in FORTRAN!

Thanks for the replies!


On Wed, 2010-04-07 at 21:39 -0400, Lori Paniak wrote:
> 
> On Wed, 2010-04-07 at 21:23 -0400, Robert P. J. Day wrote:
> > On Wed, 7 Apr 2010, Lori Paniak wrote:
> > 
> > > Dear very clever list,
> > >
> > > I am trying to write a simple bash script to (re)start a process given
> > > the logical "and" of two conditions.  I think it should look like this:
> > >
> > > if [ -n "$STICK" ] && [ -n "$STATUS" ]; then
> > > 	      /sbin/ifup ppp0
> > > fi
> > >
> > > When STICK and STATUS are non-empty strings I would like ifup to run.
> > > This works if I just use one condition, but I cannot figure out the
> > > correct syntax for the AND-ed case.  I've tried (seemingly) all
> > > combinations and locations of brackets without success.
> > >
> > > Anyone out there care to point out my trivial error(s)?
> > 
> >   i'm not sure why it *doesn't* work.  if i use hardcoded strings:
> > 
> > $ if [ -n "fred" ] && [ -n "barney" ] ; then
> > > echo yup
> > > fi
> > yup
> > $
> > 
> 
> 
> Is the space before the ";" relevant?  
> 
> 
> _______________________________________________
> kwlug-disc_kwlug.org mailing list
> kwlug-disc_kwlug.org at kwlug.org
> http://astoria.ccjclearline.com/mailman/listinfo/kwlug-disc_kwlug.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
URL: <http://astoria.ccjclearline.com/pipermail/kwlug-disc_kwlug.org/attachments/20100407/9eb92754/attachment-0001.bin>


More information about the kwlug-disc_kwlug.org mailing list