![]() |
|
|||||||
| Home | Forums | Register | Rules & FAQ | Donate | Members List | 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 here. |
![]() |
|
|
Submit Tools | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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.
|
| Forum Sponsor |
|
|
| Forum Sponsor |
|
|
|
|||
|
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: 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 |
|
|||
|
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: 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 |
|
|||
|
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: Code:
#!/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 check20.sh BARE01_DLY_???_20060803 check20.sh BARE02_DLY_???_20060803 check20.sh BARE01_DLY_???_20060720 etc. |
|
|||
|
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 |
|
||||
|
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 |
|
|||
|
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,
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 |