The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 11:19 AM
How to check the file existence using shell scripting in Solaris-10 krevathi1912 SUN Solaris 2 11-26-2007 05:07 AM
Variable check for existence ? samit_9999 UNIX for Dummies Questions & Answers 2 12-05-2006 05:15 PM
check for FILES existence mpang_ Shell Programming and Scripting 3 06-28-2006 06:51 AM
File existence mpang_ Shell Programming and Scripting 2 03-27-2006 11:27 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-04-2007
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
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.
  #2 (permalink)  
Old 12-04-2007
robsonde robsonde is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 162
if [ -f *myfile ]
then
do stuff
else
do other stuff
fi
  #3 (permalink)  
Old 12-04-2007
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
Hi robsonde,

Thanks. But your codes are not in csh i suppose.
  #4 (permalink)  
Old 12-04-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Code:
if ls *myfile 2>/dev/null
then
    echo some exist
fi
  #5 (permalink)  
Old 12-04-2007
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,209
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
  #6 (permalink)  
Old 12-05-2007
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
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
  #7 (permalink)  
Old 12-05-2007
denn denn is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 96
$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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:08 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0