Skip to content

Commit 807aea9

Browse files
committed
try other mirror if download fails
1 parent e2a28d5 commit 807aea9

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ A lightweight script to install or update a TeamSpeak 3 server on Linux or FreeB
88
* tar
99
* bzip2
1010

11-
All other dependencies (awk, basename, cd, command, cut, dirname, echo, grep, mktemp, printf, read, sed, test, touch, uname, wc, and an sh-compatible shell) are installed by default on most systems.
11+
All other dependencies (basename, cd, command, cut, dirname, echo, grep, mktemp, printf, read, sed, test, touch, uname, wc, and an sh-compatible shell) are installed by default on most systems.
1212

1313
## How to use
1414
### Install a new TeamSpeak 3 server

ts3updater.sh

+20-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/sh
22
# Script Name: ts3updater.sh
33
# Author: eminga
4-
# Version: 1.3
4+
# Version: 1.4
55
# Description: Installs and updates TeamSpeak 3 servers
66
# License: MIT License
77

@@ -55,16 +55,28 @@ version=$(printf '%s' "$server" | jq -r '.version')
5555
if [ "$old_version" != "$version" ]; then
5656
echo "New version available: $version"
5757
checksum=$(printf '%s' "$server" | jq -r '.checksum')
58-
link=$(printf '%s' "$server" | jq -r '.mirrors | values[]')
58+
links=$(printf '%s' "$server" | jq -r '.mirrors | values[]')
5959

60-
# select random mirror
61-
i=$(printf '%s\n' "$link" | wc -l)
62-
i=$(awk "BEGIN{srand(); printf \"%d\",(rand()*$i) + 1}")
63-
link=$(printf '%s' "$link" | sed -n "$i"p)
60+
# order mirrors randomly
61+
if command -v shuf > /dev/null 2>&1; then
62+
links=$(printf '%s' "$links" | shuf)
63+
fi
6464

6565
tmpfile=$(mktemp)
66-
echo "Downloading the file $link"
67-
curl -Lo "$tmpfile" "$link"
66+
i=1
67+
n=$(printf '%s\n' "$links" | wc -l)
68+
69+
# try to download from mirrors until download is successful or all mirrors tried
70+
while [ "$i" -le "$n" ]; do
71+
link=$(printf '%s' "$links" | sed -n "$i"p)
72+
echo "Downloading the file $link"
73+
curl -Lo "$tmpfile" "$link"
74+
if [ $? = 0 ]; then
75+
i=$(( n + 1 ))
76+
else
77+
i=$(( i + 1 ))
78+
fi
79+
done
6880

6981
if command -v sha256sum > /dev/null 2>&1; then
7082
sha256=$(sha256sum "$tmpfile" | cut -b 1-64)

0 commit comments

Comments
 (0)