When dealing with bash (or other shells) scripts, instead of starting check outputs, write to temp file, trying to pass variables out of their scope from oine subshell to another, just remember that there is that thing called return status that can do the trick in a simpler, quicker and easier to read way.
For example, if you need to wait for a MySQL server to do not have pending queries before starting to do something, just do
while ( mysqladmin|grep -vi "show processlist"|grep "Query" > /dev/null )
sleep 0.1 # or whatever you want, prevent system overload
# ok, it's executing something, let's do thing A
# ok, it's done, let's do thing B
It’s (almost) one line long, it’s simple, effective and it just works.