Now we have gone over the most common names of tput that you will probably use, let’s see how we can use tput in scripts.
We can use the output of all tput names and store them inside more meaningful variable names. The format to do this is:
variable_name= `tput name`
To use the Boolean tput output use the if statement:
STATUS_LINE=‘tput hs‘ if $STATUS_LINE; then echo "your terminal has a status line" else echo "your terminal has NO status line" fi
Here’s a script that has assigned the tput bel and cl to more meaningful variable names.
$ pg tput1
#!/bin/sh
BELL=`tput bel`
CLEAR=`tput cl`
echo $BELL
echo $CLEAR
The following script changes a couple of video attributes and turns the cursor off and on:
$ pg tput2
#!/bin/sh
BOLD=`tput bold`
REV=`tput rev`
NORMAL=`tput sgr0`
CURSOR_OFF=`tput civis`
CURSOR_ON=`tput cnorm`
tput init
#turn cursor off, highlight text, reverse some text, cursor on
echo $CURSOR_OFF
echo "${BOLD} WELCOME TO THE PIZZA PLACE${NORMAL}"
echo -e "
${REV} WE ARE OPEN 7 DAYS A WEEK${NORMAL}"
echo $CURSOR_ON
Please note that if you are using an emulator, you may have problems making the cursor invisible. This could be because:
Some emulators do not trap the control character that makes the cursor invisible. I had to ask the author of the software emulator that I am using to make amendments to his source code so the cursor could be turned off.
Some older versions of the tput civis command in my opinion do not seem to work properly.
The control character for turning the cursor off is ?25l (that’s the letter l). The character for turning it back on is ?25h.
All control characters start with an escape sequence, usually the escape key followed by [, then the actual sequence to turn some terminal attribute on or off.
You can use two different methods to generate escape sequences. The following table lists both methods depending on what system you have. The third method will work no matter what UNIX or LINUX variation you have got because the control sequence is embedded in the echo statement. This third method is the one we will use in the book.
To send an escape sequence to turn the cursor off:
LINUX/BSD | echo -e " 33[?25l" |
System V | echo " 33[?25l" |
Generic method | echo "<CTRL-V><ESCAPE>[?25l" |
The