Unix Shell - Syntax error in nested loop?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > January 2006 > Syntax error in nested loop?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Syntax error in nested loop?
morten.langer@gmail.com

2006-01-22, 6:11 pm

Hi NG,

I'm a total rookie at this csh-writing, so I'd very appreciate some
help from the wizards :o)

I have a simple script which calls an executable a number of times
(changing the calling parameters for each cycle) in a while-loop, like
this:

.... setting some constants first...

while ( $x <= 31 )

# compose date
set date = ${odate}$x

# run program with date as parameter
# call program happens here

# increment day
@ x = $x + 1
end

That works just fine. I now need to nest this loop in another while
loop, cycling through the months as well:

while ($month <= 12)
while ( $x <= 31 )

# compose date
set date = ${odate}$x
set date = ${date }$month

# run program with date as parameter
# call program happens here

# increment day
@ x = $x + 1
end

# increment month
@ month = $month + 1

end

But now I get a syntax error after the last line (i.e. the last end; "
'end of file' unexpected"..? Any ideas?

Thanks in advance!

best,

M.L.

Chris F.A. Johnson

2006-01-22, 6:11 pm

On 2006-01-22, morten.langer@gmail.com wrote:
> Hi NG,
>
> I'm a total rookie at this csh-writing, so I'd very appreciate some
> help from the wizards :o)
>
> I have a simple script which calls an executable a number of times
> (changing the calling parameters for each cycle) in a while-loop, like
> this:
>
> ... setting some constants first...
>
> while ( $x <= 31 )
>
> # compose date
> set date = ${odate}$x
>
> # run program with date as parameter
> # call program happens here
>
> # increment day
> @ x = $x + 1
> end
>
> That works just fine. I now need to nest this loop in another while
> loop, cycling through the months as well:
>
> while ($month <= 12)
> while ( $x <= 31 )
>
> # compose date
> set date = ${odate}$x
> set date = ${date }$month
>
> # run program with date as parameter
> # call program happens here
>
> # increment day
> @ x = $x + 1
> end
>
> # increment month
> @ month = $month + 1
>
> end
>
> But now I get a syntax error after the last line (i.e. the last end; "
> 'end of file' unexpected"..? Any ideas?


Is this csh? It is not recommended for scripting. See:

<http://www.grymoire.com/Unix/CshTop10.txt>
<http://www.grymoire.com/Unix/Csh.html#uh-0>
<http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/>

In a POSIX shell:

month=1
while [ $month -le 12 ]
do
x=1
while [ $x -le 31 ]
do

# compose date
date=${odate}$x
date=${date}$month

# run program with date as parameter
# call program happens here

# increment day
x=$(( $x + 1 ))
done

# increment month
month=$(( $month + 1 ))

done


--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
morten.langer@gmail.com

2006-01-22, 8:58 pm

Hi,

Thanks! I'll give it a try.

Best,

M.L.

Chris F.A. Johnson

2006-01-22, 8:58 pm

On 2006-01-23, morten.langer@gmail.com wrote:
> Hi,
>
> Thanks! I'll give it a try.


You'll give what a try? Please read <http://cfaj.freeshell.org/google>.

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Kenny McCormack

2006-01-23, 7:52 am

I'll try what you suggested.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com