如何控制 shell 脚本中后台作业的数量

How to control the number of background job in shell script

我有一个shell脚本来下载一批文件,想控制它有一定数量的后台下载作业。

json='list.json'
wget -o $json http://fileserver/getFileList

jobsCount=0
while IFS= read -r url ; do

        #download the file
        wget -q $url &

        #contorling the amount of concurrent jobs
          while true; do
            #get current background job count
            jobsCount=$(jobs -p | wc -l)

            if [ $jobsCount -lt 6 ]; then
              echo "jobsCount $jobsCount"
              break
            fi
            echo "jobsCount $jobsCount (Sleep)"
            sleep 1
          done

done < <(jq -r '.data[] | (.URL)' <$json)
#wait for last batch of job to finish
wait