Why for loop is acting weird


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why for loop is acting weird
# 1  
Old 09-13-2006
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 ))
# 2  
Old 09-13-2006
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

# 3  
Old 09-13-2006
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

# 4  
Old 09-13-2006
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
# 5  
Old 09-13-2006
check this variable NEW_DT contains any space. if you have still problem show us how did you get the value for that variable
# 6  
Old 09-13-2006
Thanks Anbu, the NEW_DT is having space in it. It solved the problem.

Thanks,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read command acting erratically

I have been trying to use read in a script with issues so I tried some things on the command line. $ echo "testing 123" | read x ; echo $xand $ echo "testing 123" | read -r x ; echo $xare only producing any output after being invoked the first time after rebooting the machine. I also got into... (14 Replies)
Discussion started by: Michael Stora
14 Replies

2. Shell Programming and Scripting

For loop with dashes in filenames causing weird output

Hi again, What i'm trying to accomplish here is search a large directory for certain filesames, read from a txt file and looping through it. For instance, one of my target names from the loop file is: 1ad55f47-c342-496b-a46d-ba7de0f1b434 My loop is constructed thusly, run in a directory... (2 Replies)
Discussion started by: Karunamon
2 Replies

3. Shell Programming and Scripting

BASH weird acting: unquoted parameter accepted as quoted one !

In one session I have strange behavior of the bash-shell: SDX-Q> echo ">$ss<" #this is just to present the $ss var: > lll kkk < SDX-Q> # more obviose: SDX-Q> od -cb <<<"$ss" 0000000 l l l k k k \n 040 040 040 154 154 154 040... (3 Replies)
Discussion started by: alex_5161
3 Replies

4. UNIX for Dummies Questions & Answers

Acting on results from a grep command

Hi, I am currently reading a tar file and searching for a particular word using grep e.g. Plane. At the moment, if a sentence is found with the word "Plane" the sentence itself is piped to another file. Here is the code i am using; for jar in 'cat jar_file.tar'; do tar -tvf... (3 Replies)
Discussion started by: crunchie
3 Replies

5. IP Networking

DHCP server also acting as relay

Hi. I want to use the DHCP server that comes with vxWorks 6.4. The DHCP server implementation has a table that contains addresses of DHCP servers that will receive a copy of all the client requests that the local server gets, thus the server acts as a dhcp relay at the same time. Can anyone... (4 Replies)
Discussion started by: tomwolf
4 Replies

6. Shell Programming and Scripting

Weird loop break,- please Help

Hi all, im doing this script in which i read from a logfile line by line, my problem is this: The script was working fine until i added this statement to SSH into another machine to look for some data, it enters and retrieves the data just fine, but for some strange reason after it goes thru the... (1 Reply)
Discussion started by: sx3v1l_1n51de
1 Replies

7. Shell Programming and Scripting

script acting weird..

Hi Guys, I have this script which is being called from another script, sh +x SCRIPTNAME. Now this script is failing saying the source file is missing. But i was able to see the source file was present. It was renamed and but somehow the source file is removed. There is no remove command in the... (1 Reply)
Discussion started by: mac4rfree
1 Replies

8. Shell Programming and Scripting

bash-function with array acting bizarre, bug?

Hello, basically what this script is supposed to do is showing a list of hosts that is given a number, that you will be able to choose from a list. A check is made to verify that the chosen number is within the array and this is where things go bad and I don't know why, bizarre. I've spent... (5 Replies)
Discussion started by: gand
5 Replies

9. Shell Programming and Scripting

Array in loop is acting up

Hello! I have a question about loops and arrays. I'm trying to go through this: for aa in 01 02 03 OrigNum=$(grep ${Orig} Ba3In2F12.prepos | wc -l) OrigNum=$((${OrigNum} - 1)) echo ${OrigNum} etc It gets stuck on the second line. The error reads: ./asdf: line 30:... (5 Replies)
Discussion started by: RisingSun
5 Replies

10. Solaris

Passwd Changing Acting Strange

Hello. I'm getting very odd behavior when I try to change a user's password in Solaris 8. The shell, where it used to ask for a new passwd and a confirmation, now asks FOUR times, with two success message. This is what happens every time: # passwd myusername New Password: xxxxxxxx New... (2 Replies)
Discussion started by: rockusa
2 Replies
Login or Register to Ask a Question