#!/bin/bash
diskusage=$(df "/" | grep / | tail -1 | awk '{print $4}' | sed 's/%//g')
if [ "$diskusage" -ge 85 ];then
echo "--------------> Warning Disk usage "${s[@]}" $diskusage% is Above Threshold "
disk_color='e[1;31m%-6se[m'
#red color
else
disk_color='e[1;32m%-6se[m'
#green color
fi
printf -v spaces '%*s' 100 ''; printf '%sn' ${spaces// /_}
printf -v spaces '%*s' $diskusage ''; printf '%sn' ${spaces// /▇}" $diskusage% Used "
printf -v spaces '%*s' 100 ''; printf '%sn' ${spaces// /-}
输出按预期正常,但未打印彩色"/▇":
▇▇▇▇▇▇▇▇ 8% Used ----------------------------------------------------------------------------------------------------
您需要
disk_color
位于格式字符串内才能正常工作,因此:
printf "This is a regular string, but $disk_colorn" "This is a green string"
现在绿色字符串可以正确格式化。%-6s
是获取参数并将其嵌入到打印字符串中的部分,并且它需要位于格式字符串(第一个参数(中。