Replace echo -e with printf for portability

Converted all echo -e commands to printf for better portability
across different systems and shells. Printf is POSIX-compliant
and behaves consistently.

Updated functions:
- colorize_health(): Uses printf %b for escape sequences
- colorize_temp(): Uses printf %b for escape sequences
- colorize_header(): Uses printf with newline
- log_error(), log_warn(), log_info(): Uses printf for stderr

Also simplified header output by calling colorize_header directly
since it now handles its own newline.

Fixes: #19

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-05 11:45:56 -05:00
parent 6436e9fbb4
commit 7450d79f01

View File

@@ -129,12 +129,12 @@ colorize_health() {
local health="$1"
if [[ "$USE_COLOR" == true ]]; then
if [[ "$health" == "✓" ]]; then
echo -e "${COLOR_GREEN}${health}${COLOR_RESET}"
printf '%b%s%b' "$COLOR_GREEN" "$health" "$COLOR_RESET"
else
echo -e "${COLOR_RED}${health}${COLOR_RESET}"
printf '%b%s%b' "$COLOR_RED" "$health" "$COLOR_RESET"
fi
else
echo "$health"
printf '%s' "$health"
fi
}
@@ -157,14 +157,14 @@ colorize_temp() {
temp_val="${temp_str%°C}"
if [[ "$temp_val" =~ ^[0-9]+$ ]]; then
if [[ "$temp_val" -ge 60 ]]; then
echo -e "${COLOR_RED}${temp_str}${COLOR_RESET}"
printf '%b%s%b' "$COLOR_RED" "$temp_str" "$COLOR_RESET"
elif [[ "$temp_val" -ge 50 ]]; then
echo -e "${COLOR_YELLOW}${temp_str}${COLOR_RESET}"
printf '%b%s%b' "$COLOR_YELLOW" "$temp_str" "$COLOR_RESET"
else
echo -e "${COLOR_GREEN}${temp_str}${COLOR_RESET}"
printf '%b%s%b' "$COLOR_GREEN" "$temp_str" "$COLOR_RESET"
fi
else
echo "$temp_str"
printf '%s' "$temp_str"
fi
}
@@ -176,9 +176,9 @@ colorize_temp() {
#------------------------------------------------------------------------------
colorize_header() {
if [[ "$USE_COLOR" == true ]]; then
echo -e "${COLOR_BLUE}${COLOR_BOLD}$1${COLOR_RESET}"
printf '%b%b%s%b\n' "$COLOR_BLUE" "$COLOR_BOLD" "$1" "$COLOR_RESET"
else
echo "$1"
printf '%s\n' "$1"
fi
}
@@ -190,9 +190,9 @@ colorize_header() {
#------------------------------------------------------------------------------
log_error() {
if [[ "$USE_COLOR" == true ]]; then
echo -e "${COLOR_RED}ERROR:${COLOR_RESET} $1" >&2
printf '%bERROR:%b %s\n' "$COLOR_RED" "$COLOR_RESET" "$1" >&2
else
echo "ERROR: $1" >&2
printf 'ERROR: %s\n' "$1" >&2
fi
}
@@ -205,9 +205,9 @@ log_error() {
log_warn() {
if [[ "$VERBOSE" == true ]]; then
if [[ "$USE_COLOR" == true ]]; then
echo -e "${COLOR_YELLOW}WARN:${COLOR_RESET} $1" >&2
printf '%bWARN:%b %s\n' "$COLOR_YELLOW" "$COLOR_RESET" "$1" >&2
else
echo "WARN: $1" >&2
printf 'WARN: %s\n' "$1" >&2
fi
fi
}
@@ -221,9 +221,9 @@ log_warn() {
log_info() {
if [[ "$VERBOSE" == true ]]; then
if [[ "$USE_COLOR" == true ]]; then
echo -e "${COLOR_CYAN}INFO:${COLOR_RESET} $1" >&2
printf '%bINFO:%b %s\n' "$COLOR_CYAN" "$COLOR_RESET" "$1" >&2
else
echo "INFO: $1" >&2
printf 'INFO: %s\n' "$1" >&2
fi
fi
}
@@ -851,7 +851,7 @@ if [[ "$SKIP_CEPH" != true ]]; then
fi
printf "\n"
echo -e "$(colorize_header '=== Drive Details with SMART Status (by Bay Position) ===')"
colorize_header '=== Drive Details with SMART Status (by Bay Position) ==='
if [[ "$SHOW_PCI" == true ]]; then
printf "%-5s %-15s %-10s %-8s %-8s %-8s %-30s %-20s %-12s %-10s %-10s %-30s %-40s\n" "BAY" "DEVICE" "SIZE" "TYPE" "TEMP" "HEALTH" "MODEL" "SERIAL" "CEPH OSD" "STATUS" "USAGE" "WARNINGS" "PCI PATH"
echo "----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
@@ -1007,7 +1007,7 @@ if [[ -n "$nvme_devices" ]]; then
if [[ -n "$unmapped_nvme" ]]; then
printf "\n"
echo -e "$(colorize_header '=== Unmapped NVMe Drives ===')"
colorize_header '=== Unmapped NVMe Drives ==='
printf "%-15s %-10s %-10s %-40s %-25s\n" "DEVICE" "SIZE" "TYPE" "MODEL" "SERIAL"
echo "------------------------------------------------------------------------------------------------------"
echo "$unmapped_nvme" | while read -r name size; do
@@ -1032,7 +1032,7 @@ fi
rbd_devices=$(lsblk -d -n -o NAME,SIZE,TYPE 2>/dev/null | grep "rbd" | sort -V)
if [ -n "$rbd_devices" ]; then
printf "\n"
echo -e "$(colorize_header '=== Ceph RBD Devices ===')"
colorize_header '=== Ceph RBD Devices ==='
printf "%-15s %-10s %-10s %-30s\n" "DEVICE" "SIZE" "TYPE" "MOUNTPOINT"
echo "------------------------------------------------------------"
echo "$rbd_devices" | while read -r name size type; do
@@ -1046,7 +1046,7 @@ fi
# Show mapping diagnostic info if DEBUG is set
if [[ -n "$DEBUG" ]]; then
printf "\n"
echo -e "$(colorize_header '=== DEBUG: Drive Mappings ===')"
colorize_header '=== DEBUG: Drive Mappings ==='
for key in "${!DRIVE_MAP[@]}"; do
echo "Bay $key: ${DRIVE_MAP[$key]}"
done | sort -n