The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Check existence of a login xavier054 UNIX for Advanced & Expert Users 10 03-06-2008 07:19 AM
How to check the file existence using shell scripting in Solaris-10 krevathi1912 SUN Solaris 2 11-26-2007 01:07 AM
Variable check for existence ? samit_9999 UNIX for Dummies Questions & Answers 2 12-05-2006 01:15 PM
check for FILES existence mpang_ Shell Programming and Scripting 3 06-28-2006 03:51 AM
File existence mpang_ Shell Programming and Scripting 2 03-27-2006 08:27 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-04-2007
Registered User
 

Join Date: Sep 2006
Location: Sg
Posts: 316
Stumble this Post!
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.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 12-04-2007
Registered User
 

Join Date: Dec 2007
Posts: 46
Stumble this Post!
if [ -f *myfile ]
then
do stuff
else
do other stuff
fi
Reply With Quote
  #3 (permalink)  
Old 12-04-2007
Registered User
 

Join Date: Sep 2006
Location: Sg
Posts: 316
Stumble this Post!
Hi robsonde,

Thanks. But your codes are not in csh i suppose.
Reply With Quote
  #4 (permalink)  
Old 12-04-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Stumble this Post!
Code:
if ls *myfile 2>/dev/null
then
    echo some exist
fi
Reply With Quote
  #5 (permalink)  
Old 12-04-2007
reborg's Avatar
Administrator
 
Join Date: Mar 2005
Location: Ireland
Posts: 3,513
Stumble this Post!
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:

Code:
foreach file ( *_myfile )
    echo $file
end
or if you want the action a or action b

Code:
ls *_myfile >& /dev/null
if ( $status == 0 ) then
    foreach file ( *_myfile )
        echo $file
    end
else
    echo "no files"
endif
Reply With Quote
  #6 (permalink)  
Old 12-05-2007
Registered User
 

Join Date: Sep 2006
Location: Sg
Posts: 316
Stumble this Post!
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 ?

Code:
% ls *myfile
xxx1_myfile  xxx2_myfile
% ls *myfile|echo $status
0
% Reset tty pgrp from 7873 to 11434
Reply With Quote
  #7 (permalink)  
Old 12-05-2007
Registered User
 

Join Date: Jul 2007
Posts: 93
Stumble this Post!
$status in csh/tcsh is the same as $? in sh/ksh

The below should work for you, for multiple files.

Code:
#!/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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:05 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0