101超酷脚本学习笔记 :11节 ANSI Color Sequences

按原书的脚本,发现不能显示出效果。

#!/bin/sh
initializeANSI()
{
  esc="\033" # if this doesn't work, enter an ESC directly

  blackf="${esc}[30m";   redf="${esc}[31m";    greenf="${esc}[32m"
  yellowf="${esc}[33m"   bluef="${esc}[34m";   purplef="${esc}[35m"
  cyanf="${esc}[36m";    whitef="${esc}[37m"

  blackb="${esc}[40m";   redb="${esc}[41m";    greenb="${esc}[42m"
  yellowb="${esc}[43m"   blueb="${esc}[44m";   purpleb="${esc}[45m"
  cyanb="${esc}[46m";    whiteb="${esc}[47m"

  boldon="${esc}[1m";    boldoff="${esc}[22m"
  italicson="${esc}[3m"; italicsoff="${esc}[23m"
  ulon="${esc}[4m";      uloff="${esc}[24m"
  invon="${esc}[7m";     invoff="${esc}[27m"

  reset="${esc}[0m"
}
initializeANSI

cat << EOF
${yellowf}This is a phrase in yellow${redb} and red${reset}
${boldon}This is bold${ulon} this is italics${reset} bye bye
${italicson}This is italics${italicsoff} and this is not
${ulon}This is ul${uloff} and this is not
${invon}This is inv${invoff} and this is not
${yellowf}${redb}Warning I ${yellowb}${redf}Warning II${reset}
EOF

显示效果是这样的:

\033[33mThis is a phrase in yellow\033[41m and red\033[0m
\033[1mThis is bold\033[4m this is italics\033[0m bye bye
\033[3mThis is italics\033[23m and this is not
\033[4mThis is ul\033[24m and this is not
\033[7mThis is inv\033[27m and this is not
\033[33m\033[41mWarning I \033[43m\033[31mWarning II\033[0m

这是由于\033是一个特殊字符,代表ESC键值,可以在VIM中,进入插入模式,然后按Crl+v,再按ESC,得到一个"^["这样的字符。直接输入^和[这2个字符是没有用的。

esc="^["# if this doesn't work, enter an ESC directly

修改后,效果如下:

This is a phrase in yellow and red"

This is bold this is italics bye bye

This is italics and this is not

This is ul and this is not

This is inv and this is not

Warning I Warning II




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值