![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
|
||||
|
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"
|
|
||||
|
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"
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. |
|
||||
|
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 | ||
|
|