Question

Hi there everyone at stackoverflow I'm having an issue with a bash script running as a child with a variable conflict from my parent script. I have a subprocess that starts and pulls variables from the parent. What I have going on is that within each of those child processes that run simultaneously every second the varaibles crossover between children. for example UnitIP UnitPort and Unitname are all defined from and array but as the next child opens the other child also receives the changes made to unitip unitPort, etc... anyone have a solution for this problem? so let's say UnitIP=${IP[0]} child is opened with UnitIP=${IP[0]}, loop comes back around UnitIP=${IP[1]} second child opens with UnitIP=${IP[1]} and the first child also changes to UnitIP=${IP[1]} thank you for your time and help.

for (( c=0; c<=$id-1; c++ ))
do
t8=$t
export t=$t8
export UnitIP=${IP[$c]}
export UnitPort=${Port[$c]}
export UnitName=${Name[$c]}
export UnitActive=${Active[$c]}
subsprogram.sh &
done
Was it helpful?

Solution

(untested) You could create a subshell for each child so they don't share the same parent environment:

for (( c=0; c<=$id-1; c++ ))
do
    t8=$t
    (
        export t=$t8
        export UnitIP=${IP[$c]}
        export UnitPort=${Port[$c]}
        export UnitName=${Name[$c]}
        export UnitActive=${Active[$c]}
        subsprogram.sh &
    ) 
done

OTHER TIPS

Just set the variable for the one command:

UnitIP=xyz subprocess &
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top