For loop not bringing variable back properly

Reply

Join Date: Sep 2008
Posts: 2
Reputation: gornhorse is an unknown quantity at this point 
Solved Threads: 0
gornhorse gornhorse is offline Offline
Newbie Poster

For loop not bringing variable back properly

 
0
  #1
Feb 9th, 2009
Hi, I have two shell scripts that do the same thing, but are written differently. The first one works:
Shell Scripting Syntax (Toggle Plain Text)
  1. size=0
  2. pattern=foo
  3.  
  4. for file in *
  5. do
  6. [ ! -f $file ] && continue
  7.  
  8. if grep $pattern $file > /dev/null
  9. then
  10. tsize=`cat $file |wc -c`
  11. size=`expr $size + $tsize`
  12. echo "size is: $size"
  13. fi
  14. done
  15. echo "Total size of files containing $pattern is: $size"
With the output of
Shell Scripting Syntax (Toggle Plain Text)
  1. $ ./foofinder
  2. size is: 34
  3. size is: 77
  4. size is: 223
  5. size is: 478
  6. size is: 758
  7. size is: 855
  8. Total size of files containing foo is: 855

But, I then have this script:
Shell Scripting Syntax (Toggle Plain Text)
  1. pattern=foo
  2. total=0
  3.  
  4. ls -l | while read j1 j2 j3 j4 fsize j5 j6 j7 fname
  5. do
  6. [ ! -f $fname ] && continue
  7.  
  8. if grep $pattern "$fname" > /dev/null 2>%1
  9. then
  10. total=`expr $total + $fsize`
  11. echo "size is: $total"
  12. fi
  13. done
  14. echo "Total size of files containing $pattern is: $total"
With the output of
Shell Scripting Syntax (Toggle Plain Text)
  1. $ ./foofinder2
  2. size is: 34
  3. size is: 77
  4. size is: 223
  5. size is: 478
  6. size is: 758
  7. size is: 855
  8. Total size of files containing foo is: 0

Basically, I want to know why the second script returns the line:
Shell Scripting Syntax (Toggle Plain Text)
  1. Total size of files containing foo is: 0
, when the last known value of the $size variable is 855, but the first script gets it right? Why should they be any different?

Your help with this would be greatly appreciated, as I am just new to learning shell scripting, and I would like to know why they are producing different results.

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,030
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: For loop not bringing variable back properly

 
0
  #2
Feb 9th, 2009
>Basically, I want to know why the second script returns the line:
> Total size of files containing foo is: 0
Variable scope problem in the while loop with | (pipe)
More here.
Last edited by Aia; Feb 9th, 2009 at 10:43 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: For loop not bringing variable back properly

 
0
  #3
Feb 9th, 2009
Hey there,

Just in case you have to do it the second way, this form works in bash 3.x (possibly earlier versions) and avoids the variable scoping problem while maintaining the integrity of the command you were feeding to the pipe.

Shell Scripting Syntax (Toggle Plain Text)
  1. while read j1 j2 j3 j4 fsize j5 j6 j7 fname
  2. do
  3. [ ! -f $fname ] && continue
  4.  
  5. if grep $pattern "$fname" > /dev/null 2>%1
  6. then
  7. total=`expr $total + $fsize`
  8. echo "size is: $total"
  9. fi
  10. done <<< "`ls -l`"
  11. echo "Total size of files containing $pattern is: $total"

hope it helps

, Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 2
Reputation: gornhorse is an unknown quantity at this point 
Solved Threads: 0
gornhorse gornhorse is offline Offline
Newbie Poster

Re: For loop not bringing variable back properly

 
0
  #4
Feb 11th, 2009
Thanks for that!

I used the 3rd option that is sh-compliant, with the fix of putting the last echo inside the } bracket.

I didn't realise there was an issue with ammending the variable value that was set outside the loop - it's not anywhere in my training! So, at least i've learned from my mistake!

Thanks very much for your help - greatly appreciated.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: For loop not bringing variable back properly

 
0
  #5
Feb 11th, 2009
Glad to have been part of the solution for once

best wishes,

, Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC