File count script not working properly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File count script not working properly
# 1  
Old 05-11-2017
File count script not working properly

Hi Experts,

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

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

Code:
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.
# 2  
Old 05-11-2017
Check {$prefix} and {$suffix} .

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

EDIT:
As for the count you could do something like this:-
Code:
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...

Last edited by wisecracker; 05-11-2017 at 03:34 PM.. Reason: Added EDIT and EDIT2.
# 3  
Old 05-11-2017
Quote:
Originally Posted by nalu
.
.
.
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.
Certainly not. NO two files with the same name CAN exist in the same directory
Quote:
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
No - again.If suffix is empty, the search pattern is ${prefix}*${file_date}* and the count should be 2.

Quote:
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
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.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 05-12-2017
Hi Rudic,

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


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

Code:
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.
# 5  
Old 05-12-2017
Quote:
Originally Posted by nalu
.
.
.
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.
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?

Quote:
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.
Fallen into the same trap.

Quote:
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.
Fallen into the same trap.

Quote:
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
Escaped the trap by coincidence...

Last edited by RudiC; 05-12-2017 at 05:01 AM..
This User Gave Thanks to RudiC For This Post:
# 6  
Old 05-12-2017
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.
# 7  
Old 05-12-2017
In fact, with the pre- / suffixes and the directory listing as given, no wildcards are needed at all.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed script not working properly on Solaris (works fine on AIX)?

Hi, I have a problem with a SED script that works fine on AIX but does not work properly on a Solaris system. The ksh script executes the SED and puts the output in HTML in tables. But the layout of the output in HTML is not shown correctly(no tables, no color). Can anyone tell if there is... (7 Replies)
Discussion started by: Faith111
7 Replies

2. Shell Programming and Scripting

Moving old files bash script - not working properly

I'm trying to write a script that moves data that's older than 2 weeks to a different place. It works well, EXCEPT, that when the script hits a file within a directory inside the working directory, it will move it to the root of the destination directory instead of putting it in the correct... (1 Reply)
Discussion started by: ugolee
1 Replies

3. Shell Programming and Scripting

Shell script not working properly

Hello, i have below shell script to process ftp get from server and create file list afte finish. this shell scipt has 6 parameter input. unfortunately, it is only working to get the file and terminated before creating file list. please help. thanks, #!/bin/ksh ## example usage :... (3 Replies)
Discussion started by: daryatmo
3 Replies

4. Shell Programming and Scripting

script line not working properly

Hi , I am using the below command in script. $_IDLETIME is returning value if i execute the script manually. sar > $_LOCATION/sar.txt _IDLETIME=`tail -2 $_LOCATION/sar.txt | head -1 | tr -s ' ' ' ' | cut -d ' ' -f8 | cut -d '.' -f1`; if But it s not returning any value if it put the... (3 Replies)
Discussion started by: ahamed
3 Replies

5. Shell Programming and Scripting

Shell Script Email not working Properly

Hi GURU's, I'm using a Shell Script to send email's as an attachment. I'm Storing the email address in a table and catching in a variable. MAILLIST=`noarg sqlplus -s $OraUsr << EOF set heading off set feedback off set echo off SELECT email_ids FROM tpemail_table WHERE... (9 Replies)
Discussion started by: karthikraj
9 Replies

6. Shell Programming and Scripting

Script not working..."sort" not working properly....

Hello all, I have a file - 12.txt cat 12.txt =============================================== Number of executions = 2 Total execution time (sec.ms) = 0.009883 Number of executions = 8 Total execution time (sec.ms) = 0.001270 Number of... (23 Replies)
Discussion started by: Rahulpict
23 Replies

7. Shell Programming and Scripting

\n not working properly

Hi all, I'm trying to generate a series of txt files starting from a plain csv file part of my code: #!/bin/ksh INSTALLDIR=/Users/ME/Installdir CSV=CSV.csv TMP=/tmp/$(basename $0).txt tr -s "\r" "\n" < /$INSTALLDIR/$CSV > $TMP function Makefiles { printf '%24s:%30s\n' "sometext"... (1 Reply)
Discussion started by: Jive Spector
1 Replies

8. HP-UX

Cron - Not working properly

Hi, I do have three scripts. Whcih inserts records into a table using sqlldr, creating some reprot files etc. The first script will call the second and then the second will call the third. When I run my first script from the shell prompt, all my operation are completed successfully. If I do... (23 Replies)
Discussion started by: risshanth
23 Replies

9. UNIX for Dummies Questions & Answers

Telnet is not working properly

telnet at my system is behaving stange. Some times I am able to telnet to other machines but sometimes it stop doing that. Then i have to reboot the machine and most of the time (not 100%) it works. SImilar is the case with SSH. Sometime it works , some time it don't. i am new to Unix and I do not... (1 Reply)
Discussion started by: deepak_pathania
1 Replies

10. Programming

y is this not working properly?

#include <stdio.h> #include <sys/types.h> #include <string.h> #include <sys/stat.h> #include <unistd.h> struct stat s; main() { char c; if (fork()==0) { system("clear"); do { printf("myAI\\>§ "); scanf("%s",c); if(stat(c,&s)>-1) {... (3 Replies)
Discussion started by: C|[anti-trust]
3 Replies
Login or Register to Ask a Question