Csh to check for existence of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Csh to check for existence of file
# 8  
Old 12-05-2007
Hi.

Looking at the man page:
Code:
#!/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:
Code:
% ./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".
# 9  
Old 12-05-2007
Quote:
Originally Posted by Raynon
Code:
% ls *myfile
xxx1_myfile  xxx2_myfile
% ls *myfile|echo $status
0
% Reset tty pgrp from 7873 to 11434

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.
# 10  
Old 12-05-2007
Quote:
Originally Posted by reborg
I'm not really sure why you piped ls into echo $status, but as indicated previous status is the same as $? in borne 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 ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File existence check

hi i wanted to check if the file exist or not(multiple files) DIRE=/home/V478 if ; then echo "file present" else echo "file not present" fi But i am getting the error as : [: unexpected operator/operand (3 Replies)
Discussion started by: ATWC
3 Replies

2. Shell Programming and Scripting

How to check existence of variable in csh

Hi All, I want to check existence of variable, whose name gets decided dynamically. E.g. value of this variable,is derived as $option_"exclude" , where value of option varies depending upon user input. I am trying to do it in a following way : set exclude_var = `echo $option"_exclude"`... (3 Replies)
Discussion started by: Rashmee
3 Replies

3. UNIX for Dummies Questions & Answers

To check for existence of a file

I need to check for the existence of a file *.log in a specific directory using a perl script. Presently am not in that particular directory. So i am using chdir ("/path/to/my/file) And then i am using the -e in an if statement to check if it exists. if (-e $File) {......} $File contains the... (1 Reply)
Discussion started by: manutd
1 Replies

4. Shell Programming and Scripting

command to check existence of file name which has regex

Hi All. Pls help me with the command to check existence of files (I'll mention name of the file as regex) and proceed with my further processing if atleast one of them exists in detail, I've a dir /tmp/TURP, which may or may not have files named with "exter*.txt" I need to check and... (2 Replies)
Discussion started by: skpvalvekar
2 Replies

5. Shell Programming and Scripting

Check for file existence using wildcards

I am using the following command to check for files on a Unix (Solaris 9) and on Linux: if (-r *.) then echo " las file found" else echo " no las file found" endif If no las file is present, the "no las file found" message is displayed. If a las file is present, however, I get... (9 Replies)
Discussion started by: phudgens
9 Replies

6. Shell Programming and Scripting

How to check for file existence?

i want to check if the file is in the directory or not, and also it should be handle error conditions, like missing files and report the error and exit. i did something like this: file ="hello" if !test -e "${file}" then echo "No such files exist!" exit 1 else do something....... fi ... (1 Reply)
Discussion started by: mingming88
1 Replies

7. Shell Programming and Scripting

Find Existence of File with wild card using Csh

Hi All, I would like to find out the existence of files with wild card using CSH. I have used the below code but does not seem to work. Can any expert give me some advice ? set nonomatch set pattern = "_xxx" set filetype = ( *$pattern* ) if ( -e $filetype) then echo... (2 Replies)
Discussion started by: Raynon
2 Replies

8. AIX

Check for File Existence

I have requirement where i need to search for files which start with SALESORDER and PURCHASEORDER. i need to process the files with SALESORDER first and then PURCHASEORDER. If SALESORDER files are not there i dont want to process PURCHASEORDER and i want to come out of script. I have written a code... (4 Replies)
Discussion started by: dsdev_123
4 Replies

9. AIX

check for file existence

Hello I am having a requirement like if there is no file in the directory then i need a message to pop on after the execution of the script. My script basically does for File in `ls -t $DIRECTORY | tail -1`; if there is no file the DIRECTORY then the script is simply exiting with out... (2 Replies)
Discussion started by: dsdev_123
2 Replies

10. AIX

how to check the existence of a file using korn shell?

we have tranferred an ear from local server to remote server using ftp.consider, we have an ear file named a.ear in remote server,again if we transfer the same file named a.ear from local server to remote server.we need the kshell to check the existence of the ear file in remote server,and if the... (3 Replies)
Discussion started by: karthikprasathk
3 Replies
Login or Register to Ask a Question