#!/bin/bash
#"Colorizing" Scripts
#First,define functions.
function subColor()
{
Fg=$1
Bg=$2
SetColor="\E[""$Fg;$Bg""m"
UseColor="\033[""$3""m"
EndColor="\033[0m"
Content=$e
echo -en "$SetColor""$UseColor"$Content"$EndColor"
}
function ShowHelp()
{
echo "Error!"
echo "Your parameters were $a,$b,$c,they are unexpected parameters."
echo "Show help file or continue?(h|c)"
read Choice
case $Choice in
h|H)
echo "This is a script for coloring characters and strings."
echo "There are four parameters.Parameters are seperated by spacebars."
echo "The frst parameter is a number ranged from 1 to 10,represents the foreground color."
echo "The second parameter is a number ranged from 1 to 10,represents the background color."
Color 1 8 2 "1 red";echo
Color 2 8 2 "2 green";echo
Color 3 8 2 "3 yellow";echo
Color 4 8 2 "4 blue";echo
Color 5 8 2 "5 magenta";echo
Color 6 8 2 "6 cyan";echo
Color 7 8 2 "7 gray";echo
Color 8 9 2 "8 white";echo
Color 9 8 2 "9 white";echo
Color 10 9 2 "10 black";echo
echo "The third parameter is a number ranged from 1 to 9,represents the style of the characters."
Color 10 8 1 "1 lighter,and bold";echo
Color 9 8 4 "4 draw a line under the string.";echo
Color 9 8 5 "5 coruscate the string.";echo
Color 9 8 7 "7 swap the foreground color and the background color";echo
Color 9 8 9 "9 draw a deleting line";echo
echo "The fourth parameter is the content you wanna clolor,a string."
echo "Thanks for using this script ! "
echo "Script halted."
exit 1
;;
c|C)
echo "script halted."
exit 1
;;
*)
ShowHelp
esac
}
function ParaCheck()
{
if [ $a -le 0 ] || [ $b -le 0 ] || [ $c -le 0 ] || [ $a -gt 10 ] || [ $b -gt 10 ] || [ $c -gt 9 ]
then
ShowHelp
else
if [ $a -eq 10 ]
then
a=30
else
a=30+$a
fi
if [ $b -eq 10 ]
then
b=40
else
b=40+$b
fi
fi
}
function GenString()
{
e=""
declare -i f=1
for d in $@
do
if [ $f -eq 1 ] || [ $f -eq 2 ] || [ $f -eq 3 ]
then
e=$e
elif [ $f -eq 4 ]
then
e=$d
else
e="$e $d"
fi
let f=$f+1
done
}
function Color()
{
declare -i a=$1
declare -i b=$2
declare -i c=$3
ParaCheck
GenString $@
subColor $a $b $c $e
}
#Here is where the script begins.
Color $@
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/53398/showart_419411.html