5

I'm running a watch on a command to monitor the number of open connections for a service we're diagnosing. I need to keep an eye on it while doing other work, to help other developers identify when the service is acting up.

The result of the watch is a number between 1 and 5000. I'd like to show the number as green when it's below 500, yellow when its between 501 and 4000, and red for above 4000.

Is there a command which can set the color easily based on the value of my command?

STW
  • 1,908

4 Answers4

3

CCZE may be what you're looking for:
man page over at Ubuntu manuals

enter image description here

Installation

On Debian-based systems, you can easily install it via

sudo apt-get install ccze

Usage

Use it like this:

tail -f /var/log/syslog | ccze

Plugins

If the default coloring behavior of ccze is not to your liking, you can extend the functionality through plugins. Sadly, I have never written any as I'm served well with the default behavior, so I can only direct you to the ccze-plugin man page.

Oliver Salzburg
  • 89,072
  • 65
  • 269
  • 311
3

I wrote this small script that I named color:

#!/bin/bash

result=$($@)

GREEN="1;32m"
YELLOW="1;33m"
RED="1;31m"

if [ "$result" -lt "500" ]
then
        echo -e "\033[$GREEN  $result  \033[0m" ;
elif [ "$result" -ge "500" -a "$result" -le "4000" ]
then
        echo -e "\033[$YELLOW  $result  \033[0m" ;
else
        echo -e "\033[$RED  $result  \033[0m" ;
fi

You can now run:

watch --color ./color <your command here>

Make sure your command just output a number. Else you got to handle the output correctly and assign it to result variable.

janos
  • 3,505
fmanco
  • 2,517
1

I think you can color the man pages, however (after running several Google searches, I don't think what you're looking for exists.

wizlog
  • 13,573
1

You could use tput to to put some color in your life. Examples are

tput setf 3 # textcolor green
tput setf 6 # red
tput sgr0 # back to normal
echo "$(tput setf 6)$line$(tput setf sgr0)"

This and more fun with tput on http://www.ibm.com/developerworks/aix/library/au-learningtput/

ott--
  • 2,251