I have some logs syntax. For example if I got error I get something like this:
[ERROR] You get error here
How I can intercept terminal (ubuntu for example) and color this text?
I have some logs syntax. For example if I got error I get something like this:
[ERROR] You get error here
How I can intercept terminal (ubuntu for example) and color this text?
You can do something like this:
## Color coded message
ERR=$(printf "%-25s" "$(echo -e "\033[1;31m[ ERROR ]\033[0m")")
OKY=$(printf "%-25s" "$(echo -e "\033[1;32m[ OK ]\033[0m")")
WRN=$(printf "%-25s" "$(echo -e "\033[1;33m[ WARN ]\033[0m")")
INF=$(printf "%-25s" "$(echo -e "\033[1;36m[ INFO ]\033[0m")")
echo -e "${ERR}You get error here!!"
echo -e "${OKY}This is a success!!"
echo -e "${WRN}You have been warned!!"
Hope it helps. Cheers!!
ER="\033[0;31m"
OK="\033[0;32m"
WR="\033[0;33m"
IN="\033[0;34m"
NC="\033[0m"
#
echo -e "${ER}[ERROR] You get Error here!!${NC}"
echo -e "${OK}[OKAY ] This is a Success!!${NC}"
echo -e "${WR}[WARN ] You have been Warned!!${NC}"
echo -e "${IN}[INFO ] You have been Informed!!${NC}"
or, create a function() something like this:
function txtColor()
{
local typ="$1"
local msg="${@:2}"
if [ $typ == "ER" ]; then
echo -e "\033[0;31m[ ERROR ] ${msg}\033[0m"
elif [ $typ == "OK" ]; then
echo -e "\033[0;32m[ OKAY ] ${msg}\033[0m"
elif [ $typ == "WR" ]; then
echo -e "\033[0;33m[ WARN ] ${msg}\033[0m"
elif [ $typ == "IN" ]; then
echo -e "\033[0;34m[ INFO ] ${msg}\033[0m"
else
echo -en "\033[1;33m[ WARN ] Wrong message type: "
echo -e "${typ}\033[0m\n\t ${msg}"
fi
}
and use it anywhere you like:
txtColor "ER" You got Error here!!
txtColor OK "This is a success!!"
txtColor "WR" "You have been warned!!"
txtColor OL Text without any color!!
(change the spacing ans.or type as you please)
FYI, replace all the 0;
with 1;
if you want bold text in stead.
Cheers!!