Joey Joe Joe
Joey Joe Joe

Reputation:

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

What I'm looking for is something like:

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

But the wildcard (*) causes problems.

I've also tried:

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

Which may work in other shells but doesn't appear to work in Bourne shell (sh).

Thanks for any help!

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

Upvotes: 2

Views: 1575

Answers (5)

William Pursell
William Pursell

Reputation: 212248

Rather than using test -n $(ls filenam*), you might prefer:

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

Upvotes: 2

Rob Wells
Rob Wells

Reputation: 37103

As this is csh, what about:

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

Upvotes: 0

Chen Levy
Chen Levy

Reputation: 16358

I like this:

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

Upvotes: 0

Jonathan
Jonathan

Reputation: 13624

You were on the right track. Try this:

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

That will check whether ls returns anything.

Upvotes: 0

Powerlord
Powerlord

Reputation: 88796

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

ls `glob filenam*`

Upvotes: 0

Related Questions