FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Ubuntu > Ubuntu User

 
 
LinkBack Thread Tools
 
Old 12-13-2009, 11:22 PM
Ray Parrish
 
Default Global variables, and their scope in Bash.

Hello,

I have a global variable called LoopCount, that I want to access, and
write to in four different functions. The functions are as follows, [not
actual code, just showing function names] -

ReadintoArray [
# Loop with LoopCount, then reset it to zero.
}
WriteTopSection {
# Loop with LoopCount, then decrement it one.
}
WriteDescriptionSection {
# Loop with LoopCount starting where WriteTopSection left off.
}
WriteEndSection {
# Loop with LoopCount starting where WriteDescriptionSection left off.
}
# Main Program
declare -i LoopCount
ReadintoArray
WriteTopSection
WriteDescriptionSection
WriteEndSection

The problem I am having, is that in the ReadintoArray section the global
variable LoopCount gets modified, and I have to reset it to zero at the
end to get WriteTopDescription to start in the proper place.

Then perversely the ending value of LoopCount does not get carried
forward from WriteTopSection to WriteDescriptionSection, but instead is
once again at the value of zero.

Why in the world can my ReadintoArray function modify the value of the
global variable LoopCount, but the next function down cannot?

Does anyone have any light they can shed on the behavior of global
variables in Bash?

Thanks, Ray Parrish


The Future of Technology.
http://www.rayslinks.com/The%20Future%20of%20Technology.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com



--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 12-13-2009, 11:35 PM
Lachlan Musicman
 
Default Global variables, and their scope in Bash.

On Mon, Dec 14, 2009 at 11:22, Ray Parrish <crp@cmc.net> wrote:
> Hello,
>
> I have a global variable called LoopCount, that I want to access, and
> write to in four different functions. The functions are as follows, [not
> actual code, just showing function names] -
>
> ReadintoArray [
> * * # Loop with LoopCount, then reset it to zero.
> }
> WriteTopSection {
> * * # Loop with LoopCount, then decrement it one.
> }
.....
> Then perversely the ending value of LoopCount does not get carried
> forward from WriteTopSection to WriteDescriptionSection, but instead is
> once again at the value of zero.

that would be because you are decrementing by 1 as you loop LoopCount times?

> Why in the world can my ReadintoArray function modify the value of the
> global variable LoopCount, but the next function down cannot?
>
> Does anyone have any light they can shed on the behavior of global
> variables in Bash?
>
> Thanks, Ray Parrish
>
>
> The Future of Technology.
> http://www.rayslinks.com/The%20Future%20of%20Technology.html
> Ray's Links, a variety of links to usefull things, and articles by Ray.
> http://www.rayslinks.com
> Writings of "The" Schizophrenic, what it's like to be a schizo, and other
> things, including my poetry.
> http://www.writingsoftheschizophrenic.com
>
>
>
> --
> ubuntu-users mailing list
> ubuntu-users@lists.ubuntu.com
> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
>



--
the historian of science looks in vain for a single line of development.

Norbert Wiener.

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 

Thread Tools




All times are GMT. The time now is 05:16 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org