[gentoo-user] OT - Question about conditionals and bash scripting

2006-10-12 Thread Michael Sullivan
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



Re: [gentoo-user] OT - Question about conditionals and bash scripting

2006-10-12 Thread Richard Broersma Jr
 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



Re: [gentoo-user] OT - Question about conditionals and bash scripting

2006-10-12 Thread Michael Sullivan
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:

[EMAIL PROTECTED] ~/.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



Re: [gentoo-user] OT - Question about conditionals and bash scripting

2006-10-12 Thread Neil Bothwick
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?


signature.asc
Description: PGP signature


Re: [gentoo-user] OT - Question about conditionals and bash scripting

2006-10-12 Thread 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:
 
 [EMAIL PROTECTED] ~/.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



Re: [gentoo-user] OT - Question about conditionals and bash scripting

2006-10-12 Thread Brett I. Holcomb
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:

 [EMAIL PROTECTED] ~/.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



Re: [gentoo-user] OT - Question about conditionals and bash scripting

2006-10-12 Thread Bo Ørsted Andresen
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


pgpxpDTTnsL1r.pgp
Description: PGP signature


Re: [gentoo-user] OT - Question about conditionals and bash scripting

2006-10-12 Thread alain . didierjean
Selon Michael Sullivan [EMAIL PROTECTED]:


 #!/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:

 [EMAIL PROTECTED] ~/.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