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.

Using ksh:

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:

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

You can also do something like this :

integer filecount=$(ls BARE01_DLY_???_$(date +%Y%m%d) 2>/dev/null | wc -l)

Jean-Pierre.

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.

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:

#!/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

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.

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:

#!/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.

Glenn,

You made a very good point. But here there will be 20 files for BARE01_DLY_MKT_YYYYMMDD alone and 20 files for BARE02_DLY_MKT_20060803 alone and like that with some more names. So that is why I need to pass BARE01_DLY_MKT_YYYYMMDD as the parameter so that it checks whether that named file has 20 files or not and similarly for other file names. Please remember that MKT is the only that changes in the file. So please help.

It's really the same concept... just pass the entire pattern instead of just the date.

check20.sh:

#!/bin/ksh
myfilepattern=$1
integer filecount=0
for file in $myfilepattern; do
  filecount=$filecount+1
done

if (( $filecount == 20 )); then
  exit 0
else
  exit 1
fi

Syntax:

check20.sh BARE01_DLY_???_20060803
check20.sh BARE02_DLY_???_20060803
check20.sh BARE01_DLY_???_20060720
etc.

Glenn,

I tried this and the code is not returning error if the file count doesn't match the required count. Please suggest.

Woops! Use $@ instead of $1.

#!/bin/ksh
myfilepattern=$@
integer filecount=0
for file in $myfilepattern; do
  filecount=$filecount+1
done

if (( $filecount == 20 )); then
  exit 0
else
  exit 1
fi

Glenn,

I ran the code and i am using set -x to debug the code and surprised by the way code is taking. I have created one file named BARE01_DLY_MKT_20060803 in the directory /biddf/ab6498/dev/ctl direcotry and the script should show an error but please see below for the debug statments from the script;

script2.ksh BARE01_DLY_MKT_20060803
+ myfilepattern=BARE01_DLY_MKT_20060803
+ typeset -i filecount=0
+ filecount=0+1
+ let 1 == 10
+ exit 1

It is taking as 1=10 and exit ing from the code with out througing an error. Below is the script I modified.

#!/bin/ksh
dir=/biddf/ab6498/dev/ctl
export dir
set -x
myfilepattern=$@
integer filecount=0
for file in $myfilepattern; do
filecount=$filecount+1
done

if (( $filecount == 10 )); then
exit 0
else
exit 1
fi

Please suugest

Glenn,

I don't need to pass the whole name of the file but just BARE01_DLY would suffice. But it has to check whether 20 files are present for that day or not?

Please suggest.

The "let 1 == 10" line is not setting 1 equal to 10. It is comparing 1 (the number of files it counted) and 10 (the number of files needed for success). Because 1 != 10, the script exits status 1, which is an error. The unix convention is that "0" is success, and anything else is failure.

Glenn,

If I need to through out an error , how should I set it Iam doing like this but it's not throughing error.

#!/bin/ksh

dir=/biddf/ab6498/dev/ctl
export dir

# set -x

myfilepattern=$@
integer filecount=0
for file in $myfilepattern; do
filecount=$filecount+1
done

if (( $filecount == 10 )); then
exit 0
else
exit 1 echo failure
fi

If you want to be verbose:

if (( $filecount == 10 )); then
  print "success"
  exit 0
else
  print "failure"
  exit 1
fi

Glenn,

Thank you very much it's throughing an error now. But if I don't need to pass the whole name of the file but just BARE01_DLY.

some thing like this: check20.sh BARE01_DLY

But it has to check whether 20 files are present for that day or not? I am using the code below. It's not working. What's wrong in this?

#!/bin/ksh
myfilepattern=$@
integer filecount=0
for file in $myfilepattern_$(date +%Y%m%d); do
filecount=$filecount+1
done

if (( $filecount == 20 )); then
exit 0
else
exit 1
fi

Please suggest

Glenn,

I am getting the following error when I run the debug mode.

myfilepattern=BARE01_DLY
+ typeset -i filecount=0
./script3.ksh[7]: syntax error at line 9 : `(' unexpected

Glenn,

The code I modifed like this;

#!/bin/ksh

dir=/biddf/ab6498/dev/ctl
export dir
set -x
myfilepattern=$@
integer filecount=0
for file in $myfilepattern_???_(date +%Y%m%d); do
filecount=$filecount+1
done

if (( $filecount == 10 )); then
print "success"
exit 0
else
print "failure"
exit 1
fi

Please suggest

Glenn,

I removed the braces in between date field but i am surprised how the value of filecount is 2 here. See the debug output once I remove the braces between date field in the script.

myfilepattern=BARE01_DLY
+ typeset -i filecount=0
+ filecount=0+1
+ filecount=1+1
+ let 2 == 10
+ print failure
failure
+ exit 1

Here is the script;

#!/bin/ksh

dir=/biddf/ab6498/dev/ctl
export dir
set -x
myfilepattern=$@
integer filecount=0
for file in $myfilepattern_???_date +%Y%m%d ; do
filecount=$filecount+1
done

if (( $filecount == 10 )); then
print "success"
exit 0
else
print "failure"
exit 1
fi