0

我正在编写一个脚本来自动化一堆命令,并且我希望输出是右对齐和彩色的。到目前为止,我能够获得其中一个,但我未能成功实现这两个。我的(测试)脚本如下:

#!/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%*s\n" "$col1" "$string1" "$col2" "$string2"

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


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

string1="Running update 10"
string2="(10 of 10)"
printf "%-*s%*s\n" "$col1" "$string1" "$col2" "$string2"

exit 0

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

4

1 回答 1

1
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"
于 2013-02-08T23:46:00.737 回答