Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Bash Local Variable Recursion With Array


    I have read the guides on bash scripting but they seem too general or over-complicated for what I am trying to do. I have a function (listed below) which calls itself recursively. Ultimately I want this to function like it does in C but instead of level incrementing and decrementing correctly as it recurses it still stays at 1. I want to later use level to calculate the number of tabs or spaces to output a tree like structure. BTW, I can't change the input data structure. Thanks in advance.

    # ************************************************** ***************
    # sample data
    # ************************************************** *****************
    TEST6[0]="Test6"
    TEST5[0]="Test5"
    TEST4[0]="Test4"
    TEST4[1]=${TEST6[@]}
    TEST3[0]="Test3"
    TEST3[1]=${TEST5[@]}
    TEST2[0]="Test2"
    TEST2[1]=${TEST3[@]}
    TEST2[2]=${TEST4[@]}
    TEST1[0]="Test1"
    TEST1[1]=${TEST2[@]}
    TEST1[2]=${TEST6[@]}

    # ************************************************** *****************
    generate_recursively()
    {

    # entering function, increment
    local level=`expr $2 + 1`

    local passed_array
    passed_array=(`echo "$1"`)

    for index in $(seq 1 $((${#passed_array[@]} - 1)))
    do
    need_generated=( "${need_generated[@]}" "${level}${passed_array[$index]}" )
    generate_recursively "${passed_array[$index]}" "${level}"
    done

    # leaving function, decrement
    level=`expr $2 - 1`

    }

    # ************************************************** *****************
    display()
    {

    local passed_array
    passed_array=(`echo "$1"`)

    for index in $(seq 0 $((${#passed_array[@]} - 1)))
    do
    echo -ne "${passed_array[$index]}\n"
    done

    }

    argument=`echo ${TEST1[@]}`
    need_generated[0]=${TEST1[0]}
    generate_recursively "$argument" 0

    argument2=`echo ${need_generated[@]}`
    display "$argument2"

  2. #2
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,472
    Hi, jshivers.

    I modeled your function in a very simple script:
    Code:
    #!/bin/bash
    
    # @(#) s5       Test recursion in bash functions, local variable.
    
    merry()
    {
    
    echo " Entered with argument      = $2"
    local level=$2
    if [[ $level > 4 ]]
    then
            echo
            echo " The limit reached at level = $level"
            return
    fi
    level=`expr $level + 1`
    
    echo " Calling oneself with level = $level"
    merry "Other stuff" $level
    
    echo " Exiting with level         = $level"
    }
    
    bash --version
    merry "Anything you want" 0
    The results:
    Code:
    > ./s5
    GNU bash, version 2.05b.0(1)-release (i586-suse-linux)
    Copyright (C) 2002 Free Software Foundation, Inc.
     Entered with argument      = 0
     Calling oneself with level = 1
     Entered with argument      = 1
     Calling oneself with level = 2
     Entered with argument      = 2
     Calling oneself with level = 3
     Entered with argument      = 3
     Calling oneself with level = 4
     Entered with argument      = 4
     Calling oneself with level = 5
     Entered with argument      = 5
    
     The limit reached at level = 5
     Exiting with level         = 5
     Exiting with level         = 4
     Exiting with level         = 3
     Exiting with level         = 2
     Exiting with level         = 1
    My observations are that you don't seem to have a test for exiting the recursion. In my script I test for a limit, then return without calling the function again, which then produces the exit cascade.

    The decrease of the local variable will not make any difference because it's local. I'm guessing that you put that in because the script wasn't stopping.

    Keep us posted on the changes you make ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •