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
How to check if a file exists using the if statement Jazmania Shell Programming and Scripting 3 09-19-2008 12:32 PM
check if remote file exists hcclnoodles Shell Programming and Scripting 2 08-27-2008 05:53 PM
Check if a file exists with certain prefix raoscb Shell Programming and Scripting 11 08-20-2008 08:13 AM
check if exists a .ZIP file and unzip it using ftp DebianJ Shell Programming and Scripting 1 05-05-2005 04:46 PM
perl ftp check file exists methos Shell Programming and Scripting 2 06-18-2003 08:21 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-23-2009
tonydsam tonydsam is offline
Registered User
  
 

Join Date: May 2004
Posts: 17
Check file exists from a shellscript

Hi,

I have a list of files that I want to check to see if they exist and then count how many of these files exist, I also want to do the same for the files that arent found.

I have done this by creating temp files see below but want ot do this using variables instead:

Code:
for FILE in $FILELIST
do
ls -l $OKAYDIR/$FILE >>countload.txt 2>/dev/null
done

LOADCOUNT=`cat countload.txt|wc -l`
cat countload.txt >>results.txt

echo >>results.txt
echo "$LOADCOUNT files loaded">>results.txt
Thanks.
  #2 (permalink)  
Old 04-23-2009
lavascript lavascript is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 47
I'd do something like this (in ksh):-


Code:
foundc=0
nfoundc=0

for file in $FILELIST
do
   if [[ -s "${file}" ]];then
       # file is found and is > 0 bytes.
       foundc=$(( found + 1 ))
       ...do something else you want...
   else
       # file is not found or is 0 bytes
       nfoundc=$(( nfounc + 1 ))
       ...do something else you want...
   fi
done

print "number of files in [$FILELIST] found = [${foundc}]\n"
print "number of files in [$FILELIST] NOT found = [${nfoundc}]\n"
You can change the -s test for -r (file is readble) -e (file exists) -d (file is directory etc) as you see fit.
  #3 (permalink)  
Old 04-23-2009
tonydsam tonydsam is offline
Registered User
  
 

Join Date: May 2004
Posts: 17
Thanks, how would I go about putting the found files and not found files into other variables which I could then echo out?
  #4 (permalink)  
Old 04-23-2009
lavascript lavascript is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 47
Code:
foundc=0
nfoundc=0
fflist=""
nflist=""

for file in $FILELIST
do
   if [[ -s "${file}" ]];then
       # file is found and is > 0 bytes.
       foundc=$(( found + 1 ))
       fflist="${fflist}\n${file}"
       ...do something else you want...
   else
       # file is not found or is 0 bytes
       nfoundc=$(( nfounc + 1 ))
       nflist="${nflist}\n${file}"
       ...do something else you want...
   fi
done

print "List of found files:\n\n${fflist}\n---------"
print "List of NOT found files:\n\n${nflist}\n-----"

print "number of files in [$FILELIST] found = [${foundc}]\n"
print "number of files in [$FILELIST] NOT found = [${nfoundc}]\n"
I've not tested above so maybe couple of bugs.

You could of course save the outputs to a file instead of a variable.

NOTE: accessing the variable list of files will contain \n whereas printing it out should put each entry on new line.
  #5 (permalink)  
Old 04-23-2009
tonydsam tonydsam is offline
Registered User
  
 

Join Date: May 2004
Posts: 17
Thanks, how would I go about putting the found files and not found files into other variables which I could then echo out?
  #6 (permalink)  
Old 04-23-2009
lavascript lavascript is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 47
I've just answered that with $fflist and $nflist.

They should contain a list of files found and a list of files not found.

Let me know if it doesn't work
  #7 (permalink)  
Old 05-28-2009
supriyabv supriyabv is offline
Registered User
  
 

Join Date: May 2009
Posts: 12
for i in $1
do
if [ ! -f $i ]; then
let countValue=1
fi
done
This is the code i am using and it is not working. where $1 is my txt file name

Thanks
Supriya
Sponsored Links
Reply

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 04:27 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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