From 59c288723691934ca73062f06eb1eef4ca5ba2db Mon Sep 17 00:00:00 2001 From: lawtancool <26829131+lawtancool@users.noreply.github.com> Date: Wed, 16 Jun 2021 21:05:34 -0700 Subject: [PATCH] Fix tail command and remove unnecessary echo --- dyndns.sh | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/dyndns.sh b/dyndns.sh index 561b029..acf6409 100755 --- a/dyndns.sh +++ b/dyndns.sh @@ -33,20 +33,34 @@ while ( true ); do ip="$(curl -s $service | grep '[0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{3\}')" test -n "$ip" && break done - + echo "Found IP address $ip" if [[ -n $ip ]]; then # disable glob expansion set -f - + for sub in ${NAME//;/ }; do record_id=$(echo $domain_records| jq ".domain_records[] | select(.type == \"A\" and .name == \"$sub\") | .id") record_data=$(echo $domain_records| jq -r ".domain_records[] | select(.type == \"A\" and .name == \"$sub\") | .data") - + + if [ $(echo "$record_id" | wc -l) -ge 2 ]; then : + echo "'$sub' domain name has duplicate DNS records, removing duplicates" + record_id_to_delete=$(echo "$record_id"| tail -n +2) + record_id=$(echo "$record_id"| head -1) + record_data=$(echo "$record_data"| head -1) + + while IFS= read -r line; do + curl -s -X DELETE \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \ + "$dns_list/$line" &> /dev/null + done <<< "$record_id_to_delete" + fi + # re-enable glob expansion set +f - + data="{\"type\": \"A\", \"name\": \"$sub\", \"data\": \"$ip\"}" url="$dns_list/$record_id"