Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Do/Done help please

Status
Not open for further replies.

Trancemission

Technical User
Oct 16, 2001
108
GB
I am trying to create a simple script in UNIX that will search a file for a specific line, go 2 lines forward and capture the next 2 lines. (the file is a whois query result. I am currently doing a few test scripts and coming accross problems using the Do/Done looping structure

My Script:

#!/usr/bin/sh
i=0
for line in `cat windsor-life.co.uk`
do
i=`expr $i + 1`
if [ $line -eq "*COMMERCE*" ] then
echo "Match! $line"
end if
done

The error is get is

syntax error at line 10: `done' unexpected

Any help gratefully recieevd

Cheers Trancemission
=============
If it's logical, it'll work!
 
Your "end if" line shoud be "fi".
 
Hi:

In addition to the end if problem, you need to either place the then on a separate line of place a semi-colon before it:


if [ $line -eq "*COMMERCE*" ]
then
echo "Match! $line"
fi
or

if [ $line -eq "*COMMERCE*" ]; then
echo "Match! $line"
fi

Regards,

Ed
 
Now I get:

syntax error at line 8: `fi' unexpected

Doesn't expect much does it :)



Trancemission
=============
If it's logical, it'll work!
 
Can't you replace this whole section with something like
grep COMMERCE windsor-life.co.uk | wc -l ??? This will give you a count of line with COMMERCE on them.

Otherwise I would guess that $line is at some point evaluated to blanks or null and the if would look like this
if [ -eq "*COMMERCE"] Which would be a problem.
 
Thanks for everybodies help. I have got the do/done loop sorted.

regarding the grep idea, how do I then retrieve the data from 2 lines below the line found in the grep statement?.

Many thanks again for everyones input.




Trancemission
=============
If it's logical, it'll work!
 
That's a good point!

Make sure that you are putting your $line variable inside double-quotes. And remember that since $line is a string you need to use the = not -eq for testing equality. Like this:

Code:
#!/usr/bin/sh
i=0
for line in `cat windsor-life.co.uk`; do
      i=`expr $i + 1`
      if [ "$line" = "*COMMERCE*" ]; then
          echo "Match! $line"
      fi
done

Einstein47
(Love is like PI - natural, irrational, endless, and very important.)
 
Glad you got it working.
You might want to check out this snippet for future scripts.

awk 'BEGIN{count=0} /COMMERCE/ { print $0;count=count+1 }END{print count}' windsor-life.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top