ksh: Checking for file existence

228-4197,ATTT rbr at bonnie.ATT.COM
Mon Jun 17 22:06:41 AEST 1991


In article <91165.101842RS0VRD at ROHVM1.BITNET> RS0VRD at 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 at 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}.



More information about the Comp.unix.shell mailing list