Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

24.2. tput

为输出着色

tput Color Capabilities:

tput setab [1-7] – Set a background color using ANSI escape
tput setb [1-7] – Set a background color
tput setaf [1-7] – Set a foreground color using ANSI escape
tput setf [1-7] – Set a foreground color

tput Text Mode Capabilities:

tput bold – Set bold mode
tput dim – turn on half-bright mode
tput smul – begin underline mode
tput rmul – exit underline mode
tput rev – Turn on reverse mode
tput smso – Enter standout mode (bold on rxvt)
tput rmso – Exit standout mode
tput sgr0 – Turn off all attributes

Color Code for tput:

0 – Black
1 – Red
2 – Green
3 – Yellow
4 – Blue
5 – Magenta
6 – Cyan	
7 – White
		
NORMAL=$(tput sgr0)  
RED=$(tput setaf 1)  
GREEN=$(tput setaf 2; tput bold)  
YELLOW=$(tput setaf 3)  
BLUE=$(tput setaf 4) 
MAGENTA=$(tput setaf 5) 
CYAN=$(tput setaf 6) 
WHITE=$(tput setaf 7) 
   
function exception(){
	echo -e "$WHITE$*$NORMAL"
}

function critical() {  
    echo -e "$RED$*$NORMAL"  
}  
   
function info() {  
    echo -e "$GREEN$*$NORMAL"  
}  
   
function warning() {  
    echo -e "$YELLOW$*$NORMAL"  
}  
  
function error(){
	echo -e "$MAGENTA$*$NORMAL"
} 
 
function debug(){
	echo -e "$CYAN$*$NORMAL"
} 
 
# To print critical  
critical "kernel error"  

# To print exception 
exception "file system exception"
   
# To print error  
error "The configuration file does not exist"  
   
# To print warning  
warning "You have to use higher version."
	
# To print info  
info "Task has been completed."	

# To print debug 
debug "This is a debug message."
		

24.2.1. Change the prompt color using tput

$ export PS1="\[$(tput bold)$(tput setb 4)$(tput setaf 7)\]\u@\h:\w $ \[$(tput sgr0)\]"