Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
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! – Valentin Jun 19 '12 at 19:31
up vote 3 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

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

share|improve this answer

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/

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .