public inbox for gentoo-user@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-user] OT - Question about conditionals and bash scripting
@ 2006-10-12 20:41 Michael Sullivan
  2006-10-12 20:49 ` Richard Broersma Jr
  2006-10-12 21:17 ` Neil Bothwick
  0 siblings, 2 replies; 8+ messages in thread
From: Michael Sullivan @ 2006-10-12 20:41 UTC (permalink / raw
  To: gentoo-user

I have a short script:

#!/bin/bash

while read LINE
do
   whois $LINE | grep 'abuse' &> /dev/null
   if $? != 0; then
      echo "$LINE" >> noabuse.txt
   fi
done < iplist

I'm getting "command not found" on the if line.  Have I not formatted it
correctly?  The script is supposed to append $LINE to a file if the
return code of whois $LINE | grep 'abuse' returns false (not 0)...

-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [gentoo-user] OT - Question about conditionals and bash scripting
  2006-10-12 20:41 [gentoo-user] OT - Question about conditionals and bash scripting Michael Sullivan
@ 2006-10-12 20:49 ` Richard Broersma Jr
  2006-10-12 21:08   ` Michael Sullivan
  2006-10-12 21:17 ` Neil Bothwick
  1 sibling, 1 reply; 8+ messages in thread
From: Richard Broersma Jr @ 2006-10-12 20:49 UTC (permalink / raw
  To: gentoo-user

> I have a short script:
> 
> #!/bin/bash
> 
> while read LINE
> do
>    whois $LINE | grep 'abuse' &> /dev/null
>    if $? != 0; then
>       echo "$LINE" >> noabuse.txt
>    fi
> done < iplist
> 
> I'm getting "command not found" on the if line.  Have I not formatted it
> correctly?  The script is supposed to append $LINE to a file if the
> return code of whois $LINE | grep 'abuse' returns false (not 0)...
> 

Do you need to add 
   if test... 
or if [ ... ]?

Regards,

Richard Broersma Jr.
-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [gentoo-user] OT - Question about conditionals and bash scripting
  2006-10-12 20:49 ` Richard Broersma Jr
@ 2006-10-12 21:08   ` Michael Sullivan
  2006-10-12 21:37     ` Bo Ørsted Andresen
                       ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Michael Sullivan @ 2006-10-12 21:08 UTC (permalink / raw
  To: gentoo-user

On Thu, 2006-10-12 at 13:49 -0700, Richard Broersma Jr wrote:
> > I have a short script:
> > 
> > #!/bin/bash
> > 
> > while read LINE
> > do
> >    whois $LINE | grep 'abuse' &> /dev/null
> >    if $? != 0; then
> >       echo "$LINE" >> noabuse.txt
> >    fi
> > done < iplist
> > 
> > I'm getting "command not found" on the if line.  Have I not formatted it
> > correctly?  The script is supposed to append $LINE to a file if the
> > return code of whois $LINE | grep 'abuse' returns false (not 0)...
> > 
> 
> Do you need to add 
>    if test... 
> or if [ ... ]?
> 
> Regards,
> 
> Richard Broersma Jr.

OK.  Here's my new code:

#!/bin/bash

while read LINE
do
   whois $LINE | grep 'abuse' &> /dev/null
   if [$? -ne 0]; then
      echo "$LINE" >> noabuse.txt
   fi
done < iplist

Here's the output:

michael@bullet ~/.maildir/.SPAM/cur $ ./process.sh
./process.sh: line 6: [1: command not found
./process.sh: line 6: [1: command not found
./process.sh: line 6: [1: command not found
Interrupted by signal 2...


-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [gentoo-user] OT - Question about conditionals and bash scripting
  2006-10-12 20:41 [gentoo-user] OT - Question about conditionals and bash scripting Michael Sullivan
  2006-10-12 20:49 ` Richard Broersma Jr
@ 2006-10-12 21:17 ` Neil Bothwick
  1 sibling, 0 replies; 8+ messages in thread
From: Neil Bothwick @ 2006-10-12 21:17 UTC (permalink / raw
  To: gentoo-user

[-- Attachment #1: Type: text/plain, Size: 718 bytes --]

On Thu, 12 Oct 2006 15:41:12 -0500, Michael Sullivan wrote:

> while read LINE
> do
>    whois $LINE | grep 'abuse' &> /dev/null
>    if $? != 0; then
>       echo "$LINE" >> noabuse.txt
>    fi
> done < iplist
> 
> I'm getting "command not found" on the if line.

The if line should be "if [ $? -ne 0 ]; then"

Or you can put grep directly into the if statement, since you are only
testing its result anyway

if whois $LINE | grep -q abuse; then


-- 
Neil Bothwick

There are some micro-organisms that exhibit characteristics of both
plants and animals.  When exposed to light they undergo photosynthesis;
and when the lights go out, they turn into animals.  But then again,
don't we all?

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [gentoo-user] OT - Question about conditionals and bash scripting
  2006-10-12 21:08   ` Michael Sullivan
@ 2006-10-12 21:37     ` Bo Ørsted Andresen
  2006-10-12 21:48     ` Richard Broersma Jr
                       ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: Bo Ørsted Andresen @ 2006-10-12 21:37 UTC (permalink / raw
  To: gentoo-user

[-- Attachment #1: Type: text/plain, Size: 223 bytes --]

On Thursday 12 October 2006 23:08, Michael Sullivan wrote:
[SNIP]
> OK.  Here's my new code:
[SNIP]

Maybe you should have a look at:

http://devmanual.gentoo.org/tools-reference/bash/index.html

-- 
Bo Andresen

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [gentoo-user] OT - Question about conditionals and bash scripting
  2006-10-12 21:08   ` Michael Sullivan
  2006-10-12 21:37     ` Bo Ørsted Andresen
@ 2006-10-12 21:48     ` Richard Broersma Jr
  2006-10-12 21:53     ` Brett I. Holcomb
  2006-10-13  5:40     ` alain.didierjean
  3 siblings, 0 replies; 8+ messages in thread
From: Richard Broersma Jr @ 2006-10-12 21:48 UTC (permalink / raw
  To: gentoo-user


> OK.  Here's my new code:
> 
> #!/bin/bash
> 
> while read LINE
> do
>    whois $LINE | grep 'abuse' &> /dev/null
>    if [$? -ne 0]; then
>       echo "$LINE" >> noabuse.txt
>    fi
> done < iplist
> 
> Here's the output:
> 
> michael@bullet ~/.maildir/.SPAM/cur $ ./process.sh
> ./process.sh: line 6: [1: command not found
> ./process.sh: line 6: [1: command not found
> ./process.sh: line 6: [1: command not found
> Interrupted by signal 2...

Check out the section "If statements"

http://www.gentoo.org/doc/en/articles/bash-by-example-p1.xml

Regards,

Richard Broersma Jr.
-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [gentoo-user] OT - Question about conditionals and bash scripting
  2006-10-12 21:08   ` Michael Sullivan
  2006-10-12 21:37     ` Bo Ørsted Andresen
  2006-10-12 21:48     ` Richard Broersma Jr
@ 2006-10-12 21:53     ` Brett I. Holcomb
  2006-10-13  5:40     ` alain.didierjean
  3 siblings, 0 replies; 8+ messages in thread
From: Brett I. Holcomb @ 2006-10-12 21:53 UTC (permalink / raw
  To: gentoo-user

Surround the [ and ] with spaces.

On Thursday October 12 2006 17:08, Michael Sullivan wrote:
> On Thu, 2006-10-12 at 13:49 -0700, Richard Broersma Jr wrote:
> > > I have a short script:
> > >
> > > #!/bin/bash
> > >
> > > while read LINE
> > > do
> > >    whois $LINE | grep 'abuse' &> /dev/null
> > >    if $? != 0; then
> > >       echo "$LINE" >> noabuse.txt
> > >    fi
> > > done < iplist
> > >
> > > I'm getting "command not found" on the if line.  Have I not formatted
> > > it correctly?  The script is supposed to append $LINE to a file if the
> > > return code of whois $LINE | grep 'abuse' returns false (not 0)...
> >
> > Do you need to add
> >    if test...
> > or if [ ... ]?
> >
> > Regards,
> >
> > Richard Broersma Jr.
>
> OK.  Here's my new code:
>
> #!/bin/bash
>
> while read LINE
> do
>    whois $LINE | grep 'abuse' &> /dev/null
>    if [$? -ne 0]; then
>       echo "$LINE" >> noabuse.txt
>    fi
> done < iplist
>
> Here's the output:
>
> michael@bullet ~/.maildir/.SPAM/cur $ ./process.sh
> ./process.sh: line 6: [1: command not found
> ./process.sh: line 6: [1: command not found
> ./process.sh: line 6: [1: command not found
> Interrupted by signal 2...

-- 

Brett I. Holcomb
-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [gentoo-user] OT - Question about conditionals and bash scripting
  2006-10-12 21:08   ` Michael Sullivan
                       ` (2 preceding siblings ...)
  2006-10-12 21:53     ` Brett I. Holcomb
@ 2006-10-13  5:40     ` alain.didierjean
  3 siblings, 0 replies; 8+ messages in thread
From: alain.didierjean @ 2006-10-13  5:40 UTC (permalink / raw
  To: gentoo-user

Selon Michael Sullivan <michael@espersunited.com>:


> #!/bin/bash
>
> while read LINE
> do
>    whois $LINE | grep 'abuse' &> /dev/null
>    if [$? -ne 0]; then
>       echo "$LINE" >> noabuse.txt
>    fi
> done < iplist
>
> Here's the output:
>
> michael@bullet ~/.maildir/.SPAM/cur $ ./process.sh
> ./process.sh: line 6: [1: command not found
> ./process.sh: line 6: [1: command not found
> ./process.sh: line 6: [1: command not found
> Interrupted by signal 2...
>

[ ... ] is a command, equivalent to test. So it has to be followed by an IFS,
usually a <space>. Try
   if [ $? -ne 0 ]; then
See the spaces between [ and $?, 0 and ]. It should work now. Once it done, read
some good doc about bash programming. It's very powerful but the syntax may seem
weird in a few cases.
Happy bash programming !

-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2006-10-13  5:44 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-10-12 20:41 [gentoo-user] OT - Question about conditionals and bash scripting Michael Sullivan
2006-10-12 20:49 ` Richard Broersma Jr
2006-10-12 21:08   ` Michael Sullivan
2006-10-12 21:37     ` Bo Ørsted Andresen
2006-10-12 21:48     ` Richard Broersma Jr
2006-10-12 21:53     ` Brett I. Holcomb
2006-10-13  5:40     ` alain.didierjean
2006-10-12 21:17 ` Neil Bothwick

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox