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
#11
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
45
proxDoc.sh
45
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() {
|
||||
|
||||
Reference in New Issue
Block a user