Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

29.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."
		

29.2.1. Change the prompt color using tput

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