(Convention - .txt are just plain text files. .sh files are shell script files.).
Your mainscript.txt script has a race condition. Specifically the while loop starts its next iteration before the script044.txt script is able to create the temporary file. In fact the whole loop is iterated through before any of these files get created.
A more robust way to deal with this sort of thing is to forget the temporary files and use the shell builtin wait instead:
#!/bin/bash
pid_count=0
for counter in $(seq 1 5)
do
xterm -e "bash script044.txt" &
pid_list+=" $!"
(( pid_count++ ))
if (( pid_count++pid_count > 2 )); then
wait -n $pid_list
(( pid_count-- ))
fi
done
exit
This adds the pid of every xterm started to the list pid_list, and increments a counter every time a subprocess is started. If the counter is greater than 3 then we wait for any of the pids in $pid_listnext subprocess to finish. When wait returns, then we know one has finished, so we can then decrement the counter and go around again to start the next xterm.
You can remove all the tempfilename-related lines from the script044.txt - they are no longer needed.
As @chepner points out, the required -n option is only available in bash 4.3 or later.