-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogress-bar.sh
53 lines (41 loc) · 1.29 KB
/
progress-bar.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/bash
# Autor: Dotan Barak
# De: TLDP.org
BAR_WIDTH=50
#BAR_CHAR_START="["
#BAR_CHAR_END="]"
BAR_CHAR_EMPTY="▒"
BAR_CHAR_FULL="█"
BRACKET_CHARS=2
LIMIT=100
print_progress_bar(){
# Calculate how many characters will be full
let "full_limit = ((($1 - $BRACKET_CHARS) * $2) / $LIMIT)"
# Calculate how many characters will be empty
let "empty_limit = ($1 - $BRACKET_CHARS) - ${full_limit}"
# Prepare the bar
bar_line="${BAR_CHAR_START}"
for ((j=0; j<full_limit; j++)); do
bar_line="${bar_line}${BAR_CHAR_FULL}"
done
for ((j=0; j<empty_limit; j++)); do
bar_line="${bar_line}${BAR_CHAR_EMPTY}"
done
bar_line="${bar_line}${BAR_CHAR_END}"
printf "%3d%% %s" $2 ${bar_line}
}
TEMPO="$1"
echo -e "Tempo $TEMPO s"
# Here is a sample of code that uses it
MAX_PERCENT=100
for ((i=0; i<=MAX_PERCENT; i++)); do
sleep "$TEMPO" 2>/dev/null
print_progress_bar ${BAR_WIDTH} ${i}
echo -en "\r" # A magia acontece aqui, impedindo quebra de linha
done
exit
#########################################################################
# One liner simple version:
for X_ in $(seq 1 100); do echo -ne "$X_%\033[0K\r"; sleep 1; done
# \033[0K == Delete to the end of the line (in case your progress line gets shorter at some point)
# \033[K after the "\r" (\033[K works fine as well)