Compare commits

..

1 Commits

Author SHA1 Message Date
MickLesk 68571bde78 Fix clean-lxcs exclude matching and set -e cancel handling
- Replace the array-style exclude check (`${excluded_containers[@]}` on a
  plain string) with an explicit per-VMID loop, resolving the ShellCheck
  SC2199/SC2076 errors and avoiding accidental substring matches.
- Abort cleanly when the checklist dialog is cancelled instead of relying
  on an unreachable `$?` test under `set -eEuo pipefail`.
- Exit gracefully on a declined confirmation prompt.
- Use `pct exec ... -- hostname` for consistent argument handling.
2026-06-26 21:25:19 +02:00
2 changed files with 19 additions and 22 deletions
+15 -8
View File
@@ -30,7 +30,7 @@ declare -f init_tool_telemetry &>/dev/null && init_tool_telemetry "clean-lxcs" "
header_info
echo "Loading..."
whiptail --backtitle "Proxmox VE Helper Scripts" --title "Proxmox VE LXC Updater" --yesno "This will clean logs, cache and update package lists on selected LXC Containers. Proceed?" 10 58
whiptail --backtitle "Proxmox VE Helper Scripts" --title "Proxmox VE LXC Updater" --yesno "This will clean logs, cache and update package lists on selected LXC Containers. Proceed?" 10 58 || exit 0
NODE=$(hostname)
EXCLUDE_MENU=()
@@ -42,17 +42,17 @@ while read -r TAG ITEM; do
EXCLUDE_MENU+=("$TAG" "$ITEM " "OFF")
done < <(pct list | awk 'NR>1')
excluded_containers=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "Containers on $NODE" --checklist "\nSelect containers to skip from cleaning:\n" \
16 $((MSG_MAX_LENGTH + 23)) 6 "${EXCLUDE_MENU[@]}" 3>&1 1>&2 2>&3 | tr -d '"')
if [ $? -ne 0 ]; then
exit
# Capture the selection; abort cleanly if the user cancels the dialog
# (set -e would otherwise terminate on the failing command substitution).
if ! excluded_containers=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "Containers on $NODE" --checklist "\nSelect containers to skip from cleaning:\n" \
16 $((MSG_MAX_LENGTH + 23)) 6 "${EXCLUDE_MENU[@]}" 3>&1 1>&2 2>&3 | tr -d '"'); then
exit 0
fi
function run_lxc_clean() {
local container=$1
header_info
name=$(pct exec "$container" hostname)
name=$(pct exec "$container" -- hostname)
pct exec "$container" -- bash -c '
BL="\033[36m"; GN="\033[1;92m"; CL="\033[m"
@@ -84,7 +84,14 @@ function run_lxc_clean() {
}
for container in $(pct list | awk '{if(NR>1) print $1}'); do
if [[ " ${excluded_containers[@]} " =~ " $container " ]]; then
excluded=0
for ex in $excluded_containers; do
if [[ "$ex" == "$container" ]]; then
excluded=1
break
fi
done
if [[ "$excluded" -eq 1 ]]; then
header_info
echo -e "${BL}[Info]${GN} Skipping ${BL}$container${CL}"
sleep 1
+4 -14
View File
@@ -55,23 +55,12 @@ read -r selected
selected_indices=()
IFS=',' read -r -a tokens <<<"$selected"
for token in "${tokens[@]}"; do
# Strip surrounding whitespace and skip empty tokens
token="${token//[[:space:]]/}"
[ -z "$token" ] && continue
if [[ "$token" =~ ^([0-9]+)-([0-9]+)$ ]]; then
start=${BASH_REMATCH[1]}
end=${BASH_REMATCH[2]}
if ((start > end)); then
echo -e "${RD}Ignoring invalid range '${token}' (start greater than end).${CL}"
continue
fi
for ((i = start; i <= end; i++)); do
for ((i = BASH_REMATCH[1]; i <= BASH_REMATCH[2]; i++)); do
selected_indices+=("$i")
done
elif [[ "$token" =~ ^[0-9]+$ ]]; then
selected_indices+=("$token")
else
echo -e "${RD}Ignoring invalid selection '${token}'.${CL}"
selected_indices+=("$token")
fi
done
@@ -112,7 +101,8 @@ for kernel in "${kernels_to_remove[@]}"; do
remaining=$(dpkg --list |
awk '/^ii/ {print $2}' |
grep -E "^proxmox-kernel-${minor_version}\." |
grep -cv "^${kernel}$")
grep -v "^${kernel}$" |
wc -l)
if [ "$remaining" -eq 0 ]; then
pkgs_to_remove+=("$meta")
fi