|
From: | Frans de Boer |
Subject: | Re: [Help-bash] Updating global variable from recursing function. |
Date: | Mon, 02 Apr 2012 22:14:07 +0200 |
User-agent: | Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.28) Gecko/20120306 SUSE/3.1.20 Thunderbird/3.1.20 |
On 04/02/2012 09:17 PM, Greg Wooledge wrote:
No I did not show you the whole file (it is about 2184 lines total). But I made sure that the variable iHiMDirNest is declared at the global level, so before other functions and together with other variables. The thing is that it goes wrong if mdirtree() is called from within. The initial mdirtree() is called from another function, so it will always run at the third level anyhow (main->func->func)On Mon, Apr 02, 2012 at 09:08:12PM +0200, Frans de Boer wrote:Strange, it is working fine, the counter is incremented but the result is never propagated to the real global variable, as shown by the bash -x output.imadev:~$ cat foo #!/bin/bash foo() { local i=7 if ((i> max)); then max=$i; fi } max=0 foo echo "max is $max" imadev:~$ ./foo max is 7Every time the function returns to the caller, the iHiMDirNest is restored to the level it was before being calling.That suggests that the variable is in fact local, in some scope or other. Are you sure you showed us the *uppermost* scope? For example, this would do it: imadev:~$ cat bar #!/bin/bash bar() { local max foo "$@" } foo() { local i=7 if ((i> max)); then max=$i; fi echo "inside foo, max=$max" } max=0 bar echo "at global scope, max is $max" imadev:~$ ./bar inside foo, max=7 at global scope, max is 0
I tested it on a directory structure of 7 level deep. The counter did well but when finally returning the count decreases with every return until it stop at the first instance of mdirtree() where the count was correct. So, calling from main-func-func worked fine, but when the last function repeated itself one or more times, it went wrong.
I try a simple example myself to look if it is due to the code size/complexity or something else.
Frans.
[Prev in Thread] | Current Thread | [Next in Thread] |