2015/7/7

更改Shell輸出文字的顏色、底線、粗體等控制

要在Shell輸出文字中有顏色改變、底線與粗體效果,需要用到如下的控制碼:
printf "'\033[0;31m'Hello'\033[0m'"
會在螢幕上顯示紅色的Hello,但這樣要記住所有效果的代碼也不是辦法,可以將每種效果定義為一種變數,如此要調用就會簡單多了,如下可以達到相同效果:
Red='\033[0;31m'
NC='\033[0m'

printf "${Red}Hello${NC}"

完整的效果定義
#!/bin/bash
# File: color.sh

# Regular Colors
Black='\033[0;30m'
Red='\033[0;31m'
Green='\033[0;32m'
Yellow='\033[0;33m'
#Orange='\033[0;33m'
Blue='\033[0;34m'
Purple='\033[0;35m'
Cyan='\033[0;36m'
White='\033[0;37m'

# Bold
BBlack='\033[1;30m'
BRed='\033[1;31m'
BGreen='\033[1;32m'
BYellow='\033[1;33m'
BBlue='\033[1;34m'
BPurple='\033[1;35m'
BCyan='\033[1;36m'
BWhite='\033[1;37m'

# Underline
UBlack='\033[4;30m'
URed='\033[4;31m'
UGreen='\033[4;32m'
UYellow='\033[4;33m'
UBlue='\033[4;34m'
UPurple='\033[4;35m'
UCyan='\033[4;36m'
UWhite='\033[4;37m'

# Background
GBlack='\033[40m'
GRed='\033[41m'
GGreen='\033[42m'
GYellow='\033[43m'
GBlue='\033[44m'
GPurple='\033[45m'
GCyan='\033[46m'
GWhite='\033[47m'

# High Intensity
IBlack='\033[0;90m'
IRed='\033[0;91m'
IGreen='\033[0;92m'
IYellow='\033[0;93m'
IBlue='\033[0;94m'
IPurple='\033[0;95m'
ICyan='\033[0;96m'
IWhite='\033[0;97m'

# Bold High Intensity
BIBlack='\033[1;90m'
BIRed='\033[1;91m'
BIGreen='\033[1;92m'
BIYellow='\033[1;93m'
BIBlue='\033[1;94m'
BIPurple='\033[1;95m'
BICyan='\033[1;96m'
BIWhite='\033[1;97m'

# High Intensity Background
GIBlack='\033[0;100m'
GIRed='\033[0;101m'
GIGreen='\033[0;102m'
GIYellow='\033[0;103m'
GIBlue='\033[0;104m'
GIPurple='\033[0;105m'
GICyan='\033[0;106m'
GIWhite='\033[0;107m'

LightGray='\033[0;37m'
DarkGray='\033[1;30m'
LightRed='\033[1;31m'
LightGreen='\033[1;32m'
Yellow='\033[1;33m'
LightBlue='\033[1;34m'
LightPurple='\033[1;35'
LightCyan='\033[1;36'

NC='\033[0m'

效果Demo
#!/bin/bash
# File: color_demo.sh
. color.sh

printf "${Black}Black${NC}\n"
printf "${Red}Red${NC}\n"
printf "${Green}Green${NC}\n"
printf "${Yellow}Yellow${NC}\n"
printf "${Blue}Blue${NC}\n"
printf "${Purple}Purple${NC}\n"
printf "${Cyan}Cyan${NC}\n"
printf "${White}White${NC}\n"

printf "${BBlack}BBlack${NC}\n"
printf "${BRed}BRed${NC}\n"
printf "${BGreen}BGreen${NC}\n"
printf "${BYellow}BYellow${NC}\n"
printf "${BBlue}BBlue${NC}\n"
printf "${BPurple}BPurple${NC}\n"
printf "${BCyan}BCyan${NC}\n"
printf "${BWhite}BWhite${NC}\n"

printf "${UBlack}UBlack${NC}\n"
printf "${URed}URed${NC}\n"
printf "${UGreen}UGreen${NC}\n"
printf "${UYellow}UYellow${NC}\n"
printf "${UBlue}UBlue${NC}\n"
printf "${UPurple}UPurple${NC}\n"
printf "${UCyan}UCyan${NC}\n"
printf "${UWhite}UWhite${NC}\n"

printf "${GBlack}GBlack${NC}\n"
printf "${GRed}GRed${NC}\n"
printf "${GGreen}GGreen${NC}\n"
printf "${GYellow}GYellow${NC}\n"
printf "${GBlue}GBlue${NC}\n"
printf "${GPurple}GPurple${NC}\n"
printf "${GCyan}GCyan${NC}\n"
printf "${GWhite}GWhite${NC}\n"

printf "${IBlack}IBlack${NC}\n"
printf "${IRed}IRed${NC}\n"
printf "${IGreen}IGreen${NC}\n"
printf "${IYellow}IYellow${NC}\n"
printf "${IBlue}IBlue${NC}\n"
printf "${IPurple}IPurple${NC}\n"
printf "${ICyan}ICyan${NC}\n"
printf "${IWhite}IWhite${NC}\n"

printf "${BIBlack}BIBlack${NC}\n"
printf "${BIRed}BIRed${NC}\n"
printf "${BIGreen}BIGreen${NC}\n"
printf "${BIYellow}BIYellow${NC}\n"
printf "${BIBlue}BIBlue${NC}\n"
printf "${BIPurple}BIPurple${NC}\n"
printf "${BICyan}BICyan${NC}\n"
printf "${BIWhite}BIWhite${NC}\n"

printf "${GIBlack}GIBlack${NC}\n"
printf "${GIRed}GIRed${NC}\n"
printf "${GIGreen}GIGreen${NC}\n"
printf "${GIYellow}GIYellow${NC}\n"
printf "${GIBlue}GIBlue${NC}\n"
printf "${GIPurple}GIPurple${NC}\n"
printf "${GICyan}GICyan${NC}\n"
printf "${GIWhite}GIWhite${NC}\n"
呈現結果

沒有留言:

張貼留言