Fix remote_disk_info_short with robust df parsing

Parse df output locally using awk to find the % field and extract
the 3 size fields before it. Handles wrapped lines and CR chars.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
shuki
2026-03-06 07:14:59 +02:00
parent ccae8451c3
commit 0eb4dc00ed

View File

@@ -331,23 +331,37 @@ check_remote_disk_space() {
} }
# Compact one-line disk info: "USED/TOTAL (FREE free) PCT" # Compact one-line disk info: "USED/TOTAL (FREE free) PCT"
# Parsing is done on the remote/local side to avoid SSH output issues.
remote_disk_info_short() { remote_disk_info_short() {
local base="${REMOTE_BASE:-/}" local base="${REMOTE_BASE:-/}"
local cmd="df -h '$base' 2>/dev/null | awk 'NR>1{printf \"%s/%s (%s free) %s\", \$3, \$2, \$4, \$5}'" local df_out=""
local result=""
case "${REMOTE_TYPE:-ssh}" in case "${REMOTE_TYPE:-ssh}" in
ssh) ssh)
result=$(remote_exec "$cmd") || return 1 df_out=$(remote_exec "df -h '$base' 2>/dev/null") || return 1
;; ;;
local) local)
result=$(eval "$cmd") || return 1 df_out=$(df -h "$base" 2>/dev/null) || return 1
;; ;;
*) *)
echo "N/A" echo "N/A"
return 0 return 0
;; ;;
esac esac
echo "${result:-N/A}" # Strip carriage returns, squeeze whitespace, find line with %
local data_line
data_line=$(echo "$df_out" | tr '\r' ' ' | tr -s ' ' | grep '%' | tail -1)
if [[ -z "$data_line" ]]; then
echo "N/A"
return 0
fi
# Find the % field and extract Size/Used/Avail from the 3 fields before it
local size used avail pct
read -r size used avail pct < <(
echo "$data_line" | awk '{for(i=1;i<=NF;i++){if($i~/%/){print $(i-3),$(i-2),$(i-1),$i;exit}}}'
)
if [[ -n "$size" ]]; then
echo "${used}/${size} (${avail} free) ${pct}"
else
echo "N/A"
fi
} }