PDA

View Full Version : Bash scripting, repeating information on same line?


darksizzle
October 27th, 2007, 03:59 AM
Hi All

I am making a shell script that I would like to have the time displayed. I have a function that checks the time every second and what I would like to do is have the time keep updating but instead of having it create a new line each time I would like it to keep updating on the same line. It would be a huge help if someone can show me how to do this or point me in the right direction!

ghostdog74
October 27th, 2007, 04:26 AM
just an idea

while [ 1=1 ];
do
clear
echo `date +%S`
sleep 1
done

weresheep
October 27th, 2007, 04:41 AM
Hi,

Here is something that should do the job.


#!/bin/sh -

while [ 1 -eq 1 ] ; do
printf "$(date +%T)"
sleep 1
printf "\b\b\b\b\b\b\b\b"
done


This code uses printf to display the current time, sleeps for a second then uses printf to print a series of backspaces. Its crude but works.

However, this only works if nothing else prints to the screen whilst it is running. If you want to be able to update one part of the screen independently of another you'll need to look into using ncurses etc.

Hope that helps.

-weresheep

cwaldbieser
October 27th, 2007, 12:36 PM
Hi,

Here is something that should do the job.


#!/bin/sh -

while [ 1 -eq 1 ] ; do
printf "$(date +%T)"
sleep 1
printf "\b\b\b\b\b\b\b\b"
done


This code uses printf to display the current time, sleeps for a second then uses printf to print a series of backspaces. Its crude but works.


You could print a "\r" instead of the backspaces.

Nuup
April 14th, 2010, 10:51 AM
Hi,

while I was looking for something like this hint with printf, I happend to find this thread. The last hint was quite useful for me, So I registered, to drop a line, too.
Despite the fact, that this thread is almost 3 years old.

I hope this alias is a good solution, too:

alias rtc='while [ 1 ]; do printf "\r$(date +%T)"; sleep 1s; done'

Kind regards,

Nuup