The UNIX and Linux Forums  

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 here. Shell Script Page.

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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #16 (permalink)  
Old 07-20-2006
Glenn Arndt's Avatar
Anomalous Lurker
 

Join Date: Feb 2006
Location: Indianapolis, IN
Posts: 255
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
If you want to be verbose:

Code:
if (( $filecount == 10 )); then
  print "success"
  exit 0
else
  print "failure"
  exit 1
fi
Reply With Quote
Forum Sponsor
  #17 (permalink)  
Old 07-20-2006
Registered User
 

Join Date: Jul 2006
Posts: 180
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
  #18 (permalink)  
Old 07-20-2006
Registered User
 

Join Date: Jul 2006
Posts: 180
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
  #19 (permalink)  
Old 07-20-2006
Registered User
 

Join Date: Jul 2006
Posts: 180
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
  #20 (permalink)  
Old 07-20-2006
Registered User
 

Join Date: Jul 2006
Posts: 180
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
  #21 (permalink)  
Old 07-20-2006
Registered User
 

Join Date: Jul 2006
Posts: 180
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Debug doesn't match script logic

I have a script like this below. I am calling the script like this script1.ksh BARE01_DLY The script looks in the directory for named files BARE01_DLY_???_YYYYMMDD. The YYYYMMDD signify todays date when the script ran. It checks whether they are 10 files with that pattern for today and if they are then it shows succes otherwise failure. But in the directory I have only one file with name BARE01_DLY_MKT_20060720. But after I run the script in debug mode the output shows like it has 2 files with that name. Please see belwo for output.

myfilepattern=BARE01_DLY
+ typeset -i filecount=0
+ filecount=0+1
+ filecount=1+1
+ let 2 == 10
+ print failure
failure
+ exit 1
Here it shows 2==10 but I have only one file for that day. So where am i going wrong. Please suggest. Below is the script.
Code:
#!/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

Last edited by reborg; 07-20-2006 at 05:27 PM.
Reply With Quote
  #22 (permalink)  
Old 07-20-2006
reborg's Avatar
Administrator
 
Join Date: Mar 2005
Location: Ireland
Posts: 3,450
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
threads merged, please do not start multiple threads for the same topic.
Reply With Quote
  #23 (permalink)  
Old 07-20-2006
Registered User
 

Join Date: Jul 2006
Posts: 180
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Reborg,

I am sorry for that!!! I got no answer so thought that new thread may incerase people viewing.

Anyway can you help me with this please?
Reply With Quote
  #24 (permalink)  
Old 07-20-2006
reborg's Avatar
Administrator
 
Join Date: Mar 2005
Location: Ireland
Posts: 3,450
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Actually that exactly the reason that it is againts the rules of these forums.

However try this:

Code:
#!/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
Reply With Quote
  #25 (permalink)  
Old 07-20-2006
Registered User
 

Join Date: Jul 2006
Posts: 180
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Reborg,

I bow to you completely. You are great. It's working great man.

I will learn from this thread a lot. I will be active member from now on.

Once again thank you very much.
Reply With Quote
  #26 (permalink)  
Old 07-24-2006
Registered User
 

Join Date: Jul 2006
Posts: 180
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Hi,

I have a problem with the script below. Basically it checks whether there is any file present with the name CARE01_DLY present for today's date in the directory /biddf/ab6498/dev/ctl and if the count =1 it says success otherwise error. But it doesn't seem to take the todays date file but checking if the file is present and saying success if the file is present with out checking for todays date. But i want the script to check for todays file only and if found it should through success otherwise error. The name of the file is sent as an argument to the script.

Quote:
#!/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
here is the output: The script is called like this script3.ksh BARE02_DLY

Quote:
+ dir=/biddf/ab6498/dev/ctl
+ export dir
+ set -x
+ myfilepattern=BARE02_DLY
+ typeset -i filecount=0
+ date +%Y%m%d
+ filecount=0+1
+ let 1 == 1
+ print success
success
+ exit 0
There is no file with this name for today but it's saying success.

Please help.
Reply With Quote
  #27 (permalink)  
Old 07-24-2006
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,073
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
I found two problem in your script

$myfilepattern_???_$(date +%Y%m%d) is interpreted by the shell as ${myfilepattern_}???_$(date +%Y%m%d).
You must code like this : ${myfilepattern}_???_$(date +%Y%m%d)


The for statement returns the pattern itself if no file are found.
You must test for that pattern.

Code:
#!/bin/ksh
dir=/biddf/ab6498/dev/ctl
export dir
set -x 
myfilepattern=$@
fullpattern=${my_filepattern}_???_$(date +%Y%m%d)
integer filecount=0
for file in $fullpattern ; do
   [ "$file" != "$fullpattern"] && filecount=$filecount+1
done

if (( $filecount == 10 )); then
print "success"
exit 0
else
print "failure"
exit 1
fi
Jean-Pierre.
Reply With Quote
  #28 (permalink)  
Old 07-24-2006
Registered User
 

Join Date: Jul 2006
Posts: 180
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Jean,

Thanks for the changes. But the script is still producing wrong result. There are 2 files with todays date in the directory and still it says failure. I think the main problem is pattern matching is is complaring like this:

CARE01_DLY_AUS_20060724 != CARE01_DLY_???_20060724 and saying no match. So please suggest.

Please see output:

Quote:
+ dev=/biddf/ab6498/dev/ctl
+ cd /biddf/ab6498/dev/ctl
+ echo /biddf/ab6498/dev/ctl
/biddf/ab6498/dev/ctl
+ myfilepattern=CARE01_DLY
+ + date +%Y%m%d
fullpattern=CARE01_DLY_???_20060724
+ typeset -i filecount=0
+ [ CARE01_DLY_AUS_20060724 != CARE01_DLY_???_20060724]
./script4.ksh[11]: test: ] missing
+ [ CARE01_DLY_MKT_20060724 != CARE01_DLY_???_20060724]
./script4.ksh[11]: test: ] missing
+ let 0 == 1
+ print failure
failure
+ exit 1
Reply With Quote
  #29 (permalink)  
Old 07-24-2006
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,073
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Quote:
+ dev=/biddf/ab6498/dev/ctl
+ cd /biddf/ab6498/dev/ctl
+ echo /biddf/ab6498/dev/ctl
/biddf/ab6498/dev/ctl
+ myfilepattern=CARE01_DLY
+ + date +%Y%m%d
fullpattern=CARE01_DLY_???_20060724
+ typeset -i filecount=0
+ [ CARE01_DLY_AUS_20060724 != CARE01_DLY_???_20060724]
./script4.ksh[11]: test: ] missing
+ [ CARE01_DLY_MKT_20060724 != CARE01_DLY_???_20060724]
./script4.ksh[11]: test: ] missing
+ let 0 == 1
+ print failure
failure
+ exit 1
Sorry, type error
Add a space befor the ] in the test statement whithin the for loop



Code:
#!/bin/ksh
dir=/biddf/ab6498/dev/ctl
export dir
set -x 
myfilepattern=$@
fullpattern=${my_filepattern}_???_$(date +%Y%m%d)
integer filecount=0
for file in $fullpattern ; do
   [ "$file" != "$fullpattern" ] && filecount=$filecount+1
done

if (( $filecount == 10 )); then
print "success"
exit 0
else
print "failure"
exit 1
fi
Reply With Quote
  #30 (permalink)  
Old 07-24-2006
Glenn Arndt's Avatar
Anomalous Lurker
 

Join Date: Feb 2006
Location: Indianapolis, IN
Posts: 255
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
You need a space between the " and the ] in:
Code:
[ "$file" != "$fullpattern"]
Perhaps it would be easier for everyone if you familiarized yourself with the syntax of the shell you are using, rather than going back and forth dozens of times about tiny syntax problems. I'm not trying to be rude, just thinking that spooning tiny little pieces to you is less helpful than your taking the time to understand the general concepts of the environment in which you are programming. I've found that many of the experts here tend to give general solutions as a starting point to help you understand your specific problems, which is much more productive than debugging the same script multiple times.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes
Linear Mode Linear Mode
Threaded Mode