From 148a7ac644f4e8c1539ced81d9a5af8943fa947c Mon Sep 17 00:00:00 2001 From: Jared Vititoe Date: Thu, 5 Feb 2026 10:49:23 -0500 Subject: [PATCH] Add validation for potentially empty variables Add fallback values for variables that might be empty when system information is unavailable. Use parameter expansion with default values (${var:-Default}) to ensure meaningful output even when commands fail or return empty results. Affected functions: get_cpu_info, get_ram_info, get_network_info https://code.lotusguild.org/LotusGuild/proxDoc/issues/11 Co-Authored-By: Claude Opus 4.5 --- proxDoc.sh | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/proxDoc.sh b/proxDoc.sh index c370cd3..f8b4590 100755 --- a/proxDoc.sh +++ b/proxDoc.sh @@ -132,25 +132,27 @@ get_disk_health() { } get_cpu_info() { - cpu_info=$(grep -m 1 -w 'model name' /proc/cpuinfo | awk -F: '{print $2}' | xargs) || { - echo -e "${RED}Failed to retrieve CPU model information.${NC}" - } - cpu_cores=$(lscpu | grep '^CPU(s):' | awk '{print $2}') - cpu_mhz=$(lscpu | grep 'MHz' | awk '{print $4}') - - echo -e "${GREEN}CPU Model:${NC} $cpu_info" - echo -e "${GREEN}CPU Cores:${NC} $cpu_cores" - echo -e "${GREEN}CPU MHz:${NC} $cpu_mhz" + local cpu_info cpu_cores cpu_mhz + + cpu_info=$(grep -m 1 -w 'model name' /proc/cpuinfo 2>/dev/null | awk -F: '{print $2}' | xargs) + cpu_cores=$(lscpu 2>/dev/null | grep '^CPU(s):' | awk '{print $2}') + cpu_mhz=$(lscpu 2>/dev/null | grep 'MHz' | awk '{print $4}') + + echo -e "${GREEN}CPU Model:${NC} ${cpu_info:-Unknown}" + echo -e "${GREEN}CPU Cores:${NC} ${cpu_cores:-Unknown}" + echo -e "${GREEN}CPU MHz:${NC} ${cpu_mhz:-Unknown}" } get_ram_info() { - ram_total=$(free -h | grep 'Mem:' | awk '{print $2}') - ram_used=$(free -h | grep 'Mem:' | awk '{print $3}') - ram_free=$(free -h | grep 'Mem:' | awk '{print $4}') - - echo -e "${GREEN}Total RAM:${NC} $ram_total" - echo -e "${GREEN}Used RAM:${NC} $ram_used" - echo -e "${GREEN}Free RAM:${NC} $ram_free" + local ram_total ram_used ram_free + + ram_total=$(free -h 2>/dev/null | grep 'Mem:' | awk '{print $2}') + ram_used=$(free -h 2>/dev/null | grep 'Mem:' | awk '{print $3}') + ram_free=$(free -h 2>/dev/null | grep 'Mem:' | awk '{print $4}') + + echo -e "${GREEN}Total RAM:${NC} ${ram_total:-Unknown}" + echo -e "${GREEN}Used RAM:${NC} ${ram_used:-Unknown}" + echo -e "${GREEN}Free RAM:${NC} ${ram_free:-Unknown}" } get_storage_info() { @@ -164,10 +166,13 @@ get_storage_info() { } get_network_info() { - default_gateway=$(ip route | grep default | awk '{print $3}') - ip_addresses=$(hostname -I | xargs) - echo -e "${GREEN}Default Gateway:${NC} $default_gateway" - echo -e "${GREEN}IP Addresses:${NC} $ip_addresses" + local default_gateway ip_addresses + + default_gateway=$(ip route 2>/dev/null | grep default | awk '{print $3}') + ip_addresses=$(hostname -I 2>/dev/null | xargs) + + echo -e "${GREEN}Default Gateway:${NC} ${default_gateway:-Not configured}" + echo -e "${GREEN}IP Addresses:${NC} ${ip_addresses:-None detected}" } get_detailed_network() {