Try a simple script like this, which backgrounds a query that directs stdout to a file, then immediately checks for the file and deletes it. You can see that the file is created immediately by the mysql command, so the subsequent if block deletes it.
However the deleted filehandle is still in use by the previous command, so it happily writes to nothing.
#!/usr/bin/env bash
mysql -h localhost -e "select sleep(3)" > query.out &
if [[ -e "query.out" ]];
then
echo "File exists"
rm query.out
fi
sleep 4
if [[ ! -e "query.out" ]];
then
echo "File doesn't exist"
fi
Try a simple script like this, which backgrounds a query that directs stdout to a file, then immediately checks for the file and deletes it. You can see that the file is created immediately by the mysql command, so the subsequent if block deletes it.
However the deleted filehandle is still in use by the previous command, so it happily writes to nothing.
#!/usr/bin/env bash
mysql -h localhost -e "select sleep(3)" > query.out &
if [[ -e "query.out" ]];
then
echo "File exists"
rm query.out
fi
sleep 4
if [[ ! -e "query.out" ]];
then
echo "File doesn't exist"
fi