Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

this is the script color

#!/bin/bash 

ESC_SEQ="\x1b["
COL_RESET=$ESC_SEQ"39;49;00m"
  case $1 in
    red )    COL_START=$ESC_SEQ"31;01m"
  ;;
    green)   COL_START=$ESC_SEQ"32;01m"
  ;;
    yellow ) COL_START=$ESC_SEQ"33;01m"
  ;;
    blue )   COL_START=$ESC_SEQ"34;01m"
  ;;
    mage )   COL_START=$ESC_SEQ"35;01m"
  ;;
    cyan )   COL_START=$ESC_SEQ"36;01m"
  ;;
  esac

  echo -ne $COL_START
  while read text;
  do
  echo  $text
  done
  echo -ne $COL_RESET

when i do

curl localhost:8080/

it responds with the usual curl outputm HOOOOORAY!

when i do

curl localhost:8080/ | color red

it does not color the output how I expect it to ...... `CRY'

I don`t think I am calling read correctly for the situation.

share|improve this question
    
if i pipe from /proc/${$}/fd/0 it seems to work but i don`t kno why –  James Andino Jul 21 '13 at 7:57
1  
It works for me with curl :[ You may be interested in this: cognitivedissonance.ca/cogware/utf8_colorize/… There's a README in the tarball. –  goldilocks Jul 21 '13 at 9:14
    
what an intense little script, never know though when you might have to drop 80 000 000 emails though. –  James Andino Jul 24 '13 at 19:01
add comment

1 Answer 1

It works for me exactly as you have laid out in the questions. You may want to check to see that your escapes are correct. They might not be the correct escapes for your $TERM which can be easily fixed by using tput to query for the correct escapes for you like so:

COL_RESET=$(tput sgr0)
...
red )    COL_START=$(tput setaf 1)
green )    COL_START=$(tput setaf 2)
blue )    COL_START=$(tput setaf 4)
...

Another example of using these can be found here.

You also may want to change the way you call echo in case you are getting interference there. The man page for echo suggests the following:

Due to shell aliases and built-in `echo' command, using an unadorned `echo' 
interactively or in a script may get you different functionality than that
described here.  Invoke it via `env' (i.e., `env echo ...') to avoid
interference from the shell.
share|improve this answer
    
this confirms my thinking very useful, ty –  James Andino Jul 24 '13 at 19:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.