From fbfbc50bfa935427c36e9bb427b28a512bbce93d Mon Sep 17 00:00:00 2001 From: Janik von Rotz Date: Tue, 13 Oct 2026 17:52:15 +0200 Subject: [PATCH] fix: improve hostname parsing logic to correctly identify and free available hostnames - Replace `sed`-based hostname extraction with `grep -oE` to accurately isolate numeric suffixes - Ensure consistent variable quoting in loops and `printf` commands to prevent word splitting - Update the free hostname check to use anchored `grep` patterns for precise matching --- task | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/task b/task index a8c72a13..eeef8c66 100755 --- a/task +++ b/task @@ -526,17 +526,17 @@ get-last-hostname() { echo "Search inventories folder for '${role}_hostname: $role'" local all_hostnames all_hostnames=$(rg -I --no-ignore "^${role}_hostname: ${role}.+" inventories | cut -d' ' -f2 | sort -u) - + local highest - highest=$(echo "$all_hostnames" | sed "s/${role}//g" | sort -n | tail -1) - echo "Highest hostname: ${role}$highest" + highest=$(echo "$all_hostnames" | grep -oE "^${role}[0-9]+" | sed "s/${role}//" | sort -n | tail -1) + echo "Highest hostname: ${role}${highest}" echo "Free hostnames:" - for i in $(seq 1 $highest); do + for i in $(seq 1 "$highest"); do local num - num=$(printf "%02d" $i) + num=$(printf "%02d" "$i") local hostname="${role}${num}" - if ! echo "$all_hostnames" | grep -q "$hostname"; then + if ! echo "$all_hostnames" | grep -q "^${hostname}"; then echo "$hostname" fi done