Unixでスクリプト実行の進行状況を表示するには?

Unixでスクリプト実行の進行状況を表示するには?

Perlやtclなどのいくつかのスクリプトを実行するとしましょう。たとえば、$ ./script.sh; ./script.pl; source script.tcl。実行してプロンプトに戻るには時間がかかります。実行中に実行率を表示するには?

以下にサンプルUIを見つけることができます。最後に、スクリプトの実行時間を示します。

1% Completed the exectuion of script.pl
2% Completed the exectuion of script.pl
. . . .
100% Completed the the exectuion of script.pl

Ex: Execution time for script.pl is :50 Seconds

ベストアンサー1

次のスクリプトは、次を示しています。

  1. 現在のプロジェクト(繰り返し)番号
  2. スライドウィンドウの平均速度(例:10回の繰り返し平均)
  3. 全体平均比
  4. これまでの進行率(灌漑回数を知っている場合)

出力:

317:  window: 3.76/s   overall: 3.28/s   progress: 31.7%

テストファイル:

set test_file
# create a test file
for i in {1..1000} ;do echo $i; done >"$1"

スクリプト:

if="$1"             # the input file
lct=$(wc -l <"$if") # number of lines in input file
tot=${lct:-0}       # total number of itterations; If unknown, default is 0
                    #+  The total is know in this case. '$tot' is just a rough 
                    #+  example of how to suppress the progress %age output
beg=$(date +%s.%N)  # starting unix time.%N is nanoseconds (a GNU extension)
swx=10              # keep a sliding window of max 'n' itteratons (to average)
unset sw            # an array of the last '$swx' rates
for i in $(seq 1 $lct) ;do
    sw[$i]=$(date +%s.%N)  # sliding window start time
    # ================                   
      sleep .$RANDOM       #  ... process something here
    # ================
    now=$(date +%s.%N)     # current unix time
    if ((i<=swx)) ;then
        sw1=1              # first element of sliding window  
        sww=$i             # sliding window width (starts from 1)
    else
        sw1=$((i-swx+1))
        sww=$swx        
    fi
    bc=($(bc <<<"scale=2; $i/($now-$beg); $sww/($now-${sw[$sw1]})"))
    oavg=${bc[0]}                  # overall average rate
    swhz=${bc[1]}                  # sliding window rate
    ((i>swx)) && unset sw[$sw1-1]  # remove old entry from sliding window list
    ((tot==0)) && pc= || pc="progress: $(bc <<<"scale=1; x=($i*100)/$tot; if (x<1) print 0; x")%"
    msg="$i:  window: $swhz/s   overall: $oavg/s   $pc"
    printf "\r%"$((${#i}+1))"s=\r%s" "" "$msg"
done

おすすめ記事