Hi.
Here is an example of hiding a lot of the code (like tput
) and allowing a pattern to be specified for conditional high-lighting:
#!/usr/bin/env bash
# @(#) s1 Demonstrate colorization of text.
# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C my-hilite
pl " Results, file missing:"
FILE=test1
rm -f $FILE
if [ -f "$FILE" ];
then
echo "File $FILE does exist." |
my-hilite -f green
else
echo "File $FILE does NOT exist." |
my-hilite -f red
fi
touch $FILE
pl " Results, file present:"
if [ -f "$FILE" ];
then
echo "File $FILE does exist." |
my-hilite -f green
else
echo "File $FILE does NOT exist." |
my-hilite -f red
fi
pl " Results, if only string \"File\" should be colored:"
echo " The File is unimportant, the contents are what matters." |
my-hilite -f blue "File"
exit 0
producing:
./s1 | ansifilter -B
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-7-amd64, x86_64
Distribution : Debian 8.11 (jessie)
bash GNU bash 4.3.30
my-hilite - ( local: RepRev 1.2, ~/bin/my-hilite, 2019-04-11 )
-----
Results, file missing:
File test1 does NOT exist.
-----
Results, file present:
File test1 does exist.
-----
Results, if only string "File" should be colored:
The File is unimportant, the contents are what matters.
The ansifilter
is so that the colors show up here, and would not be needed on a terminal -- I use Konsole, I'm assuming that Mate will be similar.
I'll try to attach the script my-hilite
as a text file (in this or a following post) so that you may rename it, modify it, etc.
Best wishes ... cheers, drl
--- Post updated at 10:56 ---
Hi.
Cannot seem to get attached file to work, so copied here ... cheers, drl
#!/usr/bin/env bash
# @(#) my-hilite Colorize regex pattern matches, insert tput codes with sed.
# $Id: my-hilite,v 1.2 2019/04/11 15:12:31 drl Exp $
# Home:
# http://unix.stackexchange.com/questions/46562/how-do-you-colorize-only-some-keywords-for-a-bash-script
# Utility functions: print-as-echo, print-line-with-visual-space, debug.
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
# Short circuit for debug -- MUST BE FIRST ARGUMENT.
if [[ $# -gt 0 && "$1" =~ ^-de*b*u*g* ]]
then
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
shift;
else
db() { : ; }
fi
color_to_num () {
case $1 in
black) echo 0;;
red) echo 1;;
green) echo 2;;
yellow) echo 3;;
blue) echo 4;;
purple) echo 5;;
cyan) echo 6;;
white) echo 7;;
*) echo 0;;
esac
}
# default values for foreground and background colors
bg=
fg=
bold="$(tput bold)"
italics=""
boundary=""
while getopts f:b:sli option; do
case "$option" in
f) fg="$OPTARG";;
b) bg="$OPTARG";;
s) bold="";;
l) boundary=".*";;
i) italics="$(tput sitm)";;
esac
done
shift $(($OPTIND - 1))
if [ $# -le 0 ]
then
pattern="^.*$"
else
pattern="$*"
fi
if [ -n "$fg" ]; then
fg=$(tput setaf $(color_to_num $fg))
fi
if [ -n "$bg" ]; then
bg=$(tput setab $(color_to_num $bg))
fi
if [ -z "$fg$bg" ]; then
fg=$(tput smso)
fi
db " boundary = :$boundary:"
db " pattern = :$pattern:"
sed "s/${boundary}${pattern}${boundary}/${bold}${italics}${fg}${bg}&$(tput sgr0)/g"
exit