Csh to check for existence of file

Hi,

I would like to check the existence of files (doesn;t matter the number of files) in a directory.
My file is named in the following manner (always ending with " myfile "). Can anybody give me some guidance?

EG:
abc1_myfile
sdfr_myfile
sffgd_myfile
and so on ......

My intention is to perform the following:

if file exist, perform plan A.
else perform plan B.

if [ -f *myfile ]
then
do stuff
else
do other stuff
fi

Hi robsonde,

Thanks. But your codes are not in csh i suppose.

if ls *myfile 2>/dev/null
then
    echo some exist
fi

I'm a bit rusty on csh, since I never use it but if I remember correctly from when we had a bunch of users to support who did use it, the syntax would be something like:

foreach file ( *_myfile )
    echo $file
end

or if you want the action a or action b

ls *_myfile >& /dev/null
if ( $status == 0 ) then
    foreach file ( *_myfile )
        echo $file
    end
else
    echo "no files"
endif

hi reborg,

I tried the " $status " in a directory containing *myfile.
And these statement appear " Reset tty pgrp from 7873 to 11434 ". What does this statement mean ?
Is " $status " a special variable in csh ?

% ls *myfile
xxx1_myfile  xxx2_myfile
% ls *myfile|echo $status
0
% Reset tty pgrp from 7873 to 11434

$status in csh/tcsh is the same as $? in sh/ksh

The below should work for you, for multiple files.

#!/bin/csh

foreach file ( $* )
ls $file 2>&1 /dev/null
        if ( $status == 0 ) then
                echo $file
        else
            echo "no files"
        endif
end

to test: ./scriptname file1 file2 file3

Hi.

Looking at the man page:

#!/usr/bin/env csh

# @(#) s1       Demonstrate file existence test in csh.

echo
echo "(Versions displayed with local utility version)"
sh -c "version >/dev/null 2>&1" && version tcsh
echo

rm -f t1 t2
touch t1 t2

if ( -e t1 ) then
  echo " File t1 exists."
else
  echo " File t1 does not exist."
endif

rm t2
if ( -e t2 ) then
  echo " File t2 exists."
else
  echo " File t2 does not exist."
endif

exit 0

Producing:

% ./s1

(Versions displayed with local utility version)
tcsh 6.13.00

 File t1 exists.
 File t2 does not exist.

Adjust as necessary for your situation, see man page for details (this is done with csh as a link to tcsh) ... cheers, drl

_____
Obligatory scripting advice: "use Bourne family, not csh family, for scripting".

I'm not really sure why you piped ls into echo $status, but as indicated previous status is the same as $? in bourne based shells.

Hi Reborg,

But what's your purpose of this statement " ls *_myfile >& /dev/null " before the statement " if ( $status == 0 ) then ". My code " ls *myfile|echo $status " was emulating this.

I tried to perform "echo $status" in a directory with files and in a directory without files, the output is always " 0 ". So what does $status really mean ?