Why reset the variable set inside the loop (bash script)?

Sirs, I am writing a simple controller flows and are in a trouble, feel that some kind of change, but could not find :(
The problem is that I process cycle and in good condition is change the variable $freePipe to finish the main cycle, but I'm changing it inside the loop of the second level and when I check it in the main loop to continue or stop, then it resets the value. Below simplified code
freePipe="0"
 while [ "$freePipe" == 0 ]
 echo $freePipe
do
 echo "Begin selection"
 echo $freePipe

 cat $ipListFile | while read line
do
freePipe=$line
 echo "1 - $freePipe"
done
 echo "2 - $freePipe"
 sleep 1
 echo "3 - $freePipe"
 done

Need to get $freePipe and get freePipe=0 always :(
And echo 1 shows the set value, and 2 and 3 indicate "0"
July 9th 19 at 10:54
3 answers
July 9th 19 at 10:56
Solution
You are calling the external cat command with a pipe " | " and everything that goes in the block after the conveyor will run in a separate shell. Hence the "echo 1 freePipe" will output the internal variable of the process cat, not your cycle.

Yeah, and to finish the loop, use the exit or break.
A break can have an argument, to finish a certain cycle of nesting.
for example

while true
do
 while true
do
 while true
do
 break 2
 echo 3
done
 echo 2
done
 echo 1
done


will output only 1, because the break out of two nested loops, left in the third (i.e. outermost)
Thank you, about the cat alone is not guessed... I use Break, but with the sub it does not go beyond and cannot, unfortunately, to stop the main loop :P

For everyone else, about how you can redirect a stream, you can read a 7-paragraph - https://habrahabr.ru/post/47706/ - Madelynn_Schuppe commented on July 9th 19 at 10:59
:
You are calling the external cat command, so that what comes through the pipeline | refers to the external command, not your cycle. Hence the "echo 1 freePipe" will output the internal variable of the process cat, not your cycle.

Not, the channel causes the execution of a new sub-process, so all variables created in the channel, visible only to those programs that are in this channel. You can concatenate several channels and each channel will have its own isolated scope variables. - Ramon.Stracke commented on July 9th 19 at 11:02
July 9th 19 at 10:58
while [ condition ]
do
op1
op2
done


and still need to remember, it seems in the above code, the subshell will internally assign and then exit. it's clear that in the parent shell this assignment will not change.

by the way, the "simplified code" a senseless and merciless.
thank you ! subshell okazyvatsya because of the cat is formed just with while can be run without problems :) - Madelynn_Schuppe commented on July 9th 19 at 11:01
July 9th 19 at 11:00
Loaf and horns from a trolley bus.

gnu parallel or xargs to help you. https://debian.pro/1834
the current problem is already understood, but in the future will take note of this, looks delicious! - Madelynn_Schuppe commented on July 9th 19 at 11:03

Find more questions by tags ProgrammingbashLinuxCommand line