Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem with nested foreach

138 views
Skip to first unread message

kk_...@yahoo.com

unread,
Feb 7, 2006, 6:02:25 PM2/7/06
to
Hi. I'm programming a simple C Shell script. It has a nested foreach.
I'm finding that if the inner "foreach" has no match, my script is
terminating with a "foreach: no match" message. Is this normal? Is
there a way to continue with the outer loop if the inner loop has no
match?

The script looks like this:

*********

set main_dir = `pwd`
foreach sub_dir (`find . type d -name UNIT_TEST -print`)

cd $sub_dir

foreach file (*.ut)
echo $file
end

cd $main_dir

end

echo I am done!!!!
*********

Eventually, this will get more meaty, but first I wanted to find out
how to handle this situation.

Thanks!

Ken

Ed Morton

unread,
Feb 7, 2006, 6:06:27 PM2/7/06
to
kk_...@yahoo.com wrote:

> Hi. I'm programming a simple C Shell script.

You might want to take a look at question 17 in the FAQ
(http://home.comcast.net/~j.p.h/cus-faq-2.html#17) before continuing to
write csh scripts.

Ed.

Message has been deleted

Bill Marcum

unread,
Feb 7, 2006, 7:42:22 PM2/7/06
to
On 7 Feb 2006 15:02:25 -0800, kk_...@yahoo.com
<kk_...@yahoo.com> wrote:
> Hi. I'm programming a simple C Shell script. It has a nested foreach.
> I'm finding that if the inner "foreach" has no match, my script is
> terminating with a "foreach: no match" message. Is this normal? Is
> there a way to continue with the outer loop if the inner loop has no
> match?
>
set nonomatch
If there is no match, the loop will be executed once, with the variable
equal to the wildcard pattern.

--
A possum must be himself, and being himself he is honest.
-- Walt Kelly

Bruce Barnett

unread,
Feb 7, 2006, 8:15:38 PM2/7/06
to
kk_...@yahoo.com writes:

> Hi. I'm programming a simple C Shell script.

Danger, Will Robinson! I've been there. The more you program in the C
shell, the more headaches you will have. That's why I wrote

http://www.grymoire.com/Unix/CshTop10.txt

Given that ....

> It has a nested foreach.
> I'm finding that if the inner "foreach" has no match, my script is
> terminating with a "foreach: no match" message. Is this normal?

Yes. You can set the special variable
set nonomatch

and you will not get an error, but ...

> foreach file (*.ut)
> echo $file
> end

In this case, file will be "*.ut"
You have to test for this condition.

I think there was other work-arounds, but it's been years since I had
to deal with them.


--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.

0 new messages