10 Eylül 2015 Perşembe

How to do progress indicator using bash



  Sometimes when our machine is doing job for longer time period, we need some methods to make sure the end user being keep updated. One such example I can think off is when  we are  running our system backup script.The reason why we need such application is to make sure the end user feel content and not thinking that the script they are running are  hung or inactive.It is important for the script to do their job until finish without being interrupted .

Few example that I can think of for the progress indicator are as below

Dots
Rotating Line
Progress Counter counting to 100%

It must be highlighted that all this application are running in a loop(for,while,until)  and it should be use as a function in your script.Below are example of progress indicator script that you can try to play around

Dot

#!/bin/bash

 INT=1                                #declaring the variable


while true
do
     echo -n "." 
     sleep $INT 
done





 Rotating Line

#!/bin/bash

CHA=( "-" "\\" "|" "/" )
INT=1             
CNT=0              

while true                             #While function as controlling loop
do

    loc=$(($CNT % 4))            #using the modulo expression for arithmetic fn 

    echo -en "\b${CHA[$loc]}"  #check this link for bash arrays info.
    CNT=$(($CNT + 1))            #Increasing the CouNTer
    sleep $INT

done



 Increasing Progress indicator

#!/bin/bash

LIST="1 2 3 4 5 6 7 8 9 10"             #Note that there is a different when we
INT=1                                          #use (),"",'' .Maybe Ill explain in diff. post 
CNT=0               

for NIM in ${LIST}                         #For function  as controlling loop
do
   
    len=$(echo ${LIST} | wc -w)             #"wc"command is used print new line
    echo -en "\b\b\b$(($NIM*100/$len))%"
    sleep $INT
   

done



That's it for now. click here to download the script.

Hiç yorum yok:

Yorum Gönder