GNU`sem`似乎没有正确阻止

时间:2014-10-07 18:39:45

标签: bash parallel-processing sem

我编写了一个bash脚本,使用GNU semaria2同时下载多个文件,然后在完成后发出蜂鸣声。

#!/bin/bash

filename="${1:-files.txt}" # default input file is files.txt

dflag=0
#currdir=''

while read -r line
do
    if [ "$line" ] # skip blank lines
    then
        if [[ "$line" =~ (https?|ftp)\:\/\/ ]]
        then
            if [ $dflag -eq 1 ]
            then
                dflag=0
            fi
            echo "URL: '$line'"
            # -j N Run up to N commands in parallel
            # -j +0 is useful as it will run number-of-cpu-cores jobs simultaneously
            sem --no-notice -j +0 aria2c --file-allocation=none -c -x10 -j10 -d \"$currdir\" \"$line\"
        else
            echo "Directory: '$line'"
            if [ $dflag -eq 1 ]
            then
                currdir="${currdir}/${line}"
            else
                #currdir=''
                currdir="$line"
                dflag=1
            fi
            echo "$currdir"
            if [ ! -d "$currdir" ]
            then
                mkdir -p "$currdir"
            fi
        fi
    fi
done < "$filename"
sem --wait # waits until all jobs are done
echo -en "\007"; echo -en "\007"; echo -en "\007" 

我的脚本工作正常,但在作业完成之前很久就会发出哔哔声。我认为脚本会阻塞,直到循环完成,但它没有这样做。如果我尝试一个简单的测试脚本,它会按预期工作:

#!/bin/bash

for i in {1..10}
do
  sem --no-notice -j +0 echo $i
done
sem --wait
echo -en "\007"; echo -en "\007"; echo -en "\007"

那么我该怎么做才能使'真实'脚本正常工作?

BTW我正在使用GNU bash, version 4.3.27(1)-release (x86_64-apple-darwin12.5.0)

0 个答案:

没有答案