LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-16-2006, 04:31 PM   #1
jshivers
LQ Newbie
 
Registered: Jun 2006
Posts: 1

Rep: Reputation: 0
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"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
default array size for variable in gcc compiler mlaich Programming 13 01-04-2006 05:40 PM
tar: '--no-recursion' option doesn't prevent recursion Earl Parker II Slackware 12 08-17-2004 02:49 AM
Writing bash script with recursion.. ray5_83 Programming 4 08-04-2004 05:44 PM
Variable Array in C mojozoox Programming 6 12-16-2003 01:39 AM
Assigning a string to a variable (not a pointer, not a array) JStew Programming 3 11-18-2002 08:13 AM


All times are GMT -5. The time now is 01:00 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration