Bash while loop wait for mysql import

I'm attempting to create a bash script to initialise a database. Right now everything is going well -- I've been able to create the database, create the user, and grant the user permission on the database.

Now it comes to running the import script. I can do it simply by running the mysql command, but then the user sits there for potentially a minute or two with no feedback.

The result of (mysql -u$newUser -p$newPassword -D$dbName < l1jdb_m9.sql) &>/dev/null & is almost always going to be a true value: that is, starting the command will succeed. while receives this true result and begins iterating. At the end of the loop, it runs the expression again, to see if it should continue iterating. Since the expression runs again, mysql is executed again, in the background, and it returns success again.

The loop will do this forever, launching a new mysql command in the background on every iteration. Since you're launching in the background, nothing is waiting for the result; it's fire-and-forget, and it always returns true.

If you want to wait for the command to complete, you could do something like this:

That captures the PID of the sub-process ($!), then checks on each loop iteration to see if the process is still running, with ps -p. The >&- discards the output, and ps returns true if the process is running, false otherwise. This will be re-evaluated each iteration, so the loop will end when the process completes.