LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-24-2007, 03:46 AM   #1
scsi.king
LQ Newbie
 
Registered: Apr 2007
Posts: 6

Rep: Reputation: 0
Unhappy 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.
 
Old 04-24-2007, 03:51 AM   #2
scoban
Member
 
Registered: Nov 2004
Location: Turkey
Distribution: Slackware
Posts: 145

Rep: Reputation: 16
Take a look at this link: http://www.faqs.org/docs/bashman/bashref_49.html#SEC49
 
Old 04-24-2007, 03:56 AM   #3
v.tieubao
LQ Newbie
 
Registered: May 2006
Posts: 7

Rep: Reputation: 0
Try rewrite:
Code:
((TS+=$X))
((NF++))
To:
Code:
TS=$((TS+X))
NF=$((NF+1))
Cheers,
VTB.
 
Old 04-24-2007, 05:34 AM   #4
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
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 )

Last edited by makyo; 04-24-2007 at 08:00 AM.
 
Old 04-24-2007, 11:19 AM   #5
scsi.king
LQ Newbie
 
Registered: Apr 2007
Posts: 6

Original Poster
Rep: Reputation: 0
Unhappy 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.

Last edited by scsi.king; 04-24-2007 at 11:26 AM.
 
Old 04-24-2007, 11:25 AM   #6
scsi.king
LQ Newbie
 
Registered: Apr 2007
Posts: 6

Original Poster
Rep: Reputation: 0
Lightbulb 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
 
Old 04-24-2007, 12:35 PM   #7
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
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 )

Last edited by makyo; 04-24-2007 at 12:38 PM.
 
Old 04-25-2007, 05:41 AM   #8
omnio
Member
 
Registered: Feb 2007
Location: $HOME
Distribution: Hardened Gentoo
Posts: 66
Blog Entries: 1

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
Strange behavior: string within scope return 0 if not within a while loop. RHLinuxGUY Programming 2 08-05-2006 11:05 PM
scope of an object and global scope lucky6969b Programming 7 12-09-2005 09:09 PM
Bash for loop Genjix Programming 5 12-23-2004 02:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration