Created
June 7, 2019 15:18
-
-
Save mancap314/c1768a71b240009c33533faac64c1550 to your computer and use it in GitHub Desktop.
bash script for progression bar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Default values | |
OUTPUT_DIR="" | |
PREFIX="" | |
N_IMAGES=0 | |
INTERVAL=3 | |
while [[ ! $# -eq 0 ]]; do # looping through the arguments | |
case "$1" in | |
--interval | -i) | |
INTERVAL=$2 | |
;; | |
--n-images | -n) | |
N_IMAGES=$2 | |
;; | |
--output-dir | -o) | |
OUTPUT_DIR=$2 | |
;; | |
--prefix | -p) | |
# TODO: Check that if given, it is not empty | |
PREFIX=$2 | |
esac | |
shift | |
done | |
if [[ "${OUTPUT_DIR}" != "" && "${OUTPUT_DIR: -1}" != "/" ]]; then | |
OUTPUT_DIR="${OUTPUT_DIR}/" | |
fi | |
progress=0 | |
n_images_previous=0 | |
start_time=$( date +%s.%N ) | |
while (( $(echo "${progress} < 100" | bc -l) )); do | |
end_time=$( date +%s.%N ) | |
n_images_created="$(ls -1 ${OUTPUT_DIR}${PREFIX}*.png 2> /dev/null | wc -l)" | |
runtime=$( echo "${end_time} - ${start_time}" | bc -l ) | |
start_time=$( date +%s.%N ) # re-initialize `start_time` after `runtime` has been computed | |
progress=$(printf '%.3f\n' $(echo "${n_images_created} / ${N_IMAGES} * 100" | bc -l)) | |
progress_10=$(printf '%.3f\n' $(echo "${n_images_created} / ${N_IMAGES} * 10" | bc -l)) | |
n_images_tocreate=$((N_IMAGES - n_images_created)) | |
speed=$(printf '%.1f\n' $(echo "(${n_images_created} - ${n_images_previous}) / ${runtime}" | bc -l)) | |
n_images_previous=${n_images_created} # re-initialize `n_images_previous` for the next loop | |
if (( $(echo "${speed} == 0" | bc -l) )); then # to avoid division by zero | |
continue | |
fi | |
etl=$(printf '%.0f\n' $(echo "${n_images_tocreate} / ${speed}" | bc -l)) | |
etl_toprint="" | |
n_hours=$(printf '%.0f\n' $(echo "${etl} / 3600" | bc -l)) | |
if (( $(echo "${n_hours} >= 1" | bc -l) )); then | |
etl_toprint="${n_hours}h " | |
fi | |
n_min=$(printf '%.0f\n' $(echo "scale=0; (${etl} % 3600) / 60" | bc -l)) | |
if [[ ${n_min} -ge 1 || ${n_hours} -ge 1 ]]; then | |
etl_toprint="${etl_toprint}${n_min}min " | |
fi | |
n_sec=$(printf '%.0f\n' $(echo "scale=0; ${etl} % 60" | bc -l)) | |
etl_toprint="${etl_toprint}${n_sec}s" | |
pct="$(printf "%.*f\n" 2 ${progress})" | |
bar="" | |
for (( i=1; i<=10; i++ )); do | |
if (( $(echo "${i} <= ${progress_10}" | bc -l) )); then | |
bar="${bar}#" | |
else | |
bar="${bar} " | |
fi | |
done | |
bar="[${bar}]" | |
echo -ne "${bar} ${n_images_created}/${N_IMAGES} images created (${pct}%), ETL: ${etl_toprint}, speed: ${speed} images/s\r\c" | |
sleep ${INTERVAL} | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment