I am trying to run subscripts from a main script, but I want to make sure than no more than n subscripts run at the same time.
The following simplified example illustrates.
Each subscript creates a dummy file in RAM (/dev/shm/) with a name made of a unique timestamp, and deletes it once done.
The main script counts the number of dummy files in /dev/shm/ originating from the subscripts, and doesn't (shouldn't) launch a new subscript if 2 or more of them are already running.
However, the main script seems to ignore the while condition, and launches all 5 subscripts at once.
What's wrong with my code?
mainscript.txt
#!/bin/bash
for counter in $(seq 1 5)
do
while [ $(ls -1 /dev/shm/|grep "script044"|wc -l) -ge 2 ]
do
sleep 0
done
xterm -e "bash script044.txt" &
done
exit
script044.txt (subscript)
#!/bin/bash
tempfilename="script044_"$(date +%Y%m%d%H%M%S)_${RANDOM}
echo > /dev/shm/${tempfilename}
for counter in $(seq 1 $(shuf -i 10-45 -n 1))
do
sleep 1
printf "${counter}\r"
done
rm /dev/shm/${tempfilename}
exit

