使用bash printf右对齐第二列文本(使用彩色文本)



我正在编写一个脚本来自动化一堆命令,我希望输出既正确对齐又有颜色。到目前为止,我可以得到其中一个,但我都没有成功。我的(测试)脚本如下:

#!/bin/bash
columns=$(tput cols)
txtgrn=$(tput setaf 2) # Green
txtpur=$(tput setaf 5) # Purple
txtrst=$(tput sgr0) # Text reset.
col1="60"
let "col2 = $columns - $col1"

# Colored text, left-aligned
string1="${txtpur}Running update 1${txtrst}"
string2="${txtgrn}(1 of 10)${txtrst}"
printf "%-*s%*sn" "$col1" "$string1" "$col2" "$string2"
string1="${txtpur}Running update 10${txtrst}"
string2="${txtgrn}(10 of 10)${txtrst}"
printf "%-*s%*sn" "$col1" "$string1" "$col2" "$string2"

# Non-colored text, right-aligned
string1="Running update 1"
string2="(1 of 10)"
printf "%-*s%*sn" "$col1" "$string1" "$col2" "$string2"
string1="Running update 10"
string2="(10 of 10)"
printf "%-*s%*sn" "$col1" "$string1" "$col2" "$string2"
exit 0

我在标准的Mac终端程序中使用Mac OS 10.7.5

string1="Running update 1"
string2="(1 of 10)"
printf "${txtpur}%-*s${txtgrn}%*s${txtrst}n" "$col1" "$string1" "$col2" "$string2"
string1="Running update 10"
string2="(10 of 10)"
printf "${txtpur}%-*s${txtgrn}%*s${txtrst}n" "$col1" "$string1" "$col2" "$string2"

最新更新