Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if test -e command doesn't work!

Status
Not open for further replies.

jvbpro

Programmer
May 27, 2002
9
CA
Please anyone help me here.

I have the follwoing command in my scrip:

if test -e c:/dir/file*.dat
then
echo whatever
fi

this is giving me some systax error on test! and I know for sure that I have files in that directory that meet that criteria. I checked it with na 'ls c:/dir/file*.dat' and it retruns me a wack of them.

How do I test if there exist any file that match that combination?

Any help is appreciated,
thank you in advance
Jeya.
 
file="c:/dir/file*.dat"

if [ -f $file ] && [ -s $file ]
then
echo stuff
else
echo other stuff
fi <hr>
There's no present like the time, they say. - Henry's Cat.
 
Hi KerveR, thank you for your help.... but...

I tried your code but it is NOT working.
However, when I use -f to test for &quot;one&quot; file in particular, then it works. The script breaks when I try to test more than one file using wildcard &quot;*&quot;. I was wondering if we could use -f or -e to test more than one file in a particular directory.

Please help.
Jeya
 
Try this:

#!/usr/bin/sh

LIST=`ls -1 file*.dat`

for file_name in $LIST
do
if [ -f $file_name -a -s $file_name ]; then
echo &quot;MATCH&quot;
else
echo &quot;NONMATCH&quot;
fi
done

Hope this gives you something to think about. The best teacher is yourself; therefore, I only give you ideas to think about.
 
It appears you want to check only if at least one file exists. Use this to avoid matching each file name. It would echo only once:

ANS=`find C:/dir -name 'file*.dat'`
if [ &quot;$ANS&quot; ];then
echo whatever
fi
 
Hello !

The find command of eagertotry will find file*.dat files in directory C:/dir and in any subdirectory.

If you just want to check for entries matching pattern 'file*.dat' in directory 'C:/dir' just do:

Code:
pattern=&quot;C:/dir/file*.dat&quot;
if [ &quot;`echo $pattern`&quot; != &quot;$pattern&quot; ]; then
    echo matching files found
fi

How does this work ?
The shell does file pattern expansion of the argument of echo but not on double quoted variables. If no entry matching the pattern exists, the file pattern expansion just let the pattern unchanged.

Note:
This will find any entry matching the pattern, even directory (but who will give a directory a name starting with file ?).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top