ShellScript: Interesting command this ‘tput’


Making shell scripts, sometimes, require some visual presentation. Maybe you need make letters bold, or use a white background. Maybe we want to write in a precise position of the screen. For this worthless activities, I found ‘tput’.

tput is a quite simple command which comes with a basic Solaris installation. I reads termcap database information and let’s you manage it. For now, I know not much about the options and possibilities of this command (man page available), but can show some basic functions:

-Makes text bold:

tput bold

-Returning to normal text:

tput sgr0

-Use a white background:

tput smso

-Undo white background:

tput rmso

-Knowing rows and cols of a terminal:

tput cols
tput lines

-Reseting the terminal:

tput reset

-Initializing the terminal:

tput init

-Putting the cursor wherever we want(in the screen):

tput cup row col

It gets some options:

-T: Tells tput which terminal type we want to use (vt100,5620,…). It reads the TERM variable, so if it is defined, this option is not needed.

-S : is used to tells tput to read several capabilities from the standard input (read the man page for some examples)

Here comes a nonsense example(sorry for the indentation, I’m quite new on this blogs):

#!/bin/bash

# Let's get terminal dimensions
MY_COLS=`tput cols`
MY_ROWS=`tput lines`

# This definitions will be used
# as codes to the terminal.
BOLD=`tput bold`
NORM=`tput sgr0`

# This script will draw a platform going
# from left to right. Crtl+C to stop.

PLATFORM="${BOLD} <====> ${NORM}"
END=0
(( END=MY_COLS-8 ))
clear
while [ 1 ]
do
for (( i=1;i<END;i++ ))
do
tput cup 10 $i
echo $PLATFORM
done
for (( i=END;i!=1;i-- ))
do
tput cup 10 $i
echo $PLATFORM
done
done

I don’t know how many capabilities could be there.

So, if don’t have nice commands like dialog, whiptail and so on, this can help personalizing your scripts.

You have been reading: Another useless post, from Alex Sola

VN:F [1.9.12_1141]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.12_1141]
Rating: +1 (from 1 vote)
ShellScript: Interesting command this 'tput', 10.0 out of 10 based on 1 rating Sphere: Related Content

FacebookTwitterGoogle BookmarksLinkedInShare

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

,

  1. No comments yet.
(will not be published)