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.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Passwd Changing Acting Strange rockusa SUN Solaris 2 12-10-2007 05:41 PM
HP-UX weird FTP ? IngmarMeins HP-UX 10 11-29-2005 05:22 PM
weird core nkshirsagar HP-UX 1 10-05-2005 11:33 PM
weird stuff woofie Windows & DOS: Issues & Discussions 4 11-16-2004 04:41 PM
Weird SSH issue AKM UNIX for Advanced & Expert Users 6 11-08-2004 11:13 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-13-2006
Registered User
 

Join Date: Jul 2006
Posts: 183
Why for loop is acting weird

Hey i have a small script in which i check if a file with that pattern exists or not. If present then i go ahead with further processing.

In the present situation i have only one file with that name and for loop is reading twice. Here is the script. And the output of debug mode. Please help.
Quote:
myfilepattern=$1_$2
fullpattern=${myfilepattern}_$NEW_DT.dat
integer filecount=0
for file in $fullpattern ; do
[ "$file" != "$fullpattern" ] && filecount=$filecount+1
done

if (( $filecount == 1 )); then

I pass 2 parameters to the script.

and the debug output shows it;s reading 2 times in the for loop.

Quote:
files=AUD01_CARE_DLY_20060912 .dat
+ myfilepattern=AUD01_CARE_DLY
+ fullpattern=AUD01_CARE_DLY_20060912 .dat
+ typeset -i filecount=0
+ [ AUD01_CARE_DLY_20060912 != AUD01_CARE_DLY_20060912 .dat ]
+ filecount=0+1
+ [ .dat != AUD01_CARE_DLY_20060912 .dat ]
+ filecount=1+1
+ (( 2 == 1 ))
Reply With Quote
Forum Sponsor
  #2  
Old 09-13-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,698
Notice the whitespace in
fullpattern=AUD01_CARE_DLY_20060912 .dat

You need to get rid of that space or alternatively use
Code:
for file in "$fullpattern" ; do
[ "$file" != "$fullpattern" ] && filecount=$filecount+1
done
Reply With Quote
  #3  
Old 09-13-2006
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
AUD01_CARE_DLY_20060912[SPACE].dat
So in for loop it will take as two arguments. If you want to treat as single argument then do this

Code:
for file in "$fullpattern" ; do
Reply With Quote
  #4  
Old 09-13-2006
Registered User
 

Join Date: Jul 2006
Posts: 183
Thanks friends,

I got it working but i don't understand how the spaces are coming as my code doesn't have any spaces in it.

Quote:
myfilepattern=$1_$2
fullpattern=${myfilepattern}_$NEW_DT.dat
integer filecount=0
for file in $fullpattern ; do
[ "$file" != "$fullpattern" ] && filecount=$filecount+1
done

if (( $filecount == 1 )); then
Please observer that there is no space in fullpattern variable here. Please suggest
Reply With Quote
  #5  
Old 09-13-2006
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
check this variable NEW_DT contains any space. if you have still problem show us how did you get the value for that variable
Reply With Quote
  #6  
Old 09-13-2006
Registered User
 

Join Date: Jul 2006
Posts: 183
Thanks Anbu, the NEW_DT is having space in it. It solved the problem.

Thanks,
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:33 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0