Outputting colours in HP-UX scripts

Hi all,

This is my first ever posting, so please be gentle with me :slight_smile:

I'm trying to write a script in HP-UX which outputs text in different colours, but although I can get the script to output different colours to the screen, I can't get it to write different colours to a file. Take the following example:

 
FGNORMS=$(printf "\033[m")
FGGREEN=$(printf "\033[1;32m")
FGBLUE=$(printf "\033[1;36m")
echo $FGGREEN"Business Unit 9 Totals                  : "$FGNORMS
echo $FGBLUE"Business Unit 9 Totals                  : "$FGNORMS
echo $FGGREEN"Business Unit 9 Totals                  : "$FGNORMS >> testlogfile.txt
echo $FGBLUE"Business Unit 9 Totals                  : "$FGNORMS >> testlogfile.txt

When I run it from my terminal, it outputs:

Business Unit 9 Totals                  :
Business Unit 9 Totals                  :

where the first line is green, and the second is blue.

The testlogfile.txt, however, shows:

^[[1;32mBusiness Unit 9 Totals                  : ^[[m
^[[1;36mBusiness Unit 9 Totals                  : ^[[m

with no colours.

Anyone help me please?

It will output the text colored in desired color (when using cat or alike).

In the file you will find special escape sequences for displaying, in your case, colored text, but it can be used for all sorts of stuff (bold,underline etc.), you won't actually get green text inside the file when you edit it with editor.

Thanks, Peasant, but what file are you referring to? I don't want to edit the output file from my script, just view it.

Then just use cat testlogfile.txt , and should output green text (blue or whatever you set it to), if you edit it with editor you will see your text enclosed in special escape chars as per example above.

Hope that clears things out.

Regards
Peasant.

Hi neilharvey...

Take a look at how I do it here on a default Linux shell/terminal:-

Your first reset esc sequence should read "\033[0m" for a reset to the default colours...

Hope this helps...

---------- Post updated at 02:08 PM ---------- Previous update was at 12:46 PM ----------

Also try this as a starting point...

# Note double backslash...

printf "\\x1B[1;32;40m Some text...\n" > text.txt

cat text.txt

Hope this is helpful...

Bazza, G0LCU...