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
Subscribe to:
Post Comments (Atom)
Nice script.
ReplyDeleteThank you.
Super, owersome, you save me time
ReplyDeletehey thanks it was a great help.............zoya
ReplyDeleteWonder full post and this article tell us how reset your time and date thanks for share it personal statement editor .
ReplyDeleteFinancial research outsourcing
ReplyDeleteTopAssociate is a specialist provider of outsourcing and offshoring solutions for the global financial and investment community. We provide a very comprehensive range of high quality analytics - TopAssociate,Investment research outsourcing,hedge fund research outsourcing,M&A research outsourcing,Private equity research outsourcing,Equity research outsourcing,Financial analyst outsourcing,Customised financial research,Fixed income research outsourcing,Financial research outsourcing
Thanks for sharing this information with us.
ReplyDeleteIT Job Training in Chennai
Useful Script.Thanks for sharing.
ReplyDeleteRegards,
Unix Training in Chennai
Amazing article..Thanks for sharing..
ReplyDeleteMCSE Certification Training in Chennai | MCSE Training in Chennai | MCSE Certification Classes in Chennai | Online Training in Chennai
Wonderful post. keep sharing...Software Testing Training Institute in Chennai | Software Testing Training Institute in Velachery.
ReplyDeletehttp://www.bashguru.com/2010/04/shell-script-to-display-time-and-date.html?showComment=1489737809142#c868844832021691350
ReplyDeleteNeeded to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
ReplyDeleteJava Training In Bangalore
loved your blog lot. Almost certainly I am going to bookmark your blog. Its so interesting, Thanks a lot for sharing your blog. RPA Training Institute in Chennai | UI Path Training Institute in Chennai | Blue Prism Training Institute in Chennai
ReplyDeleteWonderful Blog..Thanks for sharing this informative post..No.1 Software Testing Training Institute in Chennai | Best Selenium Training Institute in Chennai | ISTQB Certification Center in Velachery
ReplyDeleteI am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly. Web Designing Training Institute in Chennai | DotNet Training Institute in Chennai | Software Testing Training Institute in Chennai
ReplyDeleteThe website is looking bit flashy and it catches the visitors eyes. A design is pretty simple and a good user-friendly interface.
ReplyDeleteaws training in chennai
thanks for sharing wonderful article..Always keep sharing..Summer Courses in Adyar | Summer Courses in OMR | Summer Courses in Velachery
ReplyDeleteit’s really nice and meaningful. it’s really cool blog. Linking is very useful thing.you have really helped lots of people who visit blog and provide them useful information.
ReplyDeleteSummer Courses for Business Administration in Chennai | Best Summer Courses in Porur
Wonderful post..keep sharing...Summer Courses in Chrompet | Summer Courses in Chennai
ReplyDeleteImpressive blog with lovely information. Really very useful article for us thanks for sharing such a wonderful blog...
ReplyDeleteBest IT Training Institute in Chennai |Best IT Training Institute in Velachery
Very informative post! There is a lot of information here that can help any business get started with a successful social networking campaign!!!!
ReplyDeleteGood Vecation classes in Chennai | Best Summer Course in Guindy
Keep sharing such type of useful information
ReplyDeleteBest Software Training Institute in Chennai
| Best Software Training Institute in Velachery
Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site...
ReplyDeleteEmbedded System training in Chennai | Embedded system training institute in chennai | PLC Training institute in chennai | IEEE final year projects in chennai | VLSI training institute in chennai
This comment has been removed by the author.
ReplyDeleteAs a rule they will take the auto to a sale, offer it, and recover their expenses.This will happen rapidly as they wouldn't have any desire to cling to the auto. There is no turning back when this happens.auto title loans chicago
Delete