colorize

Portrait de nepta

Script bash permettant de coloriser la sortie d'une application à l'aide de regexp

exemple d'utilisation:

$ echo "hello word" |./colorize --blue hello --purple word

nous donne:
hello word

Commentaires

Portrait de nepta

amélioration utilisant la récursion proposé par colona:

#!/bin/bash
function sub
{
	TEMP=`getopt -o r:,g:,b: --long black:,red:,green:,yellow:,blue:,purple:,cyan:,white: \
		  -n 'colorize' -- "$@"`
 
	if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
 
	# Note the quotes around `$TEMP': they are essential!
	eval set -- "$TEMP"
 
	case "$1" in
		--black) COLOR="mt=1;30" ; PATTERN=$2 ; shift 2 ;;
		--red) COLOR="mt=1;31" ; PATTERN=$2 ; shift 2 ;;
		--green) COLOR="mt=1;32" ; PATTERN=$2 ; shift 2 ;;
		--yellow) COLOR="mt=1;33" ; PATTERN=$2 ; shift 2 ;;
		--blue) COLOR="mt=1;34" ; PATTERN=$2 ; shift 2 ;;
		--purple) COLOR="mt=1;35" ; PATTERN=$2 ; shift 2 ;;
		--cyan) COLOR="mt=1;36" ; PATTERN=$2 ; shift 2 ;;
		--white) COLOR="mt=1;37" ; PATTERN=$2 ; shift 2 ;;
		--) cat ; exit ;;
		*) cat ; exit 1 ;;
	esac
	shift 2
	GREP_COLORS="$COLOR" grep -E --color=always "$PATTERN|" |sub arg
}
 
sub "$@"