From 711f45cc7325806eec3a91891d3fe7ff49383e5e Mon Sep 17 00:00:00 2001 From: Janosch Schwalm Date: Mon, 20 Nov 2017 20:30:40 +0100 Subject: [PATCH 1/7] used shellcheck to find issues. https://github.com/koalaman/shellcheck fixed SC2128: Expanding an array without an index only gives the first element. https://github.com/koalaman/shellcheck/wiki/SC2128 --- fetchmirrors.sh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/fetchmirrors.sh b/fetchmirrors.sh index dd8bcec..9ba47fa 100755 --- a/fetchmirrors.sh +++ b/fetchmirrors.sh @@ -89,7 +89,7 @@ get_opts() { ;; -l|--list) # Display a list of country codes to the user echo "${Yellow}Country codes:${ColorOff}" - echo -e "$countries" | column -t + echo -e "${countries[@]}" | column -t echo "${Yellow}Note: Use only the upercase two character code in your command ex:${Green} $this -c US" echo "${Yellow}Or simply use:${Green} ${this}${ColorOff}" break @@ -103,7 +103,7 @@ get_opts() { else shift for i in $(echo "$@") ; do - if (echo "$countries" | grep -w "$i" &> /dev/null); then + if (echo "${countries[@]}" | grep -w "$i" &> /dev/null); then query+="https://www.archlinux.org/mirrorlist/?country=${i} " else echo "${Yellow}[${this}]${Red} Error: ${Yellow}country code: $2 not found." @@ -132,7 +132,7 @@ search() { while (true) do echo "${Green}Country codes:${ColorOff}" - echo -e "$countries" | column -t + echo -e "${countries[@]}" | column -t echo -n "${Yellow}Enter the number(s) or code(s) corresponding to your country ${Green}[4 9 US]${Yellow}:${ColorOff} " read code @@ -149,13 +149,13 @@ search() { break ;; [0-9]|[1-4][0-9]|[5][0-1]) - country_code=$(<<<"$countries" grep -o "$i.*" | awk 'NR==1 {print $2}') - country+=$(<<<"$countries" grep -o "$i.*" | awk 'NR==1 {print $3" "}' | sed 's/\\n.*/ /') + country_code=$(<<<"${countries[@]}" grep -o "$i.*" | awk 'NR==1 {print $2}') + country+=$(<<<"${countries[@]}" grep -o "$i.*" | awk 'NR==1 {print $3" "}' | sed 's/\\n.*/ /') query+="https://www.archlinux.org/mirrorlist/?country=${country_code} " ;; [A-Z][A-Z]) - if (<<<"$countries" grep -o "$i" &>/dev/null); then - country+=$(<<<"$countries" grep -o "$i.*" | awk 'NR==1 {print $2}' | sed 's/\\n.*/ /') + if (<<<"${countries[@]}" grep -o "$i" &>/dev/null); then + country+=$(<<<"${countries[@]}" grep -o "$i.*" | awk 'NR==1 {print $2}' | sed 's/\\n.*/ /') query+="https://www.archlinux.org/mirrorlist/?country=${i} " else echo "${Yellow}[${this}]${Red} Error: ${Yellow}invalid input [ $i ], select a number or code from the list.${ColorOff}" From 9bcb286efe026bbdb98e30905a343ad8c246bf6d Mon Sep 17 00:00:00 2001 From: Janosch Schwalm Date: Wed, 22 Nov 2017 18:32:31 +0100 Subject: [PATCH 2/7] used shellcheck to find issues. https://github.com/koalaman/shellcheck fixed SC2116: Useless echo? Instead of 'cmd $(echo foo)', just use 'cmd foo' https://github.com/koalaman/shellcheck/wiki/SC2116 query, code and country variable to an array changed case statement --- fetchmirrors.sh | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/fetchmirrors.sh b/fetchmirrors.sh index 9ba47fa..0458f41 100755 --- a/fetchmirrors.sh +++ b/fetchmirrors.sh @@ -102,9 +102,9 @@ get_opts() { echo "${Yellow}[${this}]${Red} Error: ${Yellow}You must enter a country code." else shift - for i in $(echo "$@") ; do + for i in "$@" ; do if (echo "${countries[@]}" | grep -w "$i" &> /dev/null); then - query+="https://www.archlinux.org/mirrorlist/?country=${i} " + query+=("https://www.archlinux.org/mirrorlist/?country=${i}") else echo "${Yellow}[${this}]${Red} Error: ${Yellow}country code: $2 not found." echo "To view a list of country codes run:${Green} $this -l${ColorOff}" @@ -134,29 +134,29 @@ search() { echo "${Green}Country codes:${ColorOff}" echo -e "${countries[@]}" | column -t echo -n "${Yellow}Enter the number(s) or code(s) corresponding to your country ${Green}[4 9 US]${Yellow}:${ColorOff} " - read code + read -a code - for i in $(echo "$code") ; do + for i in "${code[@]}" ; do case "$i" in - 1) # Set query to all mirrors + 1|AM) # Set query to all mirrors country="All" - query="https://www.archlinux.org/mirrorlist/all/" + query=("https://www.archlinux.org/mirrorlist/all/") break ;; - 2) # Set query to all https mirrors + 2|AS) # Set query to all https mirrors country="All HTTPS" - query="https://www.archlinux.org/mirrorlist/all/https/" + query=("https://www.archlinux.org/mirrorlist/all/https/") break ;; [0-9]|[1-4][0-9]|[5][0-1]) country_code=$(<<<"${countries[@]}" grep -o "$i.*" | awk 'NR==1 {print $2}') - country+=$(<<<"${countries[@]}" grep -o "$i.*" | awk 'NR==1 {print $3" "}' | sed 's/\\n.*/ /') - query+="https://www.archlinux.org/mirrorlist/?country=${country_code} " + country+=($(<<<"${countries[@]}" grep -o "$i.*" | awk 'NR==1 {print $3" "}' | sed 's/\\n.*/ /')) + query+=("https://www.archlinux.org/mirrorlist/?country=${country_code}") ;; - [A-Z][A-Z]) + [[:upper:]][[:upper:]]) if (<<<"${countries[@]}" grep -o "$i" &>/dev/null); then - country+=$(<<<"${countries[@]}" grep -o "$i.*" | awk 'NR==1 {print $2}' | sed 's/\\n.*/ /') - query+="https://www.archlinux.org/mirrorlist/?country=${i} " + country+=($(<<<"${countries[@]}" grep -o "$i.*" | awk 'NR==1 {print $2}' | sed 's/\\n.*/ /')) + query+=("https://www.archlinux.org/mirrorlist/?country=${i}") else echo "${Yellow}[${this}]${Red} Error: ${Yellow}invalid input [ $i ], select a number or code from the list.${ColorOff}" err=true @@ -180,7 +180,7 @@ search() { done if "$confirm" ; then - echo -en "\n${Yellow}You have selected the countries:${Green} $country${Yellow}- is this correct ${Green}[Y/n]:${ColorOff} " + echo -en "\n${Yellow}You have selected the countries:${Green} ${country[*]} ${Yellow}- is this correct ${Green}[Y/n]:${ColorOff} " read input case "$input" in @@ -200,7 +200,7 @@ search() { get_list() { echo - for list in $(echo "$query") ; do + for list in "${query[@]}" ; do if (curl -s "$list" | grep "Server" &>/dev/null); then echo "${Yellow}Fetching new mirrorlist from:${Green} ${list}${ColorOff}" curl -s "$list" | sed '1,4d' >> /tmp/mirrorlist @@ -214,7 +214,7 @@ get_list() { done sed -i 's/#//' /tmp/mirrorlist - echo "${Yellow}Please wait while ranking${Green} $country${Yellow}mirrors...${ColorOff}" + echo "${Yellow}Please wait while ranking${Green} ${country[*]} ${Yellow}mirrors...${ColorOff}" rankmirrors -n "$rank_int" /tmp/mirrorlist > /tmp/mirrorlist.ranked if [ "$?" -gt "0" ]; then From 64fab4648e931cf438ad8d31641069adb9429cb9 Mon Sep 17 00:00:00 2001 From: Janosch Schwalm Date: Wed, 22 Nov 2017 20:39:08 +0100 Subject: [PATCH 3/7] updated countries from https://www.archlinux.org/mirrorlist/ --- fetchmirrors.sh | 87 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 85 insertions(+), 2 deletions(-) diff --git a/fetchmirrors.sh b/fetchmirrors.sh index 0458f41..ecc8899 100755 --- a/fetchmirrors.sh +++ b/fetchmirrors.sh @@ -51,8 +51,91 @@ get_opts() { Magenta=$'\e[0;35m'; ColorOff=$'\e[0m'; # frmt_countries=( "${Magenta}1) ${Green}AM ${Yellow}All-Mirrors - ${Magenta}2) ${Green}AS ${Yellow}All-Https - ${Magenta}3) ${Green}AT ${Yellow}Austria\n${Magenta}4) ${Green}AU ${Yellow}Australia - ${Magenta}5) ${Green}BE ${Yellow}Belgium - ${Magenta}6) ${Green}BG ${Yellow}Bulgaria\n${Magenta}7) ${Green}BR ${Yellow}Brazil - ${Magenta}8) ${Green}BY ${Yellow}Belarus - ${Magenta}9) ${Green}CA ${Yellow}Canada\n${Magenta}10) ${Green}CL ${Yellow}Chile - ${Magenta}11) ${Green}CN ${Yellow}China - ${Magenta}12) ${Green}CO ${Yellow}Columbia\n${Magenta}13) ${Green}CZ ${Yellow}Czech-Republic - ${Magenta}14) ${Green}DE ${Yellow}Germany - ${Magenta}15) ${Green}DK ${Yellow}Denmark\n${Magenta}16) ${Green}EE ${Yellow}Estonia - ${Magenta}17) ${Green}ES ${Yellow}Spain - ${Magenta}18) ${Green}FI ${Yellow}Finland\n${Magenta}19) ${Green}FR ${Yellow}France - ${Magenta}20) ${Green}GB ${Yellow}United-Kingdom - ${Magenta}21) ${Green}HU ${Yellow}Hungary\n${Magenta}22) ${Green}IE ${Yellow}Ireland - ${Magenta}23) ${Green}IL ${Yellow}Isreal - ${Magenta}24) ${Green}IN ${Yellow}India\n${Magenta}25) ${Green}IT ${Yellow}Italy - ${Magenta}26) ${Green}JP ${Yellow}Japan - ${Magenta}27) ${Green}KR ${Yellow}Korea\n${Magenta}28) ${Green}KZ ${Yellow}Kazakhstan - ${Magenta}29) ${Green}LK ${Yellow}Sri-Lanka - ${Magenta}30) ${Green}LU ${Yellow}Luxembourg\n${Magenta}31) ${Green}LV ${Yellow}Lativia - ${Magenta}32) ${Green}MK ${Yellow}Macedonia - ${Magenta}33) ${Green}NC ${Yellow}New-Caledonia\n${Magenta}34) ${Green}NL ${Yellow}Netherlands - ${Magenta}35) ${Green}NO ${Yellow}Norway - ${Magenta}36) ${Green}NZ ${Yellow}New-Zealand\n${Magenta}37) ${Green}PL ${Yellow}Poland - ${Magenta}38) ${Green}PT ${Yellow}Portugal - ${Magenta}39) ${Green}RO ${Yellow}Romania\n${Magenta}40) ${Green}RS ${Yellow}Serbia - ${Magenta}41) ${Green}RU ${Yellow}Russia - ${Magenta}42) ${Green}SE ${Yellow}Sweden\n${Magenta}43) ${Green}SG ${Yellow}Singapore - ${Magenta}44) ${Green}SK ${Yellow}Slovakia - ${Magenta}45) ${Green}TR ${Yellow}Turkey\n${Magenta}46) ${Green}TW ${Yellow}Taiwan - ${Magenta}47) ${Green}UA ${Yellow}Ukraine - ${Magenta}48) ${Green}US ${Yellow}United-States\n${Magenta}49) ${Green}UZ ${Yellow}Uzbekistan - ${Magenta}50) ${Green}VN ${Yellow}Viet-Nam - ${Magenta}51) ${Green}ZA ${Yellow}South-Africa" ) - countries=( "1) AM All-Mirrors - 2) AS All-Https - 3) AT Austria\n4) AU Australia - 5) BE Belgium - 6) BG Bulgaria\n7) BR Brazil - 8) BY Belarus - 9) CA Canada\n10) CL Chile - 11) CN China - 12) CO Columbia\n13) CZ Czech-Republic - 14) DE Germany - 15) DK Denmark\n16) EE Estonia - 17) ES Spain - 18) FI Finland\n19) FR France - 20) GB United-Kingdom - 21) HU Hungary\n22) IE Ireland - 23) IL Isreal - 24) IN India\n25) IT Italy - 26) JP Japan - 27) KR Korea\n28) KZ Kazakhstan - 29) LK Sri-Lanka - 30) LU Luxembourg\n31) LV Lativia - 32) MK Macedonia - 33) NC New-Caledonia\n34) NL Netherlands - 35) NO Norway - 36) NZ New-Zealand\n37) PL Poland - 38) PT Portugal - 39) RO Romania\n40) RS Serbia - 41) RU Russia - 42) SE Sweden\n43) SG Singapore - 44) SK Slovakia - 45) TR Turkey\n46) TW Taiwan - 47) UA Ukraine - 48) US United-States\n49) UZ Uzbekistan - 50) VN Viet-Nam - 51) ZA South-Africa" ) - + #countries=( "1) AM All-Mirrors - 2) AS All-Https - 3) AT Austria\n4) AU Australia - 5) BE Belgium - 6) BG Bulgaria\n7) BR Brazil - 8) BY Belarus - 9) CA Canada\n10) CL Chile - 11) CN China - 12) CO Columbia\n13) CZ Czech-Republic - 14) DE Germany - 15) DK Denmark\n16) EE Estonia - 17) ES Spain - 18) FI Finland\n19) FR France - 20) GB United-Kingdom - 21) HU Hungary\n22) IE Ireland - 23) IL Isreal - 24) IN India\n25) IT Italy - 26) JP Japan - 27) KR Korea\n28) KZ Kazakhstan - 29) LK Sri-Lanka - 30) LU Luxembourg\n31) LV Lativia - 32) MK Macedonia - 33) NC New-Caledonia\n34) NL Netherlands - 35) NO Norway - 36) NZ New-Zealand\n37) PL Poland - 38) PT Portugal - 39) RO Romania\n40) RS Serbia - 41) RU Russia - 42) SE Sweden\n43) SG Singapore - 44) SK Slovakia - 45) TR Turkey\n46) TW Taiwan - 47) UA Ukraine - 48) US United-States\n49) UZ Uzbekistan - 50) VN Viet-Nam - 51) ZA South-Africa" ) + countries+=("1) AM All-Mirrors") + countries+=("2) AS All-Https") + countries+=("3) AT Austria") + countries+=("\n") + countries+=("4) AU Australia") + countries+=("5) BA Bosnia-and-Herzegovina") + countries+=("6) BE Belgium") + countries+=("\n") + countries+=("7) BG Bulgaria") + countries+=("8) BR Brazil") + countries+=("9) BY Belarus") + countries+=("\n") + countries+=("10) CA Canada") + countries+=("11) CH Switzerland") + countries+=("12) CL Chile") + countries+=("\n") + countries+=("13) CN China") + countries+=("14) CO Colombia") + countries+=("15) CZ Czech-Republic") + countries+=("\n") + countries+=("16) DE Germany") + countries+=("17) DK Denmark") + countries+=("18) EC Ecuador") + countries+=("\n") + countries+=("19) ES Spain") + countries+=("20) FI Finland") + countries+=("21) FR France") + countries+=("\n") + countries+=("22) GB United-Kingdom") + countries+=("23) GR Greece") + countries+=("24) HK Hong-Kong") + countries+=("\n") + countries+=("25) HR Croatia") + countries+=("26) HU Hungary") + countries+=("27) ID Indonesia") + countries+=("\n") + countries+=("28) IE Ireland") + countries+=("29) IL Israel") + countries+=("30) IN India") + countries+=("\n") + countries+=("31) IR Iran") + countries+=("32) IS Iceland") + countries+=("33) IT Italy") + countries+=("\n") + countries+=("34) JP Japan") + countries+=("35) KR South-Korea") + countries+=("36) KZ Kazakhstan") + countries+=("\n") + countries+=("37) LT Lithuania") + countries+=("38) LU Luxembourg") + countries+=("39) LV Latvia") + countries+=("\n") + countries+=("40) MK Macedonia") + countries+=("41) MX Mexico") + countries+=("42) NC New-Caledonia") + countries+=("\n") + countries+=("43) NL Netherlands") + countries+=("44) NO Norway") + countries+=("45) NZ New-Zealand") + countries+=("\n") + countries+=("46) PH Philippines") + countries+=("47) PL Poland") + countries+=("48) PT Portugal") + countries+=("\n") + countries+=("49) QA Qatar") + countries+=("50) RO Romania") + countries+=("51) RS Serbia") + countries+=("\n") + countries+=("52) RU Russia") + countries+=("53) SE Sweden") + countries+=("54) SG Singapore") + countries+=("\n") + countries+=("55) SI Slovenia") + countries+=("56) SK Slovakia") + countries+=("57) TH Thailand") + countries+=("\n") + countries+=("58) TR Turkey") + countries+=("59) TW Taiwan") + countries+=("60) UA Ukraine") + countries+=("\n") + countries+=("61) US United-States") + countries+=("62) VN Vietnam") + countries+=("63) ZA South-Africa") + countries+=("\n") trap ctrl_c INT if [ -z "$1" ] && [ "$UID" -ne "0" ]; then From 6826b27420f99aeeacb4f22757460d353cd47d2b Mon Sep 17 00:00:00 2001 From: Janosch Schwalm Date: Wed, 22 Nov 2017 20:44:02 +0100 Subject: [PATCH 4/7] shellcheck fixed SC2162: read without -r will mangle backslashes. https://github.com/koalaman/shellcheck/wiki/SC2162 --- fetchmirrors.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fetchmirrors.sh b/fetchmirrors.sh index ecc8899..2c62da8 100755 --- a/fetchmirrors.sh +++ b/fetchmirrors.sh @@ -217,7 +217,7 @@ search() { echo "${Green}Country codes:${ColorOff}" echo -e "${countries[@]}" | column -t echo -n "${Yellow}Enter the number(s) or code(s) corresponding to your country ${Green}[4 9 US]${Yellow}:${ColorOff} " - read -a code + read -ra code for i in "${code[@]}" ; do case "$i" in @@ -264,7 +264,7 @@ search() { if "$confirm" ; then echo -en "\n${Yellow}You have selected the countries:${Green} ${country[*]} ${Yellow}- is this correct ${Green}[Y/n]:${ColorOff} " - read input + read -r input case "$input" in y|Y|yes|Yes|yY|Yy|yy|YY|"") @@ -306,7 +306,7 @@ get_list() { exit 1 elif "$confirm" ; then echo -en "\n${Yellow}Would you like to view new mirrorlist? ${Green}[Y/n]: ${ColorOff}" - read input + read -r input case "$input" in y|Y|yes|Yes|yY|Yy|yy|YY|"") @@ -315,7 +315,7 @@ get_list() { esac echo -en "\n${Yellow}Would you like to install the new mirrorlist backing up existing? ${Green}[Y/n]:${ColorOff} " - read input + read -r input else input="" fi From 8a9f29562556ff31c240cb948e2301322a353c17 Mon Sep 17 00:00:00 2001 From: Janosch Schwalm Date: Wed, 22 Nov 2017 20:56:36 +0100 Subject: [PATCH 5/7] shellcheck fixed SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?. https://github.com/koalaman/shellcheck/wiki/SC2181 --- fetchmirrors.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/fetchmirrors.sh b/fetchmirrors.sh index 2c62da8..e7ff2bf 100755 --- a/fetchmirrors.sh +++ b/fetchmirrors.sh @@ -298,9 +298,8 @@ get_list() { sed -i 's/#//' /tmp/mirrorlist echo "${Yellow}Please wait while ranking${Green} ${country[*]} ${Yellow}mirrors...${ColorOff}" - rankmirrors -n "$rank_int" /tmp/mirrorlist > /tmp/mirrorlist.ranked - if [ "$?" -gt "0" ]; then + if ! rankmirrors -n "$rank_int" /tmp/mirrorlist > /tmp/mirrorlist.ranked ; then echo "${Yellow}[${this}]${Red} Error: ${Yellow}an error occured in ranking mirrorlist exiting..." rm /tmp/{mirrorlist,mirrorlist.ranked} &> /dev/null exit 1 From 4e8540e2c704abfed57691e8e76d6436380acec5 Mon Sep 17 00:00:00 2001 From: Janosch Schwalm Date: Mon, 27 Nov 2017 22:13:49 +0100 Subject: [PATCH 6/7] added support for 'protocol' parameter added support for 'ip_version' parameter added support for 'use_mirror_status' parameter --- fetchmirrors.sh | 51 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/fetchmirrors.sh b/fetchmirrors.sh index e7ff2bf..37ae473 100755 --- a/fetchmirrors.sh +++ b/fetchmirrors.sh @@ -35,15 +35,24 @@ usage() { echo "${Yellow} Number of servers to rank (default 6)" echo "${Green} -v --verbose" echo "${Yellow} Verbose output" + echo "${Green} -p --protocol" + echo "${Yellow} the protocol to use either http or https, default is empty (both)" + echo "${Green} -i --ip-version" + echo "${Yellow} the ip version to use either 4 or 6, default is empty (both)" + echo "${Green} -u --use-mirror-status" + echo "${Yellow} use mirror status" echo echo "${Yellow} Use${Green} $this ${Yellow}without any option to prompt for country code(s)${ColorOff}" } get_opts() { - this=${0##*/} # Set 'this', 'rank_int', 'confirm', 'countries', and color variables + this=${0##*/} # Set 'this', 'rank_int', 'confirm', 'countries', 'protocol', 'ip_version', 'use_mirror_status' and color variables rank_int="6" confirm=true + protocol=() + ip_version=() + use_mirror_status="" err=false Green=$'\e[0;32m'; Yellow=$'\e[0;33m'; @@ -198,6 +207,29 @@ get_opts() { fi break ;; + -p|--protocol) # Set protocol eg. http or https + if [ "$2" != "https" ] && [ "$2" != "http" ] ; then + echo "${Yellow}[${this}]${Red} Error: ${Yellow} invalid protocol use https or http or both ${ColorOff}" + exit 1 + fi + protocol+=("$2"); + # remove duplicates, in case of "-p https -p https --protocol https" + protocol=($(echo "${protocol[@]}" | tr ' ' '\n' | sort -u)) + shift ; shift ; + ;; + -i|--ip-version) # Set ip version eg. 4 or 6 + if [ "$2" -ne "4" ] && [ "$2" -ne "6" ] ; then + echo "${Yellow}[${this}]${Red} Error: ${Yellow} invalid ip_version use 4 or 6 or both ${ColorOff}" + exit 1 + fi + ip_version+=("$2"); + # remove duplicates, in case of "-i 4 -i 4 --ip-version 4" + ip_version=($(echo "${ip_version[@]}" | tr ' ' '\n' | sort -u)) + shift ; shift ; + ;; + -u|--use-mirror-status) # use mirror status + use_mirror_status="&use_mirror_status=on" ; shift + ;; "") # Empty 1 parameter means search for country code search ; break ;; @@ -211,7 +243,18 @@ get_opts() { } search() { - + # build query string for protocol + protocolQueryString=""; + for p in "${protocol[@]}"; + do + protocolQueryString+="&protocol=$p"; + done + # build query string for ip_version + ip_versionQueryString=""; + for i in "${ip_version[@]}" ; + do + ip_versionQueryString+="&ip_version=$i"; + done while (true) do echo "${Green}Country codes:${ColorOff}" @@ -234,12 +277,12 @@ search() { [0-9]|[1-4][0-9]|[5][0-1]) country_code=$(<<<"${countries[@]}" grep -o "$i.*" | awk 'NR==1 {print $2}') country+=($(<<<"${countries[@]}" grep -o "$i.*" | awk 'NR==1 {print $3" "}' | sed 's/\\n.*/ /')) - query+=("https://www.archlinux.org/mirrorlist/?country=${country_code}") + query+=("https://www.archlinux.org/mirrorlist/?country=${country_code}${protocolQueryString}${ip_versionQueryString}${use_mirror_status}") ;; [[:upper:]][[:upper:]]) if (<<<"${countries[@]}" grep -o "$i" &>/dev/null); then country+=($(<<<"${countries[@]}" grep -o "$i.*" | awk 'NR==1 {print $2}' | sed 's/\\n.*/ /')) - query+=("https://www.archlinux.org/mirrorlist/?country=${i}") + query+=("https://www.archlinux.org/mirrorlist/?country=${i}${protocolQueryString}${ip_versionQueryString}${use_mirror_status}") else echo "${Yellow}[${this}]${Red} Error: ${Yellow}invalid input [ $i ], select a number or code from the list.${ColorOff}" err=true From 057a848ec2ed98b075e733da1aaa422bf56b1def Mon Sep 17 00:00:00 2001 From: Janosch Schwalm Date: Wed, 29 Nov 2017 21:48:06 +0100 Subject: [PATCH 7/7] moved parameters to get_list function, because they dont change per request --- fetchmirrors.sh | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/fetchmirrors.sh b/fetchmirrors.sh index 37ae473..6c922d4 100755 --- a/fetchmirrors.sh +++ b/fetchmirrors.sh @@ -243,18 +243,6 @@ get_opts() { } search() { - # build query string for protocol - protocolQueryString=""; - for p in "${protocol[@]}"; - do - protocolQueryString+="&protocol=$p"; - done - # build query string for ip_version - ip_versionQueryString=""; - for i in "${ip_version[@]}" ; - do - ip_versionQueryString+="&ip_version=$i"; - done while (true) do echo "${Green}Country codes:${ColorOff}" @@ -277,12 +265,12 @@ search() { [0-9]|[1-4][0-9]|[5][0-1]) country_code=$(<<<"${countries[@]}" grep -o "$i.*" | awk 'NR==1 {print $2}') country+=($(<<<"${countries[@]}" grep -o "$i.*" | awk 'NR==1 {print $3" "}' | sed 's/\\n.*/ /')) - query+=("https://www.archlinux.org/mirrorlist/?country=${country_code}${protocolQueryString}${ip_versionQueryString}${use_mirror_status}") + query+=("https://www.archlinux.org/mirrorlist/?country=${country_code}") ;; [[:upper:]][[:upper:]]) if (<<<"${countries[@]}" grep -o "$i" &>/dev/null); then country+=($(<<<"${countries[@]}" grep -o "$i.*" | awk 'NR==1 {print $2}' | sed 's/\\n.*/ /')) - query+=("https://www.archlinux.org/mirrorlist/?country=${i}${protocolQueryString}${ip_versionQueryString}${use_mirror_status}") + query+=("https://www.archlinux.org/mirrorlist/?country=${i}") else echo "${Yellow}[${this}]${Red} Error: ${Yellow}invalid input [ $i ], select a number or code from the list.${ColorOff}" err=true @@ -325,8 +313,21 @@ search() { get_list() { + # build query string for protocol + protocolQueryString=""; + for p in "${protocol[@]}"; + do + protocolQueryString+="&protocol=$p"; + done + # build query string for ip_version + ip_versionQueryString=""; + for i in "${ip_version[@]}" ; + do + ip_versionQueryString+="&ip_version=$i"; + done echo for list in "${query[@]}" ; do + list+=${protocolQueryString}${ip_versionQueryString}${use_mirror_status} if (curl -s "$list" | grep "Server" &>/dev/null); then echo "${Yellow}Fetching new mirrorlist from:${Green} ${list}${ColorOff}" curl -s "$list" | sed '1,4d' >> /tmp/mirrorlist