views:

128

answers:

5

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).

A: 

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

ls `glob filenam*`
R. Bemrose
A: 

You were on the right track. Try this:

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

That will check whether ls returns anything.

Jonathan
This isn't csh syntax
Rob Wells
if [ ! -z `ls filenam* > /dev/null 2> then ... gives me the error "test: argument expected". Any ideas?
Rather than ! -z, you can use -n
William Pursell
Same error with -n
I thought you were using a normal shell. I didn't realize how broken csh is. I tried using a tutorial to figure this out, and I still couldn't get it to work using any logical construct. There does not seem to be a way to execute ls and look at its output.
Jonathan
A: 

G'day,

As this is csh, what about

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

HTH

cheers,

Rob Wells
+1  A: 

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

if ls filenam* 2> /dev/null | grep . > /dev/null; then
   ...
fi
William Pursell
Perfect! Thanks William.
A: 

I like this:

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