| | |
For loop not bringing variable back properly
![]() |
•
•
Join Date: Sep 2008
Posts: 2
Reputation:
Solved Threads: 0
Hi, I have two shell scripts that do the same thing, but are written differently. The first one works:
With the output of
But, I then have this script:
With the output of
Basically, I want to know why the second script returns the line: , 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!
Shell Scripting Syntax (Toggle Plain Text)
size=0 pattern=foo for file in * do [ ! -f $file ] && continue if grep $pattern $file > /dev/null then tsize=`cat $file |wc -c` size=`expr $size + $tsize` echo "size is: $size" fi done echo "Total size of files containing $pattern is: $size"
Shell Scripting Syntax (Toggle Plain Text)
$ ./foofinder size is: 34 size is: 77 size is: 223 size is: 478 size is: 758 size is: 855 Total size of files containing foo is: 855
But, I then have this script:
Shell Scripting Syntax (Toggle Plain Text)
pattern=foo total=0 ls -l | while read j1 j2 j3 j4 fsize j5 j6 j7 fname do [ ! -f $fname ] && continue if grep $pattern "$fname" > /dev/null 2>%1 then total=`expr $total + $fsize` echo "size is: $total" fi done echo "Total size of files containing $pattern is: $total"
Shell Scripting Syntax (Toggle Plain Text)
$ ./foofinder2 size is: 34 size is: 77 size is: 223 size is: 478 size is: 758 size is: 855 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)
Total size of files containing foo is: 0
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!
>Basically, I want to know why the second script returns the line:
>
Variable scope problem in the while loop with | (pipe)
More here.
>
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.
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
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.
hope it helps
, Mike
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)
while read j1 j2 j3 j4 fsize j5 j6 j7 fname do [ ! -f $fname ] && continue if grep $pattern "$fname" > /dev/null 2>%1 then total=`expr $total + $fsize` echo "size is: $total" fi done <<< "`ls -l`" 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!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
•
•
Join Date: Sep 2008
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
Glad to have been part of the solution for once 
best wishes,
, Mike

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!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
![]() |
Other Threads in the Shell Scripting Forum
- Previous Thread: count line
- Next Thread: number maniplation
| Thread Tools | Search this Thread |






