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
Counting files in one directory matrixtlm UNIX for Dummies Questions & Answers 10 06-15-2007 07:14 PM
Counting number of files in a directory iamalex UNIX for Dummies Questions & Answers 2 09-05-2005 10:13 AM
List files that do not match the search pattern olapxpert UNIX for Dummies Questions & Answers 7 04-14-2005 03:49 PM
List files that do not match the search pattern olapxpert IP Networking 1 04-14-2005 02:37 PM
rm files in a directory, looping, counting, then exit JporterFDX Shell Programming and Scripting 6 07-18-2002 08:56 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-20-2006
dsravan dsravan is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 188
Counting files in a directory that match a pattern

I have 20 files in a direcotry like BARE01_DLY_MKT_YYYYMMDD. The MKT differes for all these files but the remaining syntax remains the same for a particular day. If I am checking for today I need to make sure that there are 20 files that start with BARE01_DLY_MKT_20060720. How can I write a shell script to do this? I think I have to use for loop but don't know how to check for today or not. Kindly help.
  #2 (permalink)  
Old 07-20-2006
Glenn Arndt's Avatar
Glenn Arndt Glenn Arndt is offline Forum Advisor  
Anomalous Lurker
  
 

Join Date: Feb 2006
Location: Indianapolis, IN
Posts: 255
Using ksh:
Code:
integer filecount=0
for file in BARE01_DLY_???_$(date +%Y%m%d); do
  filecount=$filecount+1
done
echo $filecount
Your test could look something like:
Code:
if (( $filecount == 20 )); then
  print "True"
else
  print "False"
fi

Last edited by Glenn Arndt; 07-20-2006 at 12:06 PM.. Reason: Didn't include an example test
  #3 (permalink)  
Old 07-20-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
You can also do something like this :

Code:
integer filecount=$(ls BARE01_DLY_???_$(date +%Y%m%d) 2>/dev/null | wc -l)
Jean-Pierre.
  #4 (permalink)  
Old 07-20-2006
dsravan dsravan is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 188
Pierre,

Do you mean i have to have the code like this:

integer filecount=0
for file in BARE01_DLY_???_$(date +%Y%m%d); do
filecount=$filecount+1
done
echo $filecount

if (( $filecount == 20 )); then
print "True"
else
print "False"
fi

is this right? How can i embed them in the single KSH script. Please let me know.
  #5 (permalink)  
Old 07-20-2006
Glenn Arndt's Avatar
Glenn Arndt Glenn Arndt is offline Forum Advisor  
Anomalous Lurker
  
 

Join Date: Feb 2006
Location: Indianapolis, IN
Posts: 255
I don't know what you expect as output. Without knowing all your requirements (and without having a desire to write the entire script for you), I would do:

check20.sh:
Code:
#!/bin/ksh
integer filecount=0
for file in BARE01_DLY_???_$(date +%Y%m%d); do
  filecount=$filecount+1
done

if (( $filecount == 20 )); then
  exit 0 # exit with success
else
  exit 1 # exit with failure
fi
  #6 (permalink)  
Old 07-20-2006
dsravan dsravan is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 188
Glenn,

I am sorry if I had asked to mean to write a script. But I created the script with the statements. What I need to know is I need to pass the name of the file as a parameter to the script and then do the processing we had before. How can this be possible.

The name of the files will look like BARE01_DLY_MKT_yyyymmdd for 20 markets. This script need to work for files named BARE02_DLY_MKT_20060803
which will be 20 again. So I need to generalise the script and pass the name of the file as parameter. So how can acheive this. Please let me know. I am trying to learn and develop at the same time.

Once again I appreicate your knid response.
  #7 (permalink)  
Old 07-20-2006
Glenn Arndt's Avatar
Glenn Arndt Glenn Arndt is offline Forum Advisor  
Anomalous Lurker
  
 

Join Date: Feb 2006
Location: Indianapolis, IN
Posts: 255
You're checking to see if 20 files named using the form, "BARE01_DLY_MKT_yyyymmdd" (where "MKT" is variable between all 20 files). How would passing one file name to the script help? Unless you mean to pass the date to the script so that it checks for that date? That is, today's date is 20060720, but you want to check for 20 files with the date 20060719...?

check20.sh 20060803:
Code:
#!/bin/ksh
mydate=$1
integer filecount=0
for file in BARE01_DLY_???_$mydate; do
  filecount=$filecount+1
done

if (( $filecount == 20 )); then
  exit 0
else
  exit 1
fi
Note that there is no error checking here. You can feed anything you want as the date.
Sponsored Links
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 03:42 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