A
A
Andrej Kopp2021-04-14 13:46:18
bash
Andrej Kopp, 2021-04-14 13:46:18

Is there a bash script with a progress bar?

Hello. I'm starting to learn Bash. Already wrote a couple of functions for configuring and installing packages. I would like to visualize the installation process, I found many examples of progress bars on the Internet, for example, this https://github.com/edouard-lopez/progress-bar.sh , but I don’t understand how to bind it to such a function:

#!/bin/bash
set -e

CURRENT_DIR=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
source ${CURRENT_DIR}/common/common.sh
source ${CURRENT_DIR}/common/progress-bar.sh

[ $(id -u) != "0" ] && { ansi -n --bold --bg-red "Выполните этот скрипт с учетной записью root"; exit 1; }

MYSQL_ROOT_PASSWORD=`random_string`

progress-bar() {
  local duration=${1}
    already_done() { for ((done=0; done<$elapsed; done++)); do printf "▇"; done }
    remaining() { for ((remain=$elapsed; remain<$duration; remain++)); do printf " "; done }
    percentage() { printf "| %s%%" $(( (($elapsed)*100)/($duration)*100/100 )); }
    clean_line() { printf "\r"; }

  for (( elapsed=1; elapsed<=$duration; elapsed++ )); do
      already_done; remaining; percentage
      sleep 1
      clean_line
  done
  clean_line
}

function init_system {
    export LC_ALL="en_US.UTF-8"
    echo "LC_ALL=en_US.UTF-8" >> /etc/default/locale
    locale-gen en_US.UTF-8
  locale-gen de_DE.UTF-8
    locale-gen ru_RU.UTF-8

    ln -sf /usr/share/zoneinfo/Europa/Berlin /etc/localtime
  
  mkdir -p /opt/source/php
  mkdir -p /opt/php/

    apt upgrade && apt update
    apt install -y software-properties-common

    init_alias
}

function init_alias {
    alias sudowww > /dev/null 2>&1 || {
        echo "alias sudowww='sudo -H -u ${WWW_USER} sh -c'" >> ~/.bash_aliases
    }
}

call_function init_system "Инициализация системы." ${LOG_PATH}


It is necessary that the value "Initializing the system." and there was a progress bar nearby. Should look something like this:

===> "Initializing the system." [====____] 50%

And after the function ended, it displayed ===> "Initializing the system." [Completed]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
ge, 2021-04-16
@gedev

Actually, what does not suit the output of the package manager? He writes the progress of installing packages in some detail. The log will crawl and show that something is moving =). And your config setup is unlikely to take more than a second in total (this is if you consider that there will be many regular expressions that will take a long time to complete).
He wrote such scripts himself, do not fasten the progress bar beautifully - there are many heterogeneous processes, there is not one thing by which progress could be tracked.
Just highlight your lines with color so that they are visible in the log stream. For example, like this:

G="\e[32m" # green
N="\e[0m"  # no color
echo -e "${G}Инициализация системы   ...   [OK]${N}"

I also tried to screw spinners (just spins and shows that "it's alive"):
show_spinner() {
    chars="/-\|*"
    while pgrep "$1" > /dev/null; do
        for (( i=0; i<${#chars}; i++ )); do
            sleep 0.2
            echo -en "${chars:$i:1} ${G}Обработка...${N}" "\r"
        done
    done
}

# show_spinner <тут имя процесса, за которым надо следить>
# Например, за find:
show_spinner find

V
Viktor Taran, 2021-04-14
@shambler81

It all depends on what you are using the progress bar
for, for example, you are going to hang it for archiving,
then there will be a gap in what is actually counted in it, you yourself understand that you can calculate the time only approximately, moreover, strongly approximately, and if it’s for sure then by the veil during this time you can have time to archive the entire file.
So here you have to at best calculate the progress in the files that got into the archive and build progress based on this.
True, for this you will have to count the number of files in the directory, while excluding those directories that you excluded during archiving.
and build the progress of archiving by the number of files in the archive.
This can be done via pv
Some utilities have their own progress bar.
In general, the problem is what actually count. in progress. and what to take as a unit.
If you need progress in the format that there is movement, look, I think
Then you can make it easier to check the process for completion once a second and set for example .......
In general, write what you are going to specifically progress;)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question