Go to the first, previous, next, last section, table of contents.


Yet More Examples

Here are some more advanced examples of using tput; most involve some shell programming. Because the C shell's flow control (decision making) constructs differ from those of the other shells, these examples do not work under the C shell.

The following sequence of commands prints `I am infalible' and then crosses it out on terminals that can overstrike, and prints `I am on strike' on terminals that cannot.

if tput os; then
    echo 'I am infalible\r- -- ---------'
else
    echo 'I am on strike'
fi

The following example is a shell script that centers a line of text given as command line arguments. An alternative approach would be to have tput send the `rep' terminfo capability to print the multiple spaces instead of using the while loop.

COLUMNS=`tput cols` export COLUMNS # Get screen width.
echo "$@" | awk '
{ spaces = ('$COLUMNS' - length) / 2
  while (spaces-- > 0) printf (" ")
  print
}'

The following commands cause the terminal to save the current cursor position, print `Hello, World' centered in the screen in reverse video, then return to the original cursor position.

COLUMNS=`tput cols`
LINES=`tput lines`
line=`expr $LINES / 2`
column=`expr \( $COLUMNS - 6 \) / 2`
tput sc
tput cup $line $column
tput rev
echo 'Hello, World'
tput sgr0
tput rc

The middle three lines of the above example can also be written using `--standard-input'.

tput --standard-input <<EOF
sc
cup $line $column
rev
EOF


Go to the first, previous, next, last section, table of contents.