From 04262c5335799d7cae9ac76b800fb46216fd25ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Mah=C3=A9?= Date: Tue, 4 Jun 2019 11:51:08 +0200 Subject: [PATCH 1/2] shorter regex to get the timezone value first remove everything up to "timezone" included, then remove everything after the actual timezone value. In my opinion, this is easier to read, and it saves one subshell spawn by removing a pipe. Saves at least a millisecond :-) --- timezone.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/timezone.sh b/timezone.sh index 169a866..28b0781 100644 --- a/timezone.sh +++ b/timezone.sh @@ -22,7 +22,8 @@ # Version 1.1 - 2019-01-08 - Changed "http://ip-api.com/json/" to "http://www.ip-api.com/json/". # Version 1.0 - 2019-01-08 - First commit. -TIMEZONE="$(curl -sLf "http://www.ip-api.com/json/" | grep -o "\"timezone\" *: *\"[^\"]*" | grep -o "[^\"]*$")" +TIMEZONE="$(curl -sLf "http://www.ip-api.com/json/" | sed 's/^.*timezone":"// ; s/",.*$//')" + if echo "$TIMEZONE" | grep -q "/" then cp "/usr/share/zoneinfo/posix/$TIMEZONE" "/media/fat/linux/timezone" @@ -32,4 +33,4 @@ else echo "Unable to get" echo "your timezone." fi -exit 0 \ No newline at end of file +exit 0 From 11d08702e9b191a5c3d96e3b0a29f5e99656c3f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Mah=C3=A9?= Date: Thu, 18 Jun 2020 16:03:42 +0200 Subject: [PATCH 2/2] use a query to get the timezone (suggested by user vladc) whitespace cleaning, use herestring to avoid a pipe, protect variables with double-quotes and curly braces. --- timezone.sh | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/timezone.sh b/timezone.sh index 28b0781..a2deb52 100644 --- a/timezone.sh +++ b/timezone.sh @@ -18,19 +18,20 @@ # You can download the latest version of this script from: # https://github.com/MiSTer-devel/Scripts_MiSTer +# version 1.3 - 2020-06-18 - use a query to get the timezone, plus some cleaning # Version 1.2 - 2019-03-02 - Changed "/media/fat/timezone" to "/media/fat/linux/timezone", removed -k option from curl. # Version 1.1 - 2019-01-08 - Changed "http://ip-api.com/json/" to "http://www.ip-api.com/json/". # Version 1.0 - 2019-01-08 - First commit. -TIMEZONE="$(curl -sLf "http://www.ip-api.com/json/" | sed 's/^.*timezone":"// ; s/",.*$//')" +TIMEZONE="$(curl -sLf "http://www.ip-api.com/line?fields=timezone")" -if echo "$TIMEZONE" | grep -q "/" -then - cp "/usr/share/zoneinfo/posix/$TIMEZONE" "/media/fat/linux/timezone" - echo "Timezone set to" - echo "$TIMEZONE." +if grep -q "/" <<< "${TIMEZONE}" ; then + cp "/usr/share/zoneinfo/posix/${TIMEZONE}" "/media/fat/linux/timezone" + echo "Timezone set to" + echo "${TIMEZONE}." else - echo "Unable to get" - echo "your timezone." + echo "Unable to get" + echo "your timezone." fi + exit 0