Progressbar as in linux

Linux + how to create simple Progress Bar in bash [duplicate]

I want to add a progress bar in my bash script that show the «.» character as progress , and process ended after MAX 180 second in the bash script I use curl command , so curl give the results after sometime but not greater then 180 sec something like that

|. after 2 sec |. after 60 sec |. after 100 sec |. after 150 sec |. | after 180 sec 

2 Answers 2

This is rather simple to do in just plain Bash:

#!/bin/bash # progress bar function prog() < local w=80 p=$1; shift # create a string of spaces, then change them to dots printf -v dots "%*s" "$(( $p*$w/100 ))" ""; dots=$; # print those dots on a fixed-width space plus the percentage etc. printf "\r\e[K|%-*s| %3d %% %s" "$w" "$dots" "$p" "$*"; > # test loop for x in ; do prog "$x" still working. sleep .1 # do some work here done ; echo 

The first argument to prog is the percentage, any others are printed after the progress bar. The variable w in the function controls the width of the bar. Print a newline after you’re done, the function doesn’t print one.

Another possibility would be to use the pv tool. It’s meant for measuring the throughput of a pipeline, but we can create one for it:

for x in ; do sleep .1 # do some work here printf . done | pv -pt -i0.2 -s100 -w 80 > /dev/null 

Here, -pt enables the progress bar and the timer, -s 100 sets the total output size, and whatever we print inside the function counts against that size.

Источник

Create the Progress Bar in Bash

When it is required to wait for a fixed amount of time during the execution of a script, it is better to create a progress bar to inform the user to wait for some time. The progress bar can be created using a simple Bash script or using some built-in Linux commands such as “cv”, “dialog”, etc. The methods of creating a progress bars using a Bash script are shown in this tutorial.

Different Examples of Creating a Progress Bar in Bash

The different ways of implementing a progress bar in Bash are shown in this part of the tutorial.

Example 1: Implement a Simple Progress Bar without Any Command

Create a Bash file with the following script that displays a progress bar using the “#” character and the “sleep” command. The “printf” command is used here to display the progress bar. The progress bar is divided into four parts. The 25% is displayed after 1 second. The 50% is displayed after 3 seconds. The 75% is displayed after 2 seconds. The 100% is displayed after 1 second.

printf » \n Wait to complete the task. \n \n «

Читайте также:  Password default linux ubuntu

#Print the first part of the progress bar

#Print the second part of the progress bar

#Print the third part of the progress bar

#Print the last of the progress bar

printf » \n \n Task completed. \n \n «

The following output appears after 1 second of executing the script:

The following output appears after 7 seconds of executing the script:

Example 2: Implement the Progress Bar Using the “Pv” Command

The full form of the “pv” command is “pipe viewer”. It is used to monitor the progress of the data that is passed through the pipe and display the progress bar based on the size of the data. This command is not installed by default in the system. Run the following command to install the “pv” command before practicing the script of this example:

You have to select a file of large size that is copied from one location to another location. Create a Bash file with the following script that copies the “test.txt” file from the current location to the “/home/fahmida/temp/” location. The “pv” command is used here to display the progress bar. The progress bar is displayed based on the size of the “test.txt” file.

echo «Copying file from one location to another location.»

#Copy the file to the destination

cat test.txt | pv -s $ ( stat -c % s test.txt ) > / home / fahmida / temp / test.txt

echo «File has been copied.»

The following output is displayed after completing the execution of the script:

Example 3: Implement the Progress Bar Using the “Dialog” Command

Another way of implementing a progress bar in Bash is using the “dialog” command. This command can be used to display a good looking progress bar in the terminal. Many types of widgets can be displayed using this progress bar. The task of the progress bar that is displayed by this command can be controlled by the Bash script. This progress bar is not installed in the system by default. Run the following command to install this progress bar in the system:

Create a Bash file with the following script that displays a progress bar using the “dialog” command. The task of copying the “/etc/passwd” file into the “/home/fahmida/tempdir” location is displayed using a progress bar. The progress bar is divided into five parts; each part is displayed after 2 seconds. The –title option is used in the “dialog” command to display the title of the progress bar. The –gauge option is used in the “dialog” command to display the progress bar with a height of 10 lines and a width of 100 characters. The “Waiting to complete the task” message is displayed above the progress bar.

#Show the current counter value

cp /etc/passwd to /home/fahmida/tempdir ( $current_pos%):

#Increment the counter by 20

#Terminate from the loop when the counter value is more than 100

[ $current_pos -gt 100 ] && break

#Wait for 2 seconds after each increment

) | dialog —title «Copying file. » —gauge «Waiting to complete the task» 10 100 0

The following output appears after 6 seconds of executing the script:

Читайте также:  Hibernate on linux mint

The following output appears after 10 seconds of executing the script:

Conclusion

The different ways of developing a progress bar using a Bash script are shown in this tutorial to help the Bash users use the progress bar on their program.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Источник

Create a progress bar in Bash

whiptail comes preinstalled on Ubuntu and many other distros, and will show full-screen (but still terminal-based) progress elements.

dialog is a superset of whiptail , so this example will work equally well with both. It does provide more advanced UI elements, so it may come in handy if you’re looking for user interaction such as file pickers and forms, but it has the disadvantage of not coming preinstalled on many systems.

whiptail

dialog

for i in $(seq 1 100) do sleep 0.1 echo $i done | whiptail --title 'Test script' --gauge 'Running. ' 6 60 0 

Note that the script output is interpreted as a percentage, so you may have to adjust your output accordingly.

Whiptail and Dialog also allow you to modify the text at run time via a rather cryptic syntax:

phases=( 'Locating Jebediah Kerman. ' 'Motivating Kerbals. ' 'Treating Kessler Syndrome. ' 'Recruiting Kerbals. ' ) for i in $(seq 1 100); do sleep 0.1 if [ $i -eq 100 ]; then echo -e "XXX\n100\nDone!\nXXX" elif [ $(($i % 25)) -eq 0 ]; then let "phase = $i / 25" echo -e "XXX\n$i\n$\nXXX" else echo $i fi done | whiptail --title 'Kerbal Space Program' --gauge "$" 6 60 0 

pv shows the progress of a file or stream being piped through it. It cannot however be (easily?) used to show progress of a custom operation such as a loop. It’s designed specifically for streams.

$ head -c 1G < /dev/urandom | pv -s 1G >/dev/null 277MB 0:00:16 [17.4MB/s] [========> ] 27% ETA 0:00:43 

Some real-world examples where pv comes in handy:

# progress while importing a DB dump pv mybigfile.sql | mysql -uroot -p dbname # importing straight from a remote server ssh user@server 'cat mybigfile.sql.gz' | pv | gzip -cd | mysql -uroot -p dbname # taking a snapshot of a btrfs partition btrfs send /snapshots/$date | pv | btrfs receive /mnt/backup/root 

I don’t know of any commands that give one-line progress bars in the style of pv or wget , but there are plenty of simple Bash/Perl/sed scripts that will add that functionality, as others have shared here.

To show the process of a loop with pv you can either make it look for the loop’s output or create some fake output, e.g. an echo in every iteration, pipe it to pv and give it the iteration count with -s . If it’s not wanted, remember to redirect the loop’s stdout to /dev/null . Here’s an example showing this approach.

You can use zenity to create simple GTK dialog windows. One of the available options is a progress bar dialog.

You create such a window using zenity —progress . To make it useful, you should specify more information by adding some of the options below (excerpt from man zenity ):

 Progress options --text=STRING Set the dialog text --percentage=INT Set initial percentage --auto-close Close dialog when 100% has been reached --auto-kill Kill parent process if cancel button is pressed --pulsate Pulsate progress bar --no-cancel Hides the cancel button 
  • pulsating: The progress bar is pulsating, it just indicates that something is running, but does not tell anything about the progress. You do this by setting the —pulsating option.
  • manual: You have to pipe the current progress percentage to the zenity command’s standard input to update the progress bar.
    An example for this could look like that below. Note that the previous commands are grouped to a subshell so that all the output is redirected to the zenity dialog and not just that of the last command:

(echo 10; sleep 2; echo 20; sleep 2; echo 50; sleep 2) | zenity --progress 

Sorry my dear , this is GUI progress bar windows , I want to create a progress bar in the terminal , for example i want to see it while the script is checking ==> [ ###########—————] 52%

Yes, I understand. It’s just that I already had written half of my answer when you said that, so I just decided to post it anyway in case somebody else might need it in the future. I hope you don’t mind, as there are quite a few terminal based solutions as well.

This code will do it, and doesn’t require anything (other than bash, of course). It prints # signs, like you asked in your comment:

pass='number1 number12 number13 number14 number15 number16' chk='number14' passarr=($pass) lenProgressBar=$ echo -n '[' i=0 while [ $i -lt $lenProgressBar ]; do echo -n '-' ((i++)) done echo -n ']' i=0 while [ $i -lt $lenProgressBar ]; do echo -e -n '\b' ((i++)) done echo -e -n '\b' for i in $pass ; do if [ "$i" = "$chk" ]; then echo -e '#\nFound ^_^' break else echo -n '#' fi done 

However, if you have a lot to check, this will just fill your screen with # signs. To fix that issue, try this code:

lenProgressBar=5 pass='number1 number12 number13 number14 number15 number16' chk='number14' passarr=($pass) lenPass=$ if [ $lenProgressBar -gt $lenPass ]; then lenProgressBar=lenPass elif [ $lenProgressBar -lt 1 ]; then lenProgressBar=1 fi let "chksForEqualsPrint = $lenPass / $lenProgressBar" echo -n '[' i=0 while [ $i -lt $lenProgressBar ]; do echo -n '-' ((i++)) done echo -n ']' i=0 while [ $i -lt $lenProgressBar ]; do echo -e -n '\b' ((i++)) done echo -e -n '\b' n=1 for i in $pass ; do if [ "$i" = "$chk" ]; then echo -e '\nFound ^_^' break else if [ $n -eq $chksForEqualsPrint ]; then echo -n '#' n=1 else ((n++)) fi fi done 

Change the 5 in the first line ( lenProgressBar=5 ) to the length that you want your progress bar to be. It’ll take longer to print a # sign with lower length progress bars than with higher length ones, but don’t let the length of the progress bar exceed your screen size; it won’t work well if you do. (It won’t let you use a progress bar higher than the number of items you’re checking or lower than 1)

Источник

Оцените статью
Adblock
detector