From e5b1668f68407a52be87d56c0bee01c31c22b04e Mon Sep 17 00:00:00 2001 From: CanbiZ <47820557+MickLesk@users.noreply.github.com> Date: Mon, 30 Jun 2025 14:01:17 +0200 Subject: [PATCH] Update lxc-delete.sh --- tools/pve/lxc-delete.sh | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/tools/pve/lxc-delete.sh b/tools/pve/lxc-delete.sh index cb413f33..85487cd2 100644 --- a/tools/pve/lxc-delete.sh +++ b/tools/pve/lxc-delete.sh @@ -44,10 +44,10 @@ if ! whiptail --backtitle "Proxmox VE Helper Scripts" --title "Proxmox VE LXC De echo -e "${RD}Aborted by user.${CL}" exit 0 fi -NODE=$(hostname) -containers=$(pct list | tail -n +2 | awk '{print $0 " " $4}') -if [ -z "$containers" ]; then +mapfile -t containers < <(pct list | tail -n +2) + +if [ ${#containers[@]} -eq 0 ]; then whiptail --title "LXC Container Delete" --msgbox "No LXC containers available!" 10 60 exit 1 fi @@ -55,41 +55,39 @@ fi menu_items=("ALL" "Delete ALL containers" "OFF") FORMAT="%-10s %-10s %-10s %-10s" -while read -r container; do - container_id=$(echo $container | awk '{print $1}') - container_name=$(echo $container | awk '{print $2}') - container_status=$(echo $container | awk '{print $3}') - container_os=$(echo $container | awk '{print $4}') - protected=$(pct config $container_id | grep -i '^protection:' | awk '{print $2}') +for line in "${containers[@]}"; do + container_id=$(echo "$line" | awk '{print $1}') + container_name=$(echo "$line" | awk '{print $2}') + container_status=$(echo "$line" | awk '{print $3}') + container_os=$(echo "$line" | awk '{print $4}') + protected=$(pct config "$container_id" | awk '/^protection:/ {print $2}') is_protected="No" [[ "$protected" == "1" ]] && is_protected="Yes" formatted_line=$(printf "$FORMAT" "$container_name" "$container_status" "$container_os" "$is_protected") menu_items+=("$container_id" "$formatted_line" "OFF") -done < <(printf '%s\n' "$containers") +done CHOICES=$(whiptail --title "LXC Container Delete" \ --checklist "Select LXC containers to delete:\n\nNAME STATUS OS PROTECTED" 25 70 15 \ "${menu_items[@]}" 3>&2 2>&1 1>&3) if [ -z "$CHOICES" ]; then - whiptail --title "LXC Container Delete" \ - --msgbox "No containers selected!" 10 60 + whiptail --title "LXC Container Delete" --msgbox "No containers selected!" 10 60 exit 1 fi read -p "Delete containers manually or automatically? (Default: manual) m/a: " DELETE_MODE DELETE_MODE=${DELETE_MODE:-m} - selected_ids=$(echo "$CHOICES" | tr -d '"' | tr -s ' ' '\n') -# Wenn ALL ausgewählt ist, überschreiben +# ALL ausgewählt if echo "$selected_ids" | grep -q "^ALL$"; then - selected_ids=$(echo "$containers" | awk '{print $1}') + selected_ids=$(printf '%s\n' "${containers[@]}" | awk '{print $1}') fi for container_id in $selected_ids; do status=$(pct status "$container_id") - protected=$(pct config "$container_id" | grep -i '^protection:' | awk '{print $2}') + protected=$(pct config "$container_id" | awk '/^protection:/ {print $2}') is_protected="No" [[ "$protected" == "1" ]] && is_protected="Yes" @@ -98,7 +96,7 @@ for container_id in $selected_ids; do continue fi - if [ "$status" == "status: running" ]; then + if [[ "$status" == "status: running" ]]; then if [[ "$is_protected" == "Yes" && "$DELETE_MODE" == "m" ]]; then read -p "⚠️ Container $container_id is PROTECTED. Delete anyway? (y/N): " CONFIRM_PROTECTED [[ ! "$CONFIRM_PROTECTED" =~ ^[Yy]$ ]] && {