[comp.unix.shell] ksh: Checking for file existence

Ross Druker <RS0VRD@ROHVM1.BITNET> (06/14/91)

Of course, after posting this, I walked away for 20 minutes, then
came back and thought of a simple way of doing this.  Forgetting
about the "test" command, doing

if ls *.data > /dev/null 2>&1

returns true if any files exist and redirects standard out and standard
error to null.

But I'm curious as to whether there's any other way.  Sorry for the
wasted time if not.

RS0VRD@ROHVM1.BITNET (Ross Druker) (06/14/91)

I have what may be a trivial problem, but I haven't solved it yet.
I'm using the Korn shell a script on an HP-UX system.  I would like
to check for the existence of ANY data files, not a specific file.
I was trying to use the "test" command.  This HP-UX does NOT have the
[[...]] operator, even though this was supposedly available after
1986 versions of ksh, :-(

I would like to do:

if test -r  *.data

But ksh barks back with a syntax error.  test doesn't like wildcards.
I've been trying to get around this, playing with quotes, etc.  The
last thing I tried was to assign the list of filenames to a variable,
as in:

filelist=*.data

What I've discovered is that ksh does NOT assign the corresponding
string to filelist.  In the csh world you'd get a wordlist.  For
instance, in csh, if there were files a.data and b.data,

echo $filelist AND echo "$filelist" both return:
a.data b.data

But in ksh, echo $filelist returns:
a.data b.data

and echo "$filelist" returns:
*.data

The filelist variable never takes on the value of the filenames really.
I was heading this way to possibly try and extract the first filename
from the variable and see if I could use that somehow.  But then I
ran into this.

Sorry to be so verbose, but does anyone have the answer that I'm too
blind to see?


Ross Druker
Rohm and Haas Co.
rs0vrd@rohmhaas.com

les@chinet.chi.il.us (Leslie Mikesell) (06/15/91)

In article <91165.101842RS0VRD@ROHVM1.BITNET> RS0VRD@ROHVM1.BITNET (Ross Druker) writes:
>I have what may be a trivial problem, but I haven't solved it yet.
>I'm using the Korn shell a script on an HP-UX system.  I would like
>to check for the existence of ANY data files, not a specific file.
>if test -r  *.data

This should work with sh or ksh:
FILES=`echo *.data`
if [ "*.data" = "$FILES" ]
  then :  # no match
else
  for i in $FILES
   do
  ... process them...
   done
fi

Les Mikesell
  les@chinet.chi.il.us

rbr@bonnie.ATT.COM (228-4197,ATTT) (06/17/91)

In article <91165.101842RS0VRD@ROHVM1.BITNET> RS0VRD@ROHVM1.BITNET (Ross Druker) writes:
>I have what may be a trivial problem, but I haven't solved it yet.
>I'm using the Korn shell a script on an HP-UX system.  I would like
>to check for the existence of ANY data files, not a specific file.
>I was trying to use the "test" command.  This HP-UX does NOT have the
>[[...]] operator, even though this was supposedly available after
>1986 versions of ksh, :-(
>
>I would like to do:
>
>if test -r  *.data
>
>But ksh barks back with a syntax error.  test doesn't like wildcards.
>I've been trying to get around this, playing with quotes, etc.  The
>last thing I tried was to assign the list of filenames to a variable,
>as in:
>
>filelist=*.data
>
>What I've discovered is that ksh does NOT assign the corresponding
>string to filelist.  In the csh world you'd get a wordlist.  For
>instance, in csh, if there were files a.data and b.data,
>
>echo $filelist AND echo "$filelist" both return:
>a.data b.data
>
>But in ksh, echo $filelist returns:
>a.data b.data
>
>and echo "$filelist" returns:
>*.data
>
>The filelist variable never takes on the value of the filenames really.
>I was heading this way to possibly try and extract the first filename
>from the variable and see if I could use that somehow.  But then I
>ran into this.
>
>Sorry to be so verbose, but does anyone have the answer that I'm too
>blind to see?
>
>
>Ross Druker
>Rohm and Haas Co.
>rs0vrd@rohmhaas.com


Try something like:

if test -r `ls *.data`

or:

filelist=`ls *.data`
for FN in `echo $filelist`
do
	if test -r "$FN" ; then
		<do something>
	fi
done

Bob Rager

Ain't no place like ${HOME}.

cudcv@warwick.ac.uk (Rob McMahon) (06/19/91)

In article <1991Jun15.031252.6351@chinet.chi.il.us> les@chinet.chi.il.us
(Leslie Mikesell) writes: 
>This should work with sh or ksh:
>FILES=`echo *.data`
>if [ "*.data" = "$FILES" ]
>  then :  # no match
>else
>  for i in $FILES
>   do
>  ... process them...
>   done
>fi

... unless you've got a file called `*.data', or a file with a space in the
name ... how about 

set - *.data
if [ "$*" = "*.data" -a ! -f "*.data" ]
then
  : no files
else
  for i
  do
    ... process them ...
  done
fi

Yuck,

Rob
-- 
UUCP:   ...!mcsun!ukc!warwick!cudcv	PHONE:  +44 203 523037
JANET:  cudcv@uk.ac.warwick             INET:   cudcv@warwick.ac.uk
Rob McMahon, Computing Services, Warwick University, Coventry CV4 7AL, England