how do I guard against "foreach: No match"?

csh, shell, tcsh

Solution

Here are two ways to handle this.

Set the shell's `nonomatch` variable. This tells tcsh to leave a pattern unchanged if it doesn't match any files. Thus:

% set nonomatch=1
% echo i686.* amd64.*
i686.* amd64.*

You will then need to make your commands handle the case of a non-matched pattern.

Pass an extra pattern that always matches. Example:

% echo i686.* amd64.* /dev/nul[l]
/dev/null

You will then need to make your commands ignore the file matched by the always-matched pattern. This is probably easier than #1.

Problem

I've got a shell script that looks like this: ``` foreach dir (i686.* amd64.*) # commands end ``` The problem with this script is that if there are no directories matching those names then I get the following error: ``` "foreach: No match." ``` What's an elegant way to guard against this error?

Original source