File count script not working properly

Hi Experts,

I have this script to count the number of files based on prefix and suffix values.

#!/bin/ksh
file_date=$1
prefix=$2
suffix=$3
file_count=$(ls -l /db/day_file/{$prefix}*${file_date}*{$suffix})

The files in the directory /db/day_file are as below.

20170501
20170501
20170501.txt
20170501.txt
20170501.txt
abc_20170501.txt
abc_20170501
abc_20170502
20170502

Scenario1:If I pass the values as file_date=20170501 and prefix and suffix are empty.The script should give the count 2.
Since it should consider the files are 20170501 and 20170501.
But the script is not working properly.

Scenario2:If I pass the values as file_date=20170501 and prefix=abc_ and suffix is empty.The script should give the count 1.
Since it should consider the file abc_20170501
But the script is not working properly.

Scenario3:If I pass the values as file_date=20170501 and prefix is empty and suffix is .txt.The script should give the count 3.
Since it should consider the file 20170501.txt
But the script is not working properly.

Scenario4:If I pass the values as file_date=20170501 and prefix is abc_ and suffix is .txt.The script should give the count 1.
Since it should consider the file 20170501.txt
in this case the script is working properly.

Please help me.

Thanks in advance.

Check {$prefix} and {$suffix} .

I think you have the "$" in the wrong place...

EDIT:
As for the count you could do something like this:-

count=1
ls /db/day_file/${prefix}*${file_date}*${suffix} | while read line
do
        echo "$count"
        count=$((count+1))
done

EDIT2:
Remember this assumes the filenames do NOT include whitespaces, etc...

Certainly not. NO two files with the same name CAN exist in the same directory

No - again.If suffix is empty, the search pattern is ${prefix}*${file_date}* and the count should be 2.

Certainly not. NO three files with the same name CAN exist in the same directory

Please note that a phrase "But the script is not working properly." doesn't really help people help you as it is way less descriptive than e.g. "the count printed is 14.7, expected was -5" or e.g. an exact error message.

1 Like

Hi Rudic,

Sorry for inadequate information. I am providing proper information now.

#!/bin/ksh
file_date=$1
prefix=$2
suffix=$3
file_count=$(ls -l /db/day_file/${prefix}*${file_date}*${suffix} | wc -l)

The files in the direcotry /db/day_file are as below.

20170501
20170501.txt
abc_20170501.txt
abc_20170501
abc_20170502

Scenario1:If I pass the values as file_date=20170501 and prefix and suffix are empty.
My script is giving the count 4 it should be 1 since only one file with the date 20170501 without prefix and sufix.
Since it should consider the file 20170501.

Scenario2:If I pass the values as file_date=20170501 and prefix=abc_ and suffix is empty.
My script is giving the count 2 it should be 1 since only one file with abc_20170501 without sufix.
Since it should consider the file abc_20170501.

Scenario3:If I pass the values as file_date=20170501 and prefix is empty and suffix is .txt.
My script is giving the count 2 it should be 1 since only one file with 20170501.txt without prefix.
Since it should consider the file 20170501.txt.

Scenario4:If I pass the values as file_date=20170501 and prefix is abc_ and suffix is .txt.
My script is working properly for this scenario. The count is 1. I's considering the file abc_20170501.txt

Could you please provide generic script to work for all scenarios.

Please help me.

Thanks in advance.

The script counts correctly. As said before, look at the pattern when both fixes are empty: *${file_date}* What would you expect with a pattern like that?

Fallen into the same trap.

Fallen into the same trap.

Escaped the trap by coincidence...

1 Like

Hi RudiC...

Good point, I missed that too. Thanks.

@ nalu...

What RudiC is pointing out is your wildcards which I missed it too.

Just think about your prefix and suffix with reference to your wildcards.

Do you put the wildcards inside the prefix and suffix or do you have the wildcards as a separate input say as wildcard="*" and call as ${wildcard} if and when it is needed. You could toggle the wildcard from "" to "*" as you need.

In fact, with the pre- / suffixes and the directory listing as given, no wildcards are needed at all.

True, but we have to assume that there are more complex filenames than the ones given.