Using tput command:
#!/bin/bash
# SCRIPT: digclock.sh
# USAGE: ./digiclock &
# PURPOSE: Displays time and date in the top right corner of the
# screen using tput command.
# To stop this digclock use command "kill pid"
################################################################
####################### VARIABLE DECLARATION ###################
# To place the clock on the appropriate column, subtract the
# length of $Time and $Date, which is 22, from the total number
# of columns
Columns=$(tput cols)
Startpoint=$(($Columns-22))
# If you're in an X Window System terminal,you can resize the
# window, and the clock will adjust its position because it is
# displayed at the last column minus 22 characters.
Color1=`tput setab 2` # Green background color for time
Color2=`tput setab 6` # Cyan background color for date
Normal=`tput sgr0` # back to normal screen colors
####################### MAIN PROGRAM ###########################
# The script is executed inside a while without conditions
while :
do
Time=`date +%r`
Date=`date +"%d-%m-%Y"`
tput sc #Save the cursor position&attributes
tput cup 0 $Startpoint
# You can also use bellow one liner
# tput cup 0 $((`tput cols`-22))
# But it is not efficient to calculate cursor position for each
# iteration. That's why I placed variable assignment before
# beginning of the loop.
# print time and date in the top right corner of the screen.
echo -n $Color1$Time $Color2$Date$Normal
# restore the cursor to whatever was its previous position
tput rc
# Delay for 1 second
sleep 1
done
Using ANSI escape sequences:
The ANSI escape sequences don't work in all terminal emulators, but
they do fine in xterm. Here's the script:
#!/bin/bash
# SCRIPT: digclock.sh
# USAGE: ./digiclock &
# PURPOSE: Displays time and date in the top right corner of the
# screen using ANSI escape sequences.
# To stop this digclock use command kill pid.
################################################################
#################### VARIABLE DECLARATION ######################
# To place the clock on the appropriate column, subtract the
# length of $Time and $Date, which is 22, from the total number
# of columns
Columns=$(tput cols)
Startpoint=$(($Columns-22))
# If you're in an X Window System terminal,you can resize the
# window, and the clock will adjust its position because it is
# displayed at the last column minus 22 characters.
########################### MAIN PROGRAM #######################
# The script is executed inside a while without conditions.
while :
do
Time=`date +%r`
Date=`date +"%d-%m-%Y"`
echo -en "\033[s" #save current screen position & attributes
tput cup 0 $Startpoint
# You can also use bellow one liner.
# tput cup 0 $((`tput cols`-22))
# But it is not efficient to calculate cursor position for each
# iteration. That's why I placed variable assignment before
# beginning of the loop
# print time and date in the top right corner of the screen
echo -en "\033[42m$Time \033[46m$Date\033[0m"
#restore current screen position & attributes
echo -e -n "\033[u"
#Delay for 1 second
sleep 1
done
Save the script as digclock.sh,change permissions to 755 using chmod,
and run it with ./digclock.sh & or . digclock.sh & or sh digclock &.
The time and date should now appear at the top right of your screen.
Output:
When you run digclock.sh, the terminal will return the job number and
process identifier (PID) of the digclock.sh process. From above output
you can find job number is "1" and PID is "15800".
You can end the execution of the script by two ways:
1. Using the kill command and specifying the job number or process ID.
If you don't remember job number or PID, you can get job number by
running jobs command and PID by ps command.
$ jobs
[1]+ Running ./digclock.sh &
$ shell]# ps | grep digclock
15800 pts/1 00:00:00 digclock.sh
To kill this job/process, either kill %1 or kill 15800 works.
$ kill %1
$
[1]+ Terminated ./digclock.sh
or
$ kill 15800
$
[1]+ Terminated ./digclock.sh
2. Using the fg command. The fg command switches a job running in the
background into the foreground, Then press Ctrl+c to terminate the
job. If no job number is specified, then fg command acts upon the
currently running job.
$ fg 1
./digclock.sh
Ctrl+c
$
With this script, you can display not only a clock, but other useful
information as well. For example,monitoring free space with df command
or CPU's load average with uptime command. Samba, Apache, and many
other servers have status commands where you can extract pieces of
information to show this way.
Tuesday, April 20, 2010
Posted by venu k
24 comments | 6:05 AM
Sunday, April 18, 2010
Posted by venu k
12 comments | 8:19 AM
Using ANSI escape sequences :
ANSI escape sequences or tput allow you to move the cursor around the
screen at will. This is more useful for full screen user interfaces
generated by shell scripts, but can also be used in prompts.
The movement escape sequences are as follows:
- Position the Cursor:
\033[<L>;<C>H
Or
\033[<L>;<C>f
puts the cursor at line L and column C.
- Move the cursor up N lines:
\033[<N>A
- Move the cursor down N lines:
\033[<N>B
- Move the cursor forward N columns:
\033[<N>C
- Move the cursor backward N columns:
\033[<N>D
- Clear the screen, move to (0,0):
\033[2J
- Erase to end of line:
\033[K
- Save cursor position:
\033[s
- Restore cursor position:
\033[u
The latter two codes are NOT honored by many terminal emulators. The
only ones that I'm aware of that do are xterm and nxterm - even though
the majority of terminal emulators are based on xterm code. As far as
I can tell, rxvt, kvt, xiterm, and Eterm do not support them. They are
supported on the console.
$ echo -en "\033[s\033[7B\033[1;34m BASH BASH\033[u\033[0m"
Above line saves the current cursor position(\033[s), then move the
cursor seven lines down the screen(\033[7B),print the word "BASH BASH"
in dark blue color(\033[1;34m), and then return to where it started to
produce a normal prompt(\033[u), and also back to the normal color
(\033[0m).
Example:
$ echo -en "\033[s\033[7B\033[1;34m BASH BASH\033[u\033[0m"
$
BASH BASH
Using tput command:
As with so many things in Unix, there is more than one way to achieve
the same ends.A utility called tput can also be used to move the cursor
around the screen, get back information about the status of the termi-
nal, or set colors.
man tput doesn't go into much detail about the available commands,but
man terminfo will give you a huge list of capabilities, many of which
are device independent, and therefore better than the escape sequences
previously mentioned.
Here is some useful tput capabilities:
tput Cursor Movement Capabilities:
tput cup Y X
Move cursor to screen location X,Y (top left is 0,0)
tput sc
Save the cursor position
tput rc
Restore the cursor position
tput lines
Output the number of lines of the terminal
tput cols
Output the number of columns of the terminal
tput cub N
Move N characters left
tput cuf N
Move N characters right
tput cuu N
up N lines
tput cud N
down N lines
tput Colour Capabilities :
tput setab [1-7]
Set a background colour using ANSI escape
tput setb [1-7]
Set a background colour
tput setaf [1-7]
Set a foreground colour using ANSI escape
tput setf [1-7]
Set a foreground colour
tput Text Mode Capabilities:
tput bold
Set bold mode
tput dim
turn on half-bright mode
tput smul
begin underline mode
tput rmul
exit underline mode
tput rev
Turn on reverse mode
tput smso
Enter standout mode (bold on rxvt)
tput rmso
Exit standout mode
tput sgr0
Turn off all attributes (doesn't work quite as expected).
tput Clear and Insert Capabilities :
tput ech N
Erase N characters
tput clear
clear screen and home cursor
tput el1
Clear to beginning of line
tput el
clear to end of line
tput ed
clear to end of screen
tput ich N
insert N characters (moves rest of line forward!)
tput il N
insert N lines
This is by no means a complete list of what terminfo and tput allow,
in fact it's only the beginning. man tput and man terminfo if you want
to know more.
Example:
$ tput sc;tput cup 4 35;tput setaf 2;echo "Hello World";\
> tput rc;tput sgr0
$
Hello World
Bellow example shows two different ways to achieve same task:
$ tput sc;tput cud 7;tput setaf 4;echo BASH BASH; tput rc;\
> tput sgr0
$ echo -en "\033[s\033[7B\033[0;34m BASH BASH\033[u\033[0m"
Subscribe to:
Posts (Atom)