-
Notifications
You must be signed in to change notification settings - Fork 0
/
speed
executable file
·66 lines (61 loc) · 1.62 KB
/
speed
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
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/sh
# vi:et lbr noet sw=2 ts=2 tw=79 wrap
# Copyright 2020-2024 David Rabkin
# The script uses local variables which are not POSIX but supported by most
# shells. See:
# https://stackoverflow.com/q/18597697
# shellcheck disable=SC3043 # Uses local variables.
# shellcheck disable=SC1091,SC2034 # File not following, appears unused.
set -- "$@" --quiet
readonly \
BASE_APP_VERSION=0.9.20240301 \
BASE_MIN_VERSION=0.9.20230505
. base.sh
# Reports download and upload internet speeds in a loop.
main() {
validate_cmd gawk speedtest-cli
# The index starts with 0, it guarantees the title printing from the begging.
local i=0
while :; do
[ $((i % 30)) -eq 0 ] && title
test_speed
sleep 30
i=$((i + 1))
done
}
# Prints current download and upload speeds. The function is ran in a loop,
# rests in case of an error.
test_speed() {
url_exists google.com || {
loge Check internet connection.
return 0
}
local down host out up
out="$(speedtest-cli 2>&1)" || {
loge "$out"
return 0
}
down="$(printf %s "$out" | grep -E '^Download: ' | gawk '{print $2}')" || {
loge "$out: $down: down."
return 0
}
up="$(printf %s "$out" | grep -E '^Upload: ' | gawk '{print $2}')" || {
loge "$out: $up: up."
return 0
}
host="$(printf %s "$out" | grep -E '^Hosted by ' | cut -c11-)" || {
loge "$out: $host: host."
return 0
}
tsout "| $(printf %6s "$down") | $(printf %6s "$up") | $host"
}
# Prints top title.
title() {
cat <<-EOM
------------------+--------+------- +----------------
Timestamp | Down | Up | Host By
------------------+--------+------- +----------------
EOM
}
# Starting point.
main "$@"