shell 颜色输出

#!/bin/bash 


function display_method(){
	clearDisplayMetho=0
	if [ $displayMethod -eq 0 ]; then 
		echo -e "\033[${color};${backgroup_color}m ${message} \033[${clearDisplayMetho}m"
	else
		echo -e "\033[${color};${backgroup_color};${displayMethod}m ${message} \033[${clearDisplayMetho}m"
	fi
}


function echo_color() {
	#displayMethodArr:
	#	0 终端默认设置
	#	1 高亮显示
	#	4 使用下划线
	#	5 闪烁
	#	7 反白显示
	#	8 不可见
	msg="Example: echo_color {red|green|yellow|blue|fuchsia|cyan|white} string [{0|1|4|5|7|8}]"
	if [[ -z $2 && $1 ]]; then
		echo_color red $msg
	fi
	displayMethodArr=[0,1,4,5,7,8]
	if [ ! -z $3 ]; then
		echo "${displayMethodArr[@]}"|grep -wq $3&& displayMethod=$3 || displayMethod=0
	else
		displayMethod=0
	fi
	
	
	red=31
	green=32
	yellow=33
	blue=34
	fuchsia=35
	cyan=36
	white=37
	backgroup_color=40
	message=$2
	color=`eval echo '$'"${1}"`
	# echo 'color' ${color}
    case $1 in
	red)
		display_method ${color} ${backgroup_color} ${message}
		;;
	green)
		display_method ${color} ${backgroup_color} ${message}
		;;
	yellow)
		display_method ${color} ${backgroup_color} ${message}
		;;
	blue)
		display_method ${color} ${backgroup_color} ${message}
		;;
	fuchsia)
		display_method ${color} ${backgroup_color} ${message}
		;;
	cyan)
		display_method ${color} ${backgroup_color} ${message}
		;;
	white)
		display_method ${color} ${backgroup_color} ${message}
		;;
	*) 
		echo_color red $msg
	esac
}

IFS_OLD=$IFS  #?当前值保存
IFS=$'\n'
echo_color $1 $2 $3
IFS=$IFS_OLD  #恢?之前保存的值

  

posted @ 2018-12-17 17:27  _Goku  阅读(291)  评论(1编辑  收藏  举报