ksh: Checking for file existence

ksh: Checking for file existence

Post by Ross Druk » Sun, 16 Jun 1991 00:18:42



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.

 
 
 

ksh: Checking for file existence

Post by Ross Druk » Sun, 16 Jun 1991 00:51:09


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.

 
 
 

ksh: Checking for file existence

Post by Leslie Mikese » Sun, 16 Jun 1991 12:12:52



>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

 
 
 

ksh: Checking for file existence

Post by 228-4197,AT » Tue, 18 Jun 1991 21:06:41



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


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

 
 
 

ksh: Checking for file existence

Post by Rob McMah » Thu, 20 Jun 1991 18:12:25



Quote:(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

Rob McMahon, Computing Services, Warwick University, Coventry CV4 7AL, England

 
 
 

1. Script to checking a file existence then send an email to myself.

Please help me.
I need to create a script that will check if xxx file is exists in
/abc folder, if not, then send me a warning email to myself.  Thanks
in advance.  I think this should be easy but some how it's not
working.

db2 # more /u01/scripts/henry.sh
#!/bin/sh
#Henry's scripts for checking xxx.lk file.
HENRY=/u11/henry
DATE=`date`
i=`ls $HENRY |grep xxx.lk`

if [ "$i" -e "xxx.lk" ]
  then
        echo "trash" >> /dev/null
  else

  fi

db2 #

2. Help regarding printing from a Dbase exe program to print

3. how to check existence of multiple files in if-clause ?

4. Is this tough or am I just stupid??? <<< Gurus please read >>>

5. (t)csh file existence check with wildcards

6. Font Setup & Backspace

7. need help on checking existence of file using autoconf

8. IBM 4019 laser printer

9. sh: How check for wildcarded files existence (e.g., *.gif)

10. check file's existence in tclsh

11. How do I check the existence of a file programmatically in C?

12. newbie: check the existence of a file

13. Checking for file existence and killing a process