Results 1 to 7 of 7

Thread: Bash scripting, repeating information on same line?

  1. #1
    Join Date
    Aug 2007
    Location
    Santa Rosa, CA
    Beans
    15
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Bash scripting, repeating information on same line?

    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!

  2. #2
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Bash scripting, repeating information on same line?

    just an idea
    Code:
    while [ 1=1 ];
    do
      clear
     echo `date +%S`
     sleep 1
    done

  3. #3
    Join Date
    Mar 2005
    Location
    Alba
    Beans
    73
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Bash scripting, repeating information on same line?

    Hi,

    Here is something that should do the job.

    Code:
    #!/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

  4. #4
    Join Date
    Oct 2004
    Location
    Pennsylvania
    Beans
    1,698

    Re: Bash scripting, repeating information on same line?

    Quote Originally Posted by weresheep View Post
    Hi,

    Here is something that should do the job.

    Code:
    #!/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.

  5. #5
    Join Date
    Apr 2010
    Beans
    1

    Re: Bash scripting, repeating information on same line?

    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:

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

    Nuup

  6. #6
    Join Date
    Mar 2013
    Beans
    1

    Re: Bash scripting, repeating information on same line?

    Your response was 3 years old and I registered 3 years later just to say this was EXACTLY what I was looking for!!! So 6 years after the original post, this is still helpful. We should all reconsider whenever we hesitate to comment on a forum that we think is "too old" to resurrect.

  7. #7
    Join Date
    Jan 2010
    Location
    Kolkata India
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Bash scripting, repeating information on same line?

    Closed old thread.
    “Progress is made by lazy men looking for easier ways to do things”
    — Robert A. Heinlein

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •