Suppose a sh script (could be ksh, if it helps much) must test whether
there exist one or more files matching a given wildcard spec, which is
in a variable. What would be the best idiom for that? I'm looking
both for something that can become "second nature", just as so many
other sh idioms, so that I can type it in without pause when needing
the same thing in the future in some other script (and for this it
would be best to have something in sheer sh, to be as portable as
possible), and for something as fast as possibile, specifically for
a system where pipes and fork are far costlier than ksh builtins (this
is particularly for MKS Tools, a full ksh for Msdos).
So far I have come up with nothing better than, say:
ls $thevar >/dev/null 2>/usr/tmp/$$
if test -s /usr/tmp/$$
then
echo no files satisfy $thevar
else
echo 1+ files satisfy $thevar
fi
rm /usr/tmp/$$
All in all, not particularly satisfying. I've also though about
checking if the output of "echo $thevar" contains stars or question
marks, but this looks even worse (it might be that the "wildcard"
in $thevar doesn't actually *have* any stars or question marks, it
might be that some of the *files* it matches actually have such [well,
not on Dos, but definitely on Unix], and anyway I can't find a simple
and fast way to test that!). Is it just me, or there *really* is no
way to do that with some clever mix of echo, test and case, which are
builtin and very fast in all target shells? Or some other builtin
which I have totally overlooked (quite likely for ksh, actually, as
I tend to only use its sh subset in scripts)? Thanks for any feedback!
--
Alex Martelli - (home snailmail:) v. Barontini 27, 40138 Bologna, ITALIA
voice phone: (work:) +39 (51) 6130360, (home:) +39 (51) 250434;
fax: +39 (51) 6130294 (work only), Fidonet: 332/407.314 (home only).