[ Level 3 ] Bash script for color ansi.
#!/bin/bash ## Get ansi color code ## Usage: ## getAnsiColorCode (f)orward_ground/(b)ack_ground color_name ## ex. ## getAnsiColorCode f/b black/red/green/yellow/blue/magenta/cyan(purple)/white/default getAnsiColorCode() { sReturnCode="" fb='f' color='default' if [ $# -eq 1 ] then color="$1" elif [ $# -gt 1 ] then fb="$1" color="$2" fi if [ "$fb" == "b" ] then sReturnCode='4' else sReturnCode='3' fi case $color in black) sReturnCode="${sReturnCode}0" ;; red) sReturnCode="${sReturnCode}1" ;; green) sReturnCode="${sReturnCode}2" ;; yellow) sReturnCode="${sReturnCode}3" ;; blue) sReturnCode="${sReturnCode}4" ;; magenta) sReturnCode="${sReturnCode}5" ;; cyan|purple) sReturnCode="${sReturnCode}6" ;; white) sReturnCode="${sReturnCode}7" ;; *) sReturnCode="${sReturnCode}9" ;; esac echo ${sReturnCode}m } ## color echo message ## Usage: ## cecho fg_color bg_color ## ex. ## cecho black red message cecho() { fgColor='default' bgColor='default' message='' if [ $# -ne 0 ] then if [ $# -eq 1 ] then echo $1 else if [ $# -eq 2 ] then fgColor=$1 message="$2" else fgColor=$1 bgColor=$2 message="$3" fi fgCode=`getAnsiColorCode f $fgColor` bgCode=`getAnsiColorCode b $bgColor` echo -en "\033[$fgCode\033[$bgCode$message\033[0m"; fi fi } ## status echo message ## Usage: ## secho OK/FAIL message ## ex. ## secho OK/FAIL message secho() { status='OK' message='' if [ $# -eq 1 ] then message="$1" else status="$1" message="$2" fi echo -n "$message ... [" if [ "$1" == "OK" ] then cecho green OK elif [ "$1" == "FAIL" ] then cecho red FAIL else cecho yellow "$1" fi echo "]" } secho OK "Run $0 is"
$ ./testColor2.sh
Run ./testColor2.sh is ... [OK]
Wish this helps.
regards,
Stanley Huang
Comments
Post a Comment