[comp.unix.questions] foreach: No match

jwm@SUN4.JHUAPL.EDU (James W. Meritt) (04/17/91)

	I have a script that I run in the background.  It checks a
directory and processes files in it.  It works nice.  That's the
good news.

	The irritating thing is that when there isn't anything in the
directory, I get the subject error message on the console.  I don't
WANT an error message.  How do I stop this?

Opinions expressed are solely those of the author, and do not necessarily
represent those opinions of this or any other organization.  The facts,
however, simply are and do not "belong" to anyone.
    jwm@sun4.jhuapl.edu or jwm@aplcen.apl.jhu.edu or meritt%aplvm.BITNET

jik@athena.mit.edu (Jonathan I. Kamens) (04/17/91)

In article <9104161842.AA13587@tea-jwm.jhuapl.edu>, jwm@SUN4.JHUAPL.EDU (James W. Meritt) writes:
|> 	The irritating thing is that when there isn't anything in the
|> directory, I get the subject error message on the console.  I don't
|> WANT an error message.  How do I stop this?

From csh(1):

     nonomatch      If set, it is not an error for a filename
                    expansion to not match any existing files;
                    rather the primitive pattern is returned.  It
                    is still an error for the primitive pattern
                    to be malformed, i.e.  `echo [' still gives
                    an error.

For example:

	% mkdir empty
	% cd empty
	% set files = *
	set: No match.
	% set nonomatch
	% set files = *
	% echo $files
	*
	%

You use this in your script like this:

	set nonomatch
	set files = *
	unset nonomatch
	if ("$files" == "*") then
		# Whatever you would do if there are no files to process, like
		# perhaps exiting
	else
		foreach file ($files)
			# the body of your foreach
		end
	endif

I think the idea is clear.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710