I am trying to figure out how the while loop logic works in bash.
If I have 4 instructions; that are in a loop, they will be executed n times, following the conditions set in the loop.
Although; is the loop aware of the return code for each of these commands?
Ideally you could write a shell script with a while loop where you check for exit code of each command, and do it again if any of the command return an error. Is this logic possible in bash or the loop is not aware of the return code of each command, and see the block between the do-done, as one?
EDIT:
example:
a=2
b="test"
while [ "$?" != 0 ]
do
ls $b
cd $b
grep "2.txt" | mv 2.txt $a.txt
ls $b
done
This simplified example is basically checking for a dir, cd into it, find a file with a specific name and rename it.
When I run this loop, if any of these commands return code is not 0, the loop should start all over, until all the commands return 0 and then the loop exit.
Is this actually true or the whole block of commands is handled as single entity, so if the last command return 0 it will exit?
$?, which is set by the last statement prior to the while loop for the first iteration, and by the last statement of the body for subsequent iterations.