LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Easy Bash Loop Scope Issue? (https://www.linuxquestions.org/questions/programming-9/easy-bash-loop-scope-issue-548529/)

scsi.king 04-24-2007 03:46 AM

Easy Bash Loop Scope Issue?
 
Hi all,
I am just learning bash programming. I know about scopes in C and Pascal. Is there some trick to bash scopes?
Well I have a loop and within it I calculate some stuff.
After the loop ends I try to print the totals and the variables seem to go back to 0 as soon as the loop ends. I'm sure its something dumb. Please help if you see my mistake. Thanks.

#!/bin/bash
#total.sh
#MB is 1 MB in bytes,
#NF = running total number of files,
#TS = running total of bytes.

declare -i MB=102400
declare -i NF=0
declare -i TS=0

ls -l *.jpg| grep "-" |cut -d ' ' -f '5' | while read -r line
do
X=$line
Size=`expr $X / $MB`
Remainder=`expr $X % $MB`
echo -e "Size: $Size.$Remainder MB"
((TS+=$X))
((NF++))
echo $NF, $TS #Sanity check - NF & TS Inc correctly
done

echo -e "Totals: $NF, $TS"

#NF = 0 and TS = 0 ??????? Scope issue of some kind.

scoban 04-24-2007 03:51 AM

Take a look at this link: http://www.faqs.org/docs/bashman/bashref_49.html#SEC49

v.tieubao 04-24-2007 03:56 AM

Try rewrite:
Code:

((TS+=$X))
((NF++))

To:
Code:

TS=$((TS+X))
NF=$((NF+1))

Cheers,
VTB.

makyo 04-24-2007 05:34 AM

Hi.

A pipeline will cause each command to be executed in a child process from the parent, called a child process. Each process has its own set of variables. You may also see the term subshell. The variables may be used, changed, etc., but the new values of the variables will not be communicated back the parent.

See Example 31-2, & 3 near the end of http://www.tldp.org/LDP/abs/html/gotchas.html ... cheers, makyo

( edit 1: clarify )

scsi.king 04-24-2007 11:19 AM

Nope. Still not working. Scope is the issue.
 
I checked scoban's link above but it didn't address Scope. Did touch on passing variables in and out of the bash environment, but didn't address my loop scope issue.
I also tried changing the variable operations of TS and NF per next suggestion, but got same results. When the loop was done, those two variables went back to zero (assigned at top of program).

If I assign TS and NF an arbitrary value like 20, during the while loop, NF counts from 21 to 32 (12 JPG files in my local dir) but resets to 20 when I print it outside the loop.
I know this has something to do with scope but not sure what dictates it nor how to solve it in this case.

scsi.king 04-24-2007 11:25 AM

makyo you are right
 
I thought that was what was happening.
I tried just a for loop and declared some variables above the loop, changed the variables in the loop, and printed outside the loop, and of course everything works fine.
BUT
In this case, the pipe that starts this loop, dictates another environment is created which makes all variable operations stay in that evnironment and not be passed back to the calling invironment. Can I set a variable that is accessable to both environments or that can be passed back to the calling environment in BASH?
Thanks

makyo 04-24-2007 12:35 PM

Hi.

See the note just prior to Example 31-3, referring to Example 14-18, and in the second part of example 31-3 itself, see the context:
Code:

# =====================Now, here is the correct way:===
also
Code:

# ==================And here is another alternative===
cheers, makyo

( edit 1: clarify )

omnio 04-25-2007 05:41 AM

And if you STILL find yourself in trouble and really need to use subshells, take a look at this post.


All times are GMT -5. The time now is 04:17 AM.