Here is an example of hiding a lot of the code (like tput) and allowing a pattern to be specified for conditional high-lighting:
Code:
#!/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:
Code:
./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
Code:
#!/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
Hello,
I am using RHEL 6.1 on VMware
I am searching for a way to change background color (not line by line color wich one can using tput command)
basically changing the color of the whole screen to white instead of the default black and changing font color to black and alos would like to... (2 Replies)
hi Folks,
Can anyone help with changing the color of the words in a linux shell script?
I get how to change default background etc in putty, but for some reason the text in the script has different colors for different parts of the cript.
Is there a way to have one color in a linux shell... (5 Replies)