目前我有这种输出格式:
{ echo "$(figlet buddhi)"; echo "$(figlet lw)"; }
_ _ _ _ _
| |__ _ _ __| | __| | |__ (_)
| '_ \| | | |/ _` |/ _` | '_ \| |
| |_) | |_| | (_| | (_| | | | | |
|_.__/ \__,_|\__,_|\__,_|_| |_|_|
_
| |_ __
| \ \ /\ / /
| |\ V V /
|_| \_/\_/
我想要这种输出格式:
figlet buddhi lw
_ _ _ _ _ _
| |__ _ _ __| | __| | |__ (_) | |_ __
| '_ \| | | |/ _` |/ _` | '_ \| | | \ \ /\ / /
| |_) | |_| | (_| | (_| | | | | | | |\ V V /
|_.__/ \__,_|\__,_|\__,_|_| |_|_| |_| \_/\_/
原因是:我想用不同的颜色为每个名字(buddhi,lw)着色。但是,保留连续字符串的格式,或最大空格分隔,如上所述。
例子:
#COMMANDS CREATED INSIDE /ETC/BASH.BASHRC FILE
# USING ANSI COLORS
RED="\e[31m"
ORANGE="\e[33m"
BLUE="\e[94m"
GREEN="\e[92m"
STOP="\e[0m"
printf "${GREEN}"
printf "=================================\n"
printf "${ORANGE}"
figlet -f standard "Buddhi"
printf "${BLUE}"
figlet -f small "LW"
printf "${GREEN}"
printf "=================================\n"
printf "${STOP}"
uj5u.com热心网友回复:
将每个单词的行存盘在阵列中,逐行输出两个阵列。由于“Buddhi”的第一行似乎短了一个字符,我将第一个单词的最长行长度存盘在一个变量中,并使用该%-s
格式来填充每一行。
#! /bin/bash
RED="\e[31m"
ORANGE="\e[33m"
BLUE="\e[94m"
GREEN="\e[92m"
STOP="\e[0m"
mapfile -t left < <(figlet -f standard "Buddhi")
mapfile -t right < <(figlet -f small "LW")
maxlength=0
for line in "${left[@]}" ; do
if (( ${#line} > maxlength )) ; then
maxlength=${#line}
fi
done
printf "${GREEN}"
printf "=================================\n"
for ((i=0; i<=${#left[@]}; i)) ; do
printf "${ORANGE}%-${maxlength}s ${GREEN}%s\n" "${left[i]}" "${right[i]}"
done
printf "${GREEN}"
printf "=================================\n"
printf "${STOP}"
uj5u.com热心网友回复:
如果您需要更短的版本:
printf "$GREEN=================================\n"
{ figlet Buddhi; echo 'EOF'; figlet LW; } | awk 'NF==1&&$1=="EOF" {noskip=1; next; } noskip==0 { f[ c]=$0; next; } { printf "%s%s%s%s\n","'"$ORANGE"'",f[ k],"'"$BLUE"'",$0;}'
printf "$GREEN=================================\n"
tput sgr0
我建议使用 tput 来设定颜色,因为并非每个终端都会知道您的转义序列
uj5u.com热心网友回复:
发明 shell 的家伙还发明了 awk,让 shell 呼叫来操作文本。这些转义序列不会在我的终端上改变颜色,它们只是按原样显示(幸运的是,您可以看到脚本将它们放在何处):
$ cat tst.sh
#!/usr/bin/env bash
awk '
BEGIN {
red = "\\e[31m"
orange = "\\e[33m"
blue = "\\e[94m"
green = "\\e[92m"
stop = "\\e[0m"
}
{
val[(NR==FNR),FNR] = $0
}
NR == FNR {
wid = length($0)
maxWid = ( wid > maxWid ? wid : maxWid )
}
END {
for ( lineNr=1; lineNr<=FNR; lineNr ) {
printf "%s%-*s%s%s%s\n", orange, maxWid, val[1,lineNr], blue, val[0,lineNr], stop
}
}
' <(cat Buddhi) <(cat LW)
$ ./tst.sh
\e[33m _ _ _ _ _ \e[94m _\e[0m
\e[33m| |__ _ _ __| | __| | |__ (_)\e[94m| |_ __\e[0m
\e[33m| '_ \| | | |/ _` |/ _` | '_ \| |\e[94m| \ \ /\ / /\e[0m
\e[33m| |_) | |_| | (_| | (_| | | | | |\e[94m| |\ V V /\e[0m
\e[33m|_.__/ \__,_|\__,_|\__,_|_| |_|_|\e[94m|_| \_/\_/\e[0m
由于我没有figlet
,我在这些档案上运行了上述内容:
$ head Buddhi LW
==> Buddhi <==
_ _ _ _ _
| |__ _ _ __| | __| | |__ (_)
| '_ \| | | |/ _` |/ _` | '_ \| |
| |_) | |_| | (_| | (_| | | | | |
|_.__/ \__,_|\__,_|\__,_|_| |_|_|
==> LW <==
_
| |_ __
| \ \ /\ / /
| |\ V V /
|_| \_/\_/
只需将脚本的最后一行更改为:
' <(cat Buddhi) <(cat LW)
到
' <(figlet Buddhi) <(figlet LW)
使用实际figlet
输出。
上面假设您只有 2 个figlet
输出字符串要连接并且两组输出的长度相同,如果这些假设中的任何一个是错误的,则很容易进行调整。
uj5u.com热心网友回复:
代替figlet
我将使用以下作为我的输入:
$ cat buddhi
_ _ _ _ _
| |__ _ _ __| | __| | |__ (_)
| '_ \| | | |/ _` |/ _` | '_ \| |
| |_) | |_| | (_| | (_| | | | | |
|_.__/ \__,_|\__,_|\__,_|_| |_|_|
$ cat lw
_
| |_ __
| \ \ /\ / /
| |\ V V /
|_| \_/\_/
假设figlet
为每个输入字符串生成相同数量的输出行,我们可以使用paste
(@
作为分隔符) 和一个while/read
回圈来生成所需的输出:
printf "${GREEN}"
printf "============================\n"
maxwidth=$(awk '{max=length($0) > max ? length($0) : max}END{print max}' buddhi)
while IFS='@' read -r col1 col2
do
printf "${ORANGE}%-*s ${BLUE}%s\n" "${maxwidth}" "${col1}" "${col2}"
done < <(paste -d"@" buddhi lw)
printf "${GREEN}"
printf "============================\n"
这会产生:
扩展到 3 个输入流:
printf "${GREEN}"
printf "============================\n"
max1=$(awk '{max=length($0) > max ? length($0) : max}END{print max}' buddhi)
max2=$(awk '{max=length($0) > max ? length($0) : max}END{print max}' lw)
while IFS='@' read -r col1 col2 col3
do
printf "${ORANGE}%-*s ${BLUE}%-*s ${RED}%s\n" "${max1}" "${col1}" "${max2}" "${col2}" "${col3}"
done < <(paste -d"@" buddhi lw buddhi)
printf "${GREEN}"
printf "============================\n"
这会产生:
uj5u.com热心网友回复:
使用坐标
#!/bin/bash
RED='\e[31m'
GRN='\e[32m'
XY(){ printf "\e[$2;${1}H$3"; }
mapfile -t frst < <(figlet -f standard "Buddhi")
mapfile -t scnd < <(figlet -f small "LW")
XY 1 1 "$GRN==============================================="; y=2
for line in "${frst[@]}"; { XY 0 $y "$RED$line"; ((y )); }; y=2
for line in "${scnd[@]}"; { XY 35 $y "$GRN$line"; ((y )); }
XY 1 8 "$GRN==============================================="
更多例子在这里,这里和这里
0 评论