global variable in bash function not global if called with & to background the command -


here example of i'm trying achieve.

#!/bin/bash  func2() {     myarray=("e" "f") }  func1() {     myarray=("c" "d")     in [1..10]             func2 &     done }  myarray=("a" "b") func1 echo "${myarray[@]}" 

the echo @ end should

e f 

but ends being

c d 

because of & when call func2. if take out & runs expected. proof of concept more complicated script using make ssh calls several servers @ once. need multi-threading capability of &, since seems run in subshell, of variables local?!?!?

i assume questions how affect parent shell variables subshell, although don't quite ask it.

there no way directly, can manage communicating background processes. easiest way arrange them write data files after completion , interpreting that. if trust subprocesses, make them write bash code , eval in parent process.

you might able detect when finish using trap on sigchld.

another way might using gnu "parallel" tool , reaping output files when completes.

yet way using tools "parallel-ssh".

a more specific answer made if provide more details.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -