Problem with getting array from function (bash)
Hi to all,
I have an problem with making changes to the array that is populated inside the function. I need to use it in main script and further functions. I use CentOS 5.3 with bash 3.2.25. Problem is when I finish "while read" multiple lines from grep output, I loose all changes made to the array menuitems (not menuitem which is local array). How can I retrieve that array from function, or write directly to global array so element are not lost after the while loop? Code:
#!/bin/bash |
A function runs as a child process, and so all variables inside it are simply not available to the parent-level of the script. You'll need to redesign your function and the top-level command using it so that it's output sets a global array, or to otherwise generate the values you need directly.
|
Ok, thanks. I solved it by creating temporary file that accepts grep output:
Code:
#!/bin/bash |
Actually, with regard to functions:
Code:
:cat function Quote:
Your actual problem and some other ways around it in addition to your own: BashFAQ24 |
Huh. So I was wrong then. Functions don't run as subshells. I just tested it myself, and it looks like I wasn't just wrong in my first post, but completely wrong. Variables set in functions are modified globally, unless declared locally.
Sorry about that. :( On the other hand, at least it does look like I was right that a subshell was to blame, since the loop in question was running inside a subprocess created by a pipe. :) On the gripping hand, I should've noticed it. I've experienced that issue before myself. :( |
Try changing num to $num in
Code:
menuitems[num]=${tempmenuitem[i]} Code:
IFS=',' tempmenuitem=(`echo "$menuline"`) There's no need for the echo in Code:
tempmenuitem=(`echo "$menuline"`) Code:
IFS=',' tempmenuitem=( $menuline ) You can avoid having to use a temporary file like this Code:
while read menuline |
Thanks catkin. When you lear from examples like: "you can do this in many ways" , you are bound to lack optimization.
As for while loop, done at the end expects filename, not text, so it give error that filename is too long, and no filename lika that "/usr/local/bin/sv3multi-db: line 464: menuitem,1,0,a,SRB,showMenuG,0,0,0,0,0,0,0" where line 464 is function start, functionname() Any idea how to solve this? lines from greped file are: "menuitem,1,0,a,SRB,showMenuG,0,0,0,0,0,0,0" |
Quote:
|
Populating array inside a loop with Multiple-line text [SOLVED]
Ok, I solved my problem after lurking in Advanced Bash Shell Scripting Guide with this:
Code:
#!/bin/bash |
Quote:
Thanks anyway for all of your help. On another forum (wireless stuff) I reached ~2,800 posts in 3 years (avg 2.5 posts per day) so I know that you need willpower to help, to provide any kind of answer, not to mention finding a correct answer with scarce input. |
Quote:
|
It is no problem really. Since I am new to BASH, I just add A LOT of echo commands for all relevant variables after every relevant block of code and just observe the output and adjust as needed.
Also, there is nothing wrong in expanding on your(our) knowledge every day we breathe, that is how we get smarter. A matter of fact, if you had not gave me the "While read" redirection example, I would not have been able to trace it to this "for loop" elegant solution for arrays from multiple-line text, so many thanks for that. |
Quote:
|
You could just put
set -xv as the 2nd line of your code. It shows everything before and after substitutions etc as it goes. Great for debugging. :) |
Wellllll, that is nice to know, but my current (combined) script has ~1000 lines and output definitely crosses that line. And just one of my arrays will fill ~100 rows on the screen when I populate database file. "Not everything that shines is made of gold...". :tisk: :D
I put echo lines as I see fit, and then just comment them out when I need them, parts at the time. If I change something before that point, I just uncomment them out again until I am pleased with changes. It's a best way to go. Thanks on the tip, I've save it for future use. P.S. I have been programming in BASIC, DOS Batch files, Pascal/Delphi, VisualBasic/Access, SQL code, and finaly BASH since 1993-1994. On and off of course, but let's say I have been arround. ;) |
All times are GMT -5. The time now is 01:53 AM. |