残り時間... ならぬ終了予定時刻を出力するシェルスクリプト
私のシェルスクリプトの書く傾向として、先にフォルダを作成して、残りの処理数をフォルダ数で示す物が多い。 残りのフォルダ数の増減によって、残り時間を出力するプログレスバーがあれば良いな。と思ったのだが、なかったので作る事にした。
パイプ等で現在のカウントを出力する物ではなく、argvにワンライナーシェルを入力し、そこから0までの時間を表示するものにした。
./progress.sh 10s "find folder -mindepth 2 -type d | wc -l"
こんな感じで、処理できる物を作ろうと考えた。
でできた物がこちら。
#!/bin/bash if [ $# -ne 2 ]; then echo "$0 10s \"find . -type f | wc -l\"" 1>&2 echo "9999...0 countdown progress" 1>&2 echo "sleep 10s; command" 1>&2 echo "bash -c \"find . -type d | wc -l\" == num .. countdown you must this 0" 1>&2 exit 1 fi START_TIME=$(date +%s); START_COUNT=$(bash -c "$2"); clear; while [ ! $(bash -c "$2") -eq 0 ]; do # now count=$(bash -c "$2"); time=$(date +%s); # percentage # 0% to not work. if [ $START_COUNT -eq $count ]; then percent=0; remain_time=0; else total_end_count=$(($START_COUNT - $count)); percent=$(echo "scale=5; $total_end_count / $START_COUNT * 100" | bc); remain_time=$(echo "scale=5; ($time - $START_TIME) / $total_end_count * $count" | bc -l); remain_sec=$(echo $remain_time | sed s/\.[0-9,]*$//g); fi # remaining time echo "$percent% $remain_time s $(date --date='TZ="Asia/Tokyo" '$remain_sec' sec')"; sleep $1; done
残り秒数まで出力できたが、残り秒数を時間やday表示にするより、dateコマンドの+秒後の方が楽にできたので、終了予定時刻を出力した。
右下の画面が実行画面。
大体、残り時間が6日かかるとわかった。