help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Updating global variable from recursing function.


From: Greg Wooledge
Subject: Re: [Help-bash] Updating global variable from recursing function.
Date: Mon, 2 Apr 2012 16:28:41 -0400
User-agent: Mutt/1.4.2.3i

On Mon, Apr 02, 2012 at 10:14:07PM +0200, Frans de Boer wrote:
> 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.

A simple recursive function with depth counter:

imadev:~$ cat foo 
#!/bin/bash

input=/one/two/three

traverse() {
  ((maxdepth++))
  if [[ $1 = */* ]]; then
    traverse "${1#*/}"
  fi
}

maxdepth=0
traverse "$input"
echo "maxdepth=$maxdepth"

imadev:~$ ./foo
maxdepth=4



reply via email to

[Prev in Thread] Current Thread [Next in Thread]