Debian Codegraph @

progress-bar.sh

raw symbols SCIP
1 #!/bin/bash
2 # progress-bar.sh
3
4 # Author: Dotan Barak (very minor revisions by ABS Guide author).
5 # Used in ABS Guide with permission (thanks!).
6
7
8 BAR_WIDTH=50
9 BAR_CHAR_START="["
10 BAR_CHAR_END="]"
11 BAR_CHAR_EMPTY="."
12 BAR_CHAR_FULL="="
13 BRACKET_CHARS=2
14 LIMIT=100
15
16 print_progress_bar()
17 {
18 # Calculate how many characters will be full.
19 let "full_limit = ((($1 - $BRACKET_CHARS) * $2) / $LIMIT)"
20
21 # Calculate how many characters will be empty.
22 let "empty_limit = ($1 - $BRACKET_CHARS) - ${full_limit}"
23
24 # Prepare the bar.
25 bar_line="${BAR_CHAR_START}"
26 for ((j=0; j<full_limit; j++)); do
27 bar_line="${bar_line}${BAR_CHAR_FULL}"
28 done
29
30 for ((j=0; j<empty_limit; j++)); do
31 bar_line="${bar_line}${BAR_CHAR_EMPTY}"
32 done
33
34 bar_line="${bar_line}${BAR_CHAR_END}"
35
36 printf "%3d%% %s" $2 ${bar_line}
37 }
38
39 # Here is a sample of code that uses it.
40 MAX_PERCENT=100
41 for ((i=0; i<=MAX_PERCENT; i++)); do
42 #
43 usleep 10000
44 # ... Or run some other commands ...
45 #
46 print_progress_bar ${BAR_WIDTH} ${i}
47 echo -en "\r"
48 done
49
50 echo ""
51
52 exit