Frage

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
War es hilfreich?

Lösung

(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

Andere Tipps

Just set the variable for the one command:

UnitIP=xyz subprocess &
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top