From bf49b7bff3a384d125e1249830d63b8861987071 Mon Sep 17 00:00:00 2001 From: Grische <2787581+grische@users.noreply.github.com> Date: Tue, 21 Nov 2023 00:21:46 +0100 Subject: [PATCH] ffgraz-ddhcpd-batman-adv: fix shellcheck issues This includes a bit of restructuring the flow as the A && B || C can run C if either A or B fails. This seemed unintentional. --- .../files/usr/sbin/ddhcpd-gateway-update | 65 +++++++++++-------- 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/ffgraz-ddhcpd-batman-adv/files/usr/sbin/ddhcpd-gateway-update b/ffgraz-ddhcpd-batman-adv/files/usr/sbin/ddhcpd-gateway-update index 204d17e3..8361b218 100755 --- a/ffgraz-ddhcpd-batman-adv/files/usr/sbin/ddhcpd-gateway-update +++ b/ffgraz-ddhcpd-batman-adv/files/usr/sbin/ddhcpd-gateway-update @@ -1,4 +1,5 @@ -#!/bin/sh +#!/bin/busybox sh +# shellcheck shell=dash CACHE_TTL=600 CACHE_FILE=/tmp/ddhcp_gw_cache @@ -7,20 +8,22 @@ GW_UPDATE_TRIES=5 verbose() { - [ -n "$VERBOSE" ] && ( >&2 echo $@ ) + [ -n "$VERBOSE" ] && ( >&2 echo "$@" ) } mac_to_ipv6_ll() { - IFS=':'; set $1; unset IFS + IFS=':'; set "$1"; unset IFS echo "fe80::$(printf %02x $((0x$1 ^ 2)))$2:${3}ff:fe$4:$5$6" } get_gw_ipv4_address() { local gwmac="$1" - local gwid="`echo "$gwmac" | tr -d ':'`" - gluon-neighbour-info -p 1001 -d "`mac_to_ipv6_ll "$gwmac"`" -i br-client -r gateway | \ - while read resp; do - local node_id="`echo "$resp" | jsonfilter -e '$.node_id'`" + local gwid + gwid="$(echo "$gwmac" | tr -d ':')" + gluon-neighbour-info -p 1001 -d "$(mac_to_ipv6_ll "$gwmac")" -i br-client -r gateway | \ + while read -r resp; do + local node_id + node_id="$(echo "$resp" | jsonfilter -e '$.node_id')" if [ "$node_id" = "$gwid" ]; then echo "$resp" | jsonfilter -e '$.address.ipv4' fi @@ -40,49 +43,55 @@ update_gateway() { /usr/sbin/ddhcpdctl -o "6:4:$1" } -gwmac="`batctl gwl | sed 's/([[:space:]]*[0-9]\+)//g' | sed 's/[[:space:]]\+/ /g' | egrep '(^\*)|(=>)' | cut -d' ' -f2`" -[ -z "$gwmac" ] && { +gwmac="$(batctl gwl | sed 's/([[:space:]]*[0-9]\+)//g' | sed 's/[[:space:]]\+/ /g' | grep -E '(^\*)|(=>)' | cut -d' ' -f2)" +if [ -z "$gwmac" ]; then verbose Failed to determine mac address of current gateway gateway_lost exit 1 -} +fi verbose Got gateway mac address: "$gwmac" -[ -f "$CACHE_FILE" ] && { +# disable checks for unknown variables (SC2154) because they are loaded from an unknown files (SC1090) +# shellcheck disable=SC1090,SC2154 +if [ -f "$CACHE_FILE" ]; then verbose Retrieving cache - source "$CACHE_FILE" - now="`date +'%s'`" - if echo "$last_update_time" | egrep -q '^[0-9]+$' && \ - echo "$last_gwaddr" | egrep -q '^(([0-9]){1,3}\.){3}[0-9]{1,3}$'; then - verbose Gateway mac address cache found, age $(($now - $last_update_time)) - if [ "$gwmac" = "$last_gwmac" ] && \ - [ $(($now - $last_update_time)) -lt "$CACHE_TTL" ]; then + . "$CACHE_FILE" + now="$(date +'%s')" + if echo "$last_update_time" | grep -E -q '^[0-9]+$' && \ + echo "$last_gwaddr" | grep -E -q '^(([0-9]){1,3}\.){3}[0-9]{1,3}$' + then + verbose "Gateway mac address cache found, age $((now - last_update_time))" + if [ "$gwmac" = "$last_gwmac" ] && [ $((now - last_update_time)) -lt "$CACHE_TTL" ] + then verbose Cache is up to date update_gateway "$last_gwaddr" exit 0 fi fi verbose Updating cache -} +fi -for _ in `seq "$GW_UPDATE_TRIES"`; do - gwaddr="`get_gw_ipv4_address "$gwmac"`" +for _ in $(seq "$GW_UPDATE_TRIES"); do + gwaddr="$(get_gw_ipv4_address "$gwmac")" [ -n "$gwaddr" ] && break done -[ -z "$gwaddr" ] && [ ! "$gwmac" == "$last_gwmac" ] && { + +if [ -z "$gwaddr" ] && [ ! "$gwmac" = "$last_gwmac" ]; then verbose Failed to retrieve new gateway IPv4 address gateway_lost exit 2 -} -[ ! -z "$gwaddr" ] && { +fi + +if [ -z "$gwaddr" ]; then + verbose "Information gathering failed, done nothing" + exit 0 +fi + verbose Got gateway IPv4 address: "$gwaddr" echo "last_gwmac=\"$gwmac\"" > "$CACHE_FILE" echo "last_gwaddr=\"$gwaddr\"" >> "$CACHE_FILE" -echo "last_update_time=`date +'%s'`" >> "$CACHE_FILE" +echo "last_update_time=$(date +'%s')" >> "$CACHE_FILE" update_gateway "$gwaddr" -} || { - verbose Information gathering failed, done nothing -}