Created
June 16, 2010 18:06
-
-
Save waffle2k/441050 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# color-echo.sh: Echoing text messages in color. | |
# Modify this script for your own purposes. | |
# It's easier than hand-coding color. | |
black='\E[30;40m' | |
red='\E[31;40m' | |
green='\E[32;40m' | |
yellow='\E[33;40m' | |
blue='\E[34;40m' | |
magenta='\E[35;40m' | |
cyan='\E[36;40m' | |
white='\E[37;40m' | |
cecho () # Color-echo. | |
# Argument $1 = message | |
# Argument $2 = color | |
{ | |
local default_msg="No message passed." | |
# Doesn't really need to be a local variable. | |
message=${1:-$default_msg} # Defaults to default message. | |
color=${2:-$black} # Defaults to black, if not specified. | |
echo -e "$color" | |
echo "$message" | |
tput sgr0 # tput sgr0 to normal. | |
return | |
} | |
# Now, let's try it out. | |
# ---------------------------------------------------- | |
cecho "Feeling blue..." $blue | |
cecho "Magenta looks more like purple." $magenta | |
cecho "Green with envy." $green | |
cecho "Seeing red?" $red | |
cecho "Cyan, more familiarly known as aqua." $cyan | |
cecho "No color passed (defaults to black)." | |
# Missing $color argument. | |
cecho "\"Empty\" color passed (defaults to black)." "" | |
# Empty $color argument. | |
cecho | |
# Missing $message and $color arguments. | |
cecho "" "" | |
# Empty $message and $color arguments. | |
# ---------------------------------------------------- | |
#echo | |
exit 0 | |
# Exercises: | |
# --------- | |
# 1) Add the "bold" attribute to the 'cecho ()' function. | |
# 2) Add options for colored backgrounds. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment