943,539 Members | Top Members by Rank

Ad:
Feb 9th, 2009
0

For loop not bringing variable back properly

Expand Post »
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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gornhorse is offline Offline
2 posts
since Sep 2008
Feb 9th, 2009
0

Re: For loop not bringing variable back properly

>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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Feb 9th, 2009
0

Re: For loop not bringing variable back properly

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
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007
Feb 11th, 2009
0

Re: For loop not bringing variable back properly

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gornhorse is offline Offline
2 posts
since Sep 2008
Feb 11th, 2009
0

Re: For loop not bringing variable back properly

Glad to have been part of the solution for once

best wishes,

, Mike
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: count line
Next Thread in Shell Scripting Forum Timeline: number maniplation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC