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:59 PM
NoOp
 
Default Global variables, and their scope in Bash.

On 12/13/2009 04:22 PM, Ray Parrish wrote:
...
>
> Does anyone have any light they can shed on the behavior of global
> variables in Bash?
>
> Thanks, Ray Parrish
>

Perhaps these will help:
http://tldp.org/LDP/abs/html/
http://tldp.org/LDP/Bash-Beginners-Guide/html/
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_02.html




--
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:59 PM
Ray Parrish
 
Default Global variables, and their scope in Bash.

Lachlan Musicman wrote:
> 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?
>
Sorry, I was a little unclear there... It actually increments up to a
certain value, then decrements only one *after* the loop so that the
next section can have the proper value to carry on through an array from
that point.

I cannot understand why ReadintoArray can affect it's value globally,
but WriteTopSection cannot. It's always set to 0 when
WriteDescriptionSection starts, but should be at around 15 or so.

Later, Ray Parrish
>
>> 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 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-14-2009, 12:26 AM
NoOp
 
Default Global variables, and their scope in Bash.

On 12/13/2009 04:59 PM, NoOp wrote:
> On 12/13/2009 04:22 PM, Ray Parrish wrote:
> ...
>>
>> Does anyone have any light they can shed on the behavior of global
>> variables in Bash?
>>
>> Thanks, Ray Parrish
>>
>
> Perhaps these will help:
> http://tldp.org/LDP/abs/html/
> http://tldp.org/LDP/Bash-Beginners-Guide/html/
> http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_02.html
>

Apologies, I forgot these:
http://tiswww.case.edu/php/chet/bash/bashtop.html
http://www2.codegnome.org:59321/cgi-bin/mailman/listinfo/shell.scripting
http://www.gnu.org/software/bash/manual/bashref.html




--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 12-14-2009, 01:24 AM
Ray Parrish
 
Default Global variables, and their scope in Bash.

NoOp wrote:
> On 12/13/2009 04:22 PM, Ray Parrish wrote:
> ...
>
>> Does anyone have any light they can shed on the behavior of global
>> variables in Bash?
>>
>> Thanks, Ray Parrish
>>
>>
>
> Perhaps these will help:
> http://tldp.org/LDP/abs/html/
> http://tldp.org/LDP/Bash-Beginners-Guide/html/
> http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_02.html
>
Thanks for the references. I've been using the Advanced Bash Scripting manual, and I could not find anywhere that it talked about globals.

According to the third link you gave, a global variable may not be modified by a subshell, which defeats the actual purpose of a global variable as it was understood by me. If I cannot modify it's value so that altered value can be available to another function, what good is it anyway?

I take it then that each of my functions is a "subshell", so the value of a global variable would not be alterable by any of them?

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-14-2009, 01:43 AM
NoOp
 
Default Global variables, and their scope in Bash.

On 12/13/2009 06:24 PM, Ray Parrish wrote:
> NoOp wrote:
>> On 12/13/2009 04:22 PM, Ray Parrish wrote: ...
>>
>>> Does anyone have any light they can shed on the behavior of
>>> global variables in Bash?
>>>
>>> Thanks, Ray Parrish
>>>
>>>
>>
>> Perhaps these will help: http://tldp.org/LDP/abs/html/
>> http://tldp.org/LDP/Bash-Beginners-Guide/html/
>> http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_02.html
>>
> Thanks for the references. I've been using the Advanced Bash
> Scripting manual, and I could not find anywhere that it talked about
> globals.
>
> According to the third link you gave, a global variable may not be
> modified by a subshell, which defeats the actual purpose of a global
> variable as it was understood by me. If I cannot modify it's value so
> that altered value can be available to another function, what good is
> it anyway?
>
> I take it then that each of my functions is a "subshell", so the
> value of a global variable would not be alterable by any of them?
>
> Thanks, Ray Parrish
>
>
>

Don't know (sorry), but the link for the shell list might have answers:
http://www2.codegnome.org:59321/cgi-bin/mailman/listinfo/shell.scripting


--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 12-14-2009, 02:07 AM
Ray Parrish
 
Default Global variables, and their scope in Bash.

NoOp wrote:
> On 12/13/2009 04:59 PM, NoOp wrote:
>
>> On 12/13/2009 04:22 PM, Ray Parrish wrote:
>> ...
>>
>>> Does anyone have any light they can shed on the behavior of global
>>> variables in Bash?
>>>
>>> Thanks, Ray Parrish
>>>
>>>
>> Perhaps these will help:
>> http://tldp.org/LDP/abs/html/
>> http://tldp.org/LDP/Bash-Beginners-Guide/html/
>> http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_02.html
>>
>>
>
> Apologies, I forgot these:
> http://tiswww.case.edu/php/chet/bash/bashtop.html
> http://www2.codegnome.org:59321/cgi-bin/mailman/listinfo/shell.scripting
> http://www.gnu.org/software/bash/manual/bashref.html
>

Thanks for the link to the scripting group. I have been looking for a
good one, but have only found the bug-bash groups so far, which isn't
for learning scripting. Now I can quit bugging the Ubuntu User's group
with bash questions. 8-)

Later, 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-14-2009, 02:07 AM
Cameron Hutchison
 
Default Global variables, and their scope in Bash.

Ray Parrish <crp@cmc.net> writes:

>I take it then that each of my functions is a "subshell", so the value
>of a global variable would not be alterable by any of them?

No. A function is not a subshell. It executes within the current shell
instance.

The right-hand sides of a pipeline are executed in a subshell though. I
noted earlier that you had a construct of:

some command | while read a b c ; do
some other command
...
done

Anything within that while loop is in a subshell (it's on the right-hand
side of the pipeline. Any changes you make to variables in that while
loop will not be propagated to the parent shell.

A subshell is a child process of the main process. Any changes in a
child process cannot be simply propagated up. You will need to either
restructure your code or use some method of IPC.

Googling for "bash variables in while loop pipeline" should give you
more info on this topic.

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 12-14-2009, 09:09 AM
"Shahar Dag"
 
Default Global variables, and their scope in Bash.

----- Original Message -----
From: "Ray Parrish" <crp@cmc.net>
To: "Ubuntu user technical support,not for general discussions"
<ubuntu-users@lists.ubuntu.com>
Sent: Monday, December 14, 2009 2:22 AM
Subject: 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
>

Just a wild guess
Did you try to declare LoopCount before the functions

declare -i LoopCount
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
ReadintoArray
WriteTopSection
WriteDescriptionSection
WriteEndSection

Shahar


--
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 01:21 PM.

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