Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Change Text Attributes From Within a Script 1

Status
Not open for further replies.

YerMom

Programmer
Oct 3, 2006
127
US
I would like my script to output text in various "formats" such as different colors, or bright white vs regular white.

Is this possible to do?

Thanks.
 
man tput

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Yes, it is. But it will be dependent on the capability of the 'screen'. There are various 'escape sequences' that can display the text as 'bold', 'underline', 'reverse video', 'blinking' and in a variety of colours, and even as 'double height &/or double width'. Try a google search for: terminal + escape + sequences

When writing a scipt, for 'printability', I use the following construct in it:
ESC=`echo "E" | tr E '\033'`
echo "$ESC[1m This is a bold statement. $ESC[0m"


I hope that helps.

Mike
 
Mike, why hard-coding escape sequences?
echo "$(tput bold)This is a bold statement.$(tput sgr0)"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV and Mike.

When I try:
Code:
echo "$(tput bold)This is a bold statement.$(tput sgr0)"
there is no text formatting, and the output is litterally:
Code:
$(tput bold)This is a bold statement.$(tput sgr0)

When I try:
Code:
ESC=`echo "E" | tr E '\033'`
   echo "$ESC[1m This is a bold statement. $ESC[0m"
It works fine.

I'm working in the tcsh shell -- I don't know if that matters.



 
In tcsh, try it this way...
Code:
echo "`tput bold`This is a bold statement.`tput sgr0`"
 
Hi PHV,

You asked: why hard-coding escape sequences?
Well, I was originally a VMS Systems Manager and learnt DCL. We had various VT terminals and I used the 'escape sequence' technique/construct for switching between VT modes (such as VT100, VT52, terminal graphics and Regis) as well as for enhancing the output to the screen (with bold, underline, reverse-video, blinking and coloured text, etc). When I switched to UNIX, I translated 'utility' scripts to run in Bourne or Korn shells. They worked, so it seemed natural to carry on using 'escape sequences'. They seem more powerful, require less typing, and have a greater variety of possibilites than 'tput'.

I hope that helps.

Mike
 
The point in using tput is: the escape-sequences will be generated at run time for all supported terminal makes/models: DEC VTxxx, IBM 31xx, HP, Wyse, Beehive, Televideo, ... Granted, some of these makes and models of terminals have all but disappeared, but still...
Also, terminal emulation software packages on PCs can emulate different makes/models of terminals, so it is best not to assume anything while scripting, and use tput (plus the TERM shell variable) to generate the proper escape sequences for the desired tput keywords...



HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top