diff --git a/bin/optimize.sh b/bin/optimize.sh index 67ac0eb..e9aa4a7 100755 --- a/bin/optimize.sh +++ b/bin/optimize.sh @@ -108,12 +108,12 @@ show_system_health() { local health_json="$1" # Parse system health using jq with fallback to 0 - local mem_used=$(echo "$health_json" | jq -r '.memory_used_gb // 0' 2>/dev/null || echo "0") - local mem_total=$(echo "$health_json" | jq -r '.memory_total_gb // 0' 2>/dev/null || echo "0") - local disk_used=$(echo "$health_json" | jq -r '.disk_used_gb // 0' 2>/dev/null || echo "0") - local disk_total=$(echo "$health_json" | jq -r '.disk_total_gb // 0' 2>/dev/null || echo "0") - local disk_percent=$(echo "$health_json" | jq -r '.disk_used_percent // 0' 2>/dev/null || echo "0") - local uptime=$(echo "$health_json" | jq -r '.uptime_days // 0' 2>/dev/null || echo "0") + local mem_used=$(echo "$health_json" | jq -r '.memory_used_gb // 0' 2> /dev/null || echo "0") + local mem_total=$(echo "$health_json" | jq -r '.memory_total_gb // 0' 2> /dev/null || echo "0") + local disk_used=$(echo "$health_json" | jq -r '.disk_used_gb // 0' 2> /dev/null || echo "0") + local disk_total=$(echo "$health_json" | jq -r '.disk_total_gb // 0' 2> /dev/null || echo "0") + local disk_percent=$(echo "$health_json" | jq -r '.disk_used_percent // 0' 2> /dev/null || echo "0") + local uptime=$(echo "$health_json" | jq -r '.uptime_days // 0' 2> /dev/null || echo "0") # Ensure all values are numeric (fallback to 0) mem_used=${mem_used:-0} @@ -381,7 +381,7 @@ main() { fi # Validate JSON before proceeding - if ! echo "$health_json" | jq empty 2>/dev/null; then + if ! echo "$health_json" | jq empty 2> /dev/null; then if [[ -t 1 ]]; then stop_inline_spinner fi diff --git a/bin/uninstall.sh b/bin/uninstall.sh index a77210a..ac7cecc 100755 --- a/bin/uninstall.sh +++ b/bin/uninstall.sh @@ -110,7 +110,7 @@ scan_applications() { # Check if cache exists and is fresh if [[ -f "$cache_file" ]]; then local cache_age=$(($(date +%s) - $(get_file_mtime "$cache_file"))) - [[ $cache_age -eq $(date +%s) ]] && cache_age=86401 # Handle missing file + [[ $cache_age -eq $(date +%s) ]] && cache_age=86401 # Handle missing file if [[ $cache_age -lt $cache_ttl ]]; then # Cache hit - return immediately echo "$cache_file" @@ -633,7 +633,7 @@ main() { local cache_file="$HOME/.cache/mole/app_scan_cache" if [[ -f "$cache_file" ]]; then local cache_age=$(($(date +%s) - $(get_file_mtime "$cache_file"))) - [[ $cache_age -eq $(date +%s) ]] && cache_age=86401 # Handle missing file + [[ $cache_age -eq $(date +%s) ]] && cache_age=86401 # Handle missing file [[ $cache_age -lt 86400 ]] && needs_scanning=false fi diff --git a/lib/common.sh b/lib/common.sh index 47fe4e9..fd11fd3 100755 --- a/lib/common.sh +++ b/lib/common.sh @@ -48,17 +48,17 @@ readonly STAT_BSD="/usr/bin/stat" get_file_size() { local file="$1" - $STAT_BSD -f%z "$file" 2>/dev/null || echo 0 + $STAT_BSD -f%z "$file" 2> /dev/null || echo 0 } get_file_mtime() { local file="$1" - $STAT_BSD -f%m "$file" 2>/dev/null || echo 0 + $STAT_BSD -f%m "$file" 2> /dev/null || echo 0 } get_file_owner() { local file="$1" - $STAT_BSD -f%Su "$file" 2>/dev/null || echo "" + $STAT_BSD -f%Su "$file" 2> /dev/null || echo "" } # Security and Path Validation Functions @@ -672,9 +672,9 @@ update_via_homebrew() { local brew_tmp_file="" local brew_exit_file="" cleanup_brew_update() { - if [[ -n "$brew_pid" ]] && kill -0 "$brew_pid" 2>/dev/null; then - kill -TERM "$brew_pid" 2>/dev/null || true - wait "$brew_pid" 2>/dev/null || true + if [[ -n "$brew_pid" ]] && kill -0 "$brew_pid" 2> /dev/null; then + kill -TERM "$brew_pid" 2> /dev/null || true + wait "$brew_pid" 2> /dev/null || true fi [[ -n "$brew_tmp_file" ]] && rm -f "$brew_tmp_file" [[ -n "$brew_exit_file" ]] && rm -f "$brew_exit_file" @@ -696,7 +696,10 @@ update_via_homebrew() { # Redirect brew output to temp file to avoid interfering with spinner # Store exit code in a separate file to avoid wait issues with zsh brew_exit_file="${brew_tmp_file}.exit" - (brew update > "$brew_tmp_file" 2>&1 "$brew_exit_file") & + ( + brew update > "$brew_tmp_file" 2>&1 < /dev/null + echo $? > "$brew_exit_file" + ) & brew_pid=$! local elapsed=0 @@ -723,7 +726,7 @@ update_via_homebrew() { # Get brew update exit code from file instead of wait local brew_exit=0 if [[ -f "$brew_exit_file" ]]; then - brew_exit=$(cat "$brew_exit_file" 2>/dev/null || echo "0") + brew_exit=$(cat "$brew_exit_file" 2> /dev/null || echo "0") fi rm -f "$brew_exit_file" @@ -736,7 +739,7 @@ update_via_homebrew() { local update_output="" if [[ -f "$brew_tmp_file" ]]; then - update_output=$(cat "$brew_tmp_file" 2>/dev/null) + update_output=$(cat "$brew_tmp_file" 2> /dev/null) fi # Check for errors in output (brew update may return 0 even on failure) diff --git a/lib/optimization_tasks.sh b/lib/optimization_tasks.sh index ccddeab..b5766e3 100644 --- a/lib/optimization_tasks.sh +++ b/lib/optimization_tasks.sh @@ -220,7 +220,7 @@ opt_mail_downloads() { local deleted=0 for target_path in "${mail_dirs[@]}"; do if [[ -d "$target_path" ]]; then - deleted=$((deleted + $(find "$target_path" -type f -mtime +30 -delete -print 2>/dev/null | wc -l | tr -d ' '))) + deleted=$((deleted + $(find "$target_path" -type f -mtime +30 -delete -print 2> /dev/null | wc -l | tr -d ' '))) fi done @@ -243,7 +243,7 @@ opt_saved_state_cleanup() { # Only delete states older than 7 days (safer - won't lose recent work) local deleted=0 - deleted=$(find "$state_dir" -type d -name "*.savedState" -mtime +7 -exec rm -rf {} \; -print 2>/dev/null | wc -l | tr -d ' ') + deleted=$(find "$state_dir" -type d -name "*.savedState" -mtime +7 -exec rm -rf {} \; -print 2> /dev/null | wc -l | tr -d ' ') if [[ $deleted -gt 0 ]]; then echo -e "${GREEN}${ICON_SUCCESS}${NC} Removed $deleted old saved state(s)" diff --git a/lib/optimize_health.sh b/lib/optimize_health.sh index e0687a7..63c4e67 100755 --- a/lib/optimize_health.sh +++ b/lib/optimize_health.sh @@ -10,7 +10,7 @@ get_memory_info() { # Total memory total_bytes=$(sysctl -n hw.memsize 2> /dev/null || echo "0") - total_gb=$(awk "BEGIN {printf \"%.2f\", $total_bytes / (1024*1024*1024)}" 2>/dev/null || echo "0") + total_gb=$(awk "BEGIN {printf \"%.2f\", $total_bytes / (1024*1024*1024)}" 2> /dev/null || echo "0") [[ -z "$total_gb" || "$total_gb" == "" ]] && total_gb="0" # Used memory from vm_stat @@ -18,16 +18,16 @@ get_memory_info() { vm_output=$(vm_stat 2> /dev/null || echo "") page_size=4096 - active=$(echo "$vm_output" | awk '/Pages active:/ {print $NF}' | tr -d '.' 2>/dev/null || echo "0") - wired=$(echo "$vm_output" | awk '/Pages wired down:/ {print $NF}' | tr -d '.' 2>/dev/null || echo "0") - compressed=$(echo "$vm_output" | awk '/Pages occupied by compressor:/ {print $NF}' | tr -d '.' 2>/dev/null || echo "0") + active=$(echo "$vm_output" | awk '/Pages active:/ {print $NF}' | tr -d '.' 2> /dev/null || echo "0") + wired=$(echo "$vm_output" | awk '/Pages wired down:/ {print $NF}' | tr -d '.' 2> /dev/null || echo "0") + compressed=$(echo "$vm_output" | awk '/Pages occupied by compressor:/ {print $NF}' | tr -d '.' 2> /dev/null || echo "0") active=${active:-0} wired=${wired:-0} compressed=${compressed:-0} local used_bytes=$(((active + wired + compressed) * page_size)) - used_gb=$(awk "BEGIN {printf \"%.2f\", $used_bytes / (1024*1024*1024)}" 2>/dev/null || echo "0") + used_gb=$(awk "BEGIN {printf \"%.2f\", $used_bytes / (1024*1024*1024)}" 2> /dev/null || echo "0") [[ -z "$used_gb" || "$used_gb" == "" ]] && used_gb="0" echo "$used_gb $total_gb" @@ -41,16 +41,16 @@ get_disk_info() { df_output=$(df -k "$home" 2> /dev/null | tail -1) local total_kb used_kb - total_kb=$(echo "$df_output" | awk '{print $2}' 2>/dev/null || echo "0") - used_kb=$(echo "$df_output" | awk '{print $3}' 2>/dev/null || echo "0") + total_kb=$(echo "$df_output" | awk '{print $2}' 2> /dev/null || echo "0") + used_kb=$(echo "$df_output" | awk '{print $3}' 2> /dev/null || echo "0") total_kb=${total_kb:-0} used_kb=${used_kb:-0} - [[ "$total_kb" == "0" ]] && total_kb=1 # Avoid division by zero + [[ "$total_kb" == "0" ]] && total_kb=1 # Avoid division by zero - total_gb=$(awk "BEGIN {printf \"%.2f\", $total_kb / (1024*1024)}" 2>/dev/null || echo "0") - used_gb=$(awk "BEGIN {printf \"%.2f\", $used_kb / (1024*1024)}" 2>/dev/null || echo "0") - used_percent=$(awk "BEGIN {printf \"%.1f\", ($used_kb / $total_kb) * 100}" 2>/dev/null || echo "0") + total_gb=$(awk "BEGIN {printf \"%.2f\", $total_kb / (1024*1024)}" 2> /dev/null || echo "0") + used_gb=$(awk "BEGIN {printf \"%.2f\", $used_kb / (1024*1024)}" 2> /dev/null || echo "0") + used_percent=$(awk "BEGIN {printf \"%.1f\", ($used_kb / $total_kb) * 100}" 2> /dev/null || echo "0") [[ -z "$total_gb" || "$total_gb" == "" ]] && total_gb="0" [[ -z "$used_gb" || "$used_gb" == "" ]] && used_gb="0" @@ -64,12 +64,12 @@ get_uptime_days() { local boot_output boot_time uptime_days boot_output=$(sysctl -n kern.boottime 2> /dev/null || echo "") - boot_time=$(echo "$boot_output" | sed -n 's/.*sec = \([0-9]*\).*/\1/p' 2>/dev/null || echo "") + boot_time=$(echo "$boot_output" | sed -n 's/.*sec = \([0-9]*\).*/\1/p' 2> /dev/null || echo "") if [[ -n "$boot_time" && "$boot_time" =~ ^[0-9]+$ ]]; then - local now=$(date +%s 2>/dev/null || echo "0") + local now=$(date +%s 2> /dev/null || echo "0") local uptime_sec=$((now - boot_time)) - uptime_days=$(awk "BEGIN {printf \"%.1f\", $uptime_sec / 86400}" 2>/dev/null || echo "0") + uptime_days=$(awk "BEGIN {printf \"%.1f\", $uptime_sec / 86400}" 2> /dev/null || echo "0") else uptime_days="0" fi