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,836
  • 4
  • 17
  • 33
  • I'm not familiar with linux :) "watch" - means show std out in terminal? – Maximus Jun 19 '12 at 18:45
  • 1
    @Maximus It's a linux utility which repeatedly executes a command and displays the output in a terminal at a specified interval. `watch -n 1 ps aux` -- A poor man's process monitor, for example (execute `ps aux` to list processes every `1` second and print the results). – Darth Android Jun 19 '12 at 18:51
  • Just to tell you: there is colordiff for all those nasty diff commands. Eg. svn diff |colordiff -> woho! – PythonLearner Jun 19 '12 at 19:31

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
  • 86,445
  • 63
  • 260
  • 306
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,297
  • 1
  • 22
  • 31
fmanco
  • 2,497
  • 18
  • 19
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,277
  • 24
  • 77
  • 116
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,201
  • 1
  • 15
  • 15