How do I check for the existence of a file using a template in the Bourne shell?

What I'm looking for is something like:

if [ -f "filenam*" ]; then
   ...
fi

      

But the (*) pattern is causing problems.

I've also tried:

if [ `ls filenam* > /dev/null 2>&1` ]; then
   ...
fi

      

Which might work in other shells, but doesn't seem to work in the Bourne (sh) shell.

Thanks for any help!

EDIT: Sorry, but not the C shell, the Bourne (sh) shell.

+2


source to share


5 answers


Instead of using, test -n $(ls filenam*)

you might prefer:



if ls filenam* 2> /dev/null | grep . > /dev/null; then
   ...
fi

      

+2


source


Since this is C based, you probably need the glob command:



ls `glob filenam*`

      

0


source


You were on the right track. Try the following:

if [ ! -z `ls filenam*` ] ; then
    ...
fi

      

This will check if it returns anything.

0


source


How is csh, and what:

foreach i (`ls -d filenam*`)
    ...
end

      

0


source


I like it:

function first() { echo "$1" ; }
[ -e $(first filenam*) ] && echo "yes" || echo "no"

      

0


source







All Articles