Loop counter resets by itself


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop counter resets by itself
# 1  
Old 05-20-2015
Loop counter resets by itself

Hi!

Can anyone explain this? The counter CDR_count should go on forever, but it suddenly resets at every step of the FOR loop (I know this because $_file has exactly 378 records). However, the counter reset is OUTSIDE the FOR loop. What's going on??

Code:
#!/bin/bash

if [ "$1" = "" ] || [ "$2" = "" ]; then
   echo
   echo "USAGE: ./CCN_CDR_parser.sh <CCN CDR files - path & wildcard allowed> <MSISDN>"
   echo
   exit
fi

echo
echo "Searching CDRs for MSISDN $2 in:"
echo "$1"
echo

file_count=0
file_total=`ls -1 $1 | wc -l | tr -d " "`
CDR_count=0
CDR_match=0
echo > CDR.tmp

for _file in `ls -1 $1`
do
  echo -ne "Files processed: $file_count / $file_total       CDRs match : $CDR_match / $CDR_count"\\r
  file_count=`expr $file_count + 1`

     cat $_file | while read _line
     do

             if [ "$_line" = "CDRCCN.ChargingDataOutputRecord.onlineCreditControlRecord" ]; then

                CDR_count=`expr $CDR_count + 1`
                echo "Files processed: $file_count / $file_total       CDRs match : $CDR_match / $CDR_count"

                if [ "`grep "servedAccount : '$2'" CDR.tmp`" != "" ]; then
                   CDR_match=`expr $CDR_match + 1`
                   mv CDR.tmp CDR$CDR_match.txt
                   echo -ne "Files processed: $file_count / $file_total       CDRs match : $CDR_match / $CDR_count"\\r
                fi

                CDR_start="true"
             else
                CDR_start="false"
             fi


             if [ "$CDR_start" = "true" ]; then
                echo $_line > CDR.tmp
             else
                echo $_line >> CDR.tmp
             fi

      done
done

echo
echo

Code:
Files processed: 23 / 1192       CDRs match : 0 / 373
Files processed: 23 / 1192       CDRs match : 0 / 374
Files processed: 23 / 1192       CDRs match : 0 / 375
Files processed: 23 / 1192       CDRs match : 0 / 376
Files processed: 23 / 1192       CDRs match : 0 / 377
Files processed: 23 / 1192       CDRs match : 0 / 378
Files processed: 24 / 1192       CDRs match : 0 / 1
Files processed: 24 / 1192       CDRs match : 0 / 2
Files processed: 24 / 1192       CDRs match : 0 / 3
Files processed: 24 / 1192       CDRs match : 0 / 4


Last edited by Flavius; 05-20-2015 at 03:24 PM..
# 2  
Old 05-20-2015
Code:
CDR_count=`expr $CDR_count + 1`

Is depricated, try instead:
Code:
CDR_count=$(( $CDR_count + 1 ))

Likewise for CDR_match.

Other than that (which could, or not, be the cause) the counting should work.

hth

EDIT:
You could add something like
Code:
echo running here

Inside that block, to see if its actualy executed.

Or just start the script like:
Code:
$SHELL -x ./scriptname

# 3  
Old 05-20-2015
It's gotta be the craziest bug of my carrier.

Code:
     cat $_file | while read _line
     do

             if [ "$_line" = "CDRCCN.ChargingDataOutputRecord.onlineCreditControlRecord" ]; then
#                CDR_count=`expr $CDR_count + 1`
                CDR_count=$(( $CDR_count + 1 ))

                echo "Files processed: $file_count / $file_total       CDRs match : $CDR_match / $CDR_count"

                if [ "`grep "servedAccount : '$2'" CDR.tmp`" != "" ]; then
#                   CDR_match=`expr $CDR_match + 1`
                   CDR_match=$(( $CDR_match + 1 ))
                   mv CDR.tmp CDR$CDR_match.txt
                   echo -ne "Files processed: $file_count / $file_total       CDRs match : $CDR_match / $CDR_count"\\r
                fi

                CDR_start="true"
             else
                CDR_start="false"
             fi

             if [ "$CDR_start" = "true" ]; then
                echo $_line > CDR.tmp
             else
                echo $_line >> CDR.tmp
             fi
echo test1 CDR_count=$CDR_count
      done
echo test2 CDR_count=$CDR_count


Code:
test1 CDR_count=379
test1 CDR_count=379
test1 CDR_count=379
test1 CDR_count=379
test2 CDR_count=0
Files processed: 2 / 2       CDRs match : 0 / 1
test1 CDR_count=1
test1 CDR_count=1
test1 CDR_count=1
test1 CDR_count=1

# 4  
Old 05-20-2015
change:
Code:
cat $_file | while read _line
do

done

to

Code:
while read _line
do

done < $_file

This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 05-20-2015
Oh man, this works thanks a lot. Any reasonable explanation for this??
# 6  
Old 05-20-2015
Using a pipe puts the loop into a subshell. Any variable changes inside it aren't reflected outside the loop, and once the loop finishes, the subshell ceases to exist. It's kind of like the difference between running a script and sourcing it.

It's also a good reason to get out of the habit of useless use of cat.

Last edited by Corona688; 05-20-2015 at 04:39 PM..
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 05-20-2015
Wow, makes sense now. Thanks a lot
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While loop with limit counter

#!/usr/bin/ksh c=0 while ]; do echo /tmp/unex NOT found, iter : $c; ((c = $c + 1)); sleep 2; done so, the above counter doesn't work, already tried both -lt & -gt, and changed || to && so what am I missing? Thanks in advance (5 Replies)
Discussion started by: unexistance
5 Replies

2. Shell Programming and Scripting

Loop usage with counter

Hi friends, I just want to try some thing with shell using loop and counter I have 30 Directory, in each directory number of files say 5, 10, 20 etc... directory_1 directory_2 directory_n what I want to do is read files from directory_1 say 5 files if my counter is like this m=2000 ... (6 Replies)
Discussion started by: Akshay Hegde
6 Replies

3. Homework & Coursework Questions

Min/Max/counter/while loop from file

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: The program is supposed to read in text from a given file ( different samples provided in the homework but not... (1 Reply)
Discussion started by: c++newb
1 Replies

4. Shell Programming and Scripting

How to implement the counter in loop?

Hi, I am working on a script where I need to add one functionality i.e. to could the number of tar files at particular location...but the script is working in below way. 1) create sandbox 2) Drop old member function 3) addmember function 4) Apply checkpoint lable 5) Resync operation(This... (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

5. Shell Programming and Scripting

File merging based on different counter loop

hello, File 1 main Group sub group MIT VAR_1D_DATA_TYPE 23-03-2012 MIT VAR_1D_DATA_TYPE 22-03-2012 MIT VAR_10D_DATA_TYPE 23-03-2012 MIT VAR_10D_DATA_TYPE 22-03-2012 MIT ... (0 Replies)
Discussion started by: manas_ranjan
0 Replies

6. Shell Programming and Scripting

loop with a counter on a constant in bash

Hello Everyone, I'm in need of assistance on creating a script with a counter on a certain string. Basically this script opens a log file and displays certain log data. There are two key words in the log. START and FINISH. In between the START and FINISH is a variable ACTNUMBER. It will... (1 Reply)
Discussion started by: rxc23816
1 Replies

7. Shell Programming and Scripting

S# in a for loop - concatenate $(loop counter)

Hi, hope I am posting in the right section. My problem is that I have 2 or more arguments passed and I want to check if the arguments passed exists or not. The first argument should not exist and the remaining others should exist. example: ./shells.sh argument1 argument2 argument3 ... (5 Replies)
Discussion started by: fight4love
5 Replies

8. Homework & Coursework Questions

Incrementing Variable resets outside of while loop

1. The problem statement, all variables and given/known data: Variable is resetting to 0 after incrementing in while loop My bit of scripting displays the current users logged in the machine. Then it reads in a specific username and displays the processes for that user. The portion that I... (3 Replies)
Discussion started by: ratzlaff
3 Replies

9. Shell Programming and Scripting

Using the counter of a for loop in command line parameter

Say I have (in psuedocode) For i=1 to 10 tar cvfb /... 5*i /junk(i) end What I mean is that I want each successive for loop to have the block size parameter be 5 times the current counter. This isn't my actual code, just a stupid example...So the question is how do I descrive that parameter... (2 Replies)
Discussion started by: jeriryan87
2 Replies

10. Shell Programming and Scripting

Loop counter variable display

Hello everyone, how can I send output to the screen from a running script or tcl, in such a way that if a loop is executing I will see the rolling counter on my screen as the records are processed in the loop. I do not want the screen to scroll, though. In other words can a var's value be painted... (2 Replies)
Discussion started by: lifespan
2 Replies
Login or Register to Ask a Question