Sponsored Content
Full Discussion: Countdown timer with seconds
Operating Systems Linux Ubuntu Countdown timer with seconds Post 303033489 by drew77 on Saturday 6th of April 2019 11:08:41 AM
Old 04-06-2019
Countdown timer with seconds

I would like this to work with seconds as well.

Code:
#!/bin/bash
# 
 
if [ "$#" -lt "2" ] ; then 
    echo "Incorrect usage ! Example:" 
    echo './CountDown.sh -d  "Jun 10 2011 16:06"' 
    echo 'or' 
    echo './CountDown.sh -m  90' 
    exit 1 
fi 
 
now=`date +%s` 
 
if [ "$1" = "-d" ] ; then 
    until=`date -d "$2" +%s` 
    sec_rem=`expr $until - $now` 
    echo "-d" 
    if [ $sec_rem -lt 1 ]; then 
        echo "$2 is already history !" 
    fi 
fi 
 
if [ "$1" = "-m" ] ; then 
    until=`expr 60 \* $2` 
    until=`expr $until + $now` 
    sec_rem=`expr $until - $now` 
    echo "-m" 
    if [ $sec_rem -lt 1 ]; then 
        echo "$2 is already history !" 
    fi 
fi 
 
_R=0
_C=7
tmp=0
percent=0
total_time=0
col=`tput cols`
col=$[ $col -5 ]

while [ $sec_rem -gt 0 ]; do 
    clear 
    date 
    let sec_rem=$sec_rem-1 
    interval=$sec_rem 
    seconds=`expr $interval % 60` 
    interval=`expr $interval - $seconds` 
    minutes=`expr $interval % 3600 / 60` 
    interval=`expr $interval - $minutes` 
    hours=`expr $interval % 86400 / 3600` 
    interval=`expr $interval - $hours` 
    days=`expr $interval % 604800 / 86400` 
    interval=`expr $interval - $hours` 
    weeks=`expr $interval / 604800` 
    echo "----------------------------" 
    echo "Seconds: " $seconds 
    echo "Minutes: " $minutes 
    echo "Hours:   " $hours 
    echo "Days:    " $days 
    echo "Weeks:   " $weeks 

    echo -n "["

    progress=$[$progress + 1]
    if [ $total_time -lt 1 ] ; then
        total_time=$[$hours * 3600 + $minutes * 60 + $seconds]
    fi
    
    printf -v f "%$(echo $_R)s>" ; printf "%s\n" "${f// /=}"
    _C=7
    tput cup 7 $col

    tmp=$percent
    percent=$[$progress * 100 / $total_time]
    printf "]%d%%" $percent
    change=$[$percent - $tmp]

    _R=$[ $col * $percent / 100 ]

    sleep 1
done
printf "\n"

 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

shell script - loop to countdown

I am taking a class in UNIX and have written a script that needs to countdown from a number that is read in from the keyboard to zero. If no number is given the start of the countdown should default to 10. I can't get this to do the default #! /bin/sh echo Enter a number here to countdown... (2 Replies)
Discussion started by: froggwife
2 Replies

2. Shell Programming and Scripting

Display runnning countdown in a bash script?

I am looking for a way to display on a single line, a running countdown for a given amount of time in a terminal using a bash script. I am looking for this to use as part of a larger bash script that captures Video. The script sets up a bunch of parameters for DVgrab, and one of the parameters... (11 Replies)
Discussion started by: Starcast
11 Replies

3. UNIX for Dummies Questions & Answers

countdown in unix?

is there a script to preform a countdown from 10 seconds to 0? (5 Replies)
Discussion started by: JamieMurry
5 Replies

4. Shell Programming and Scripting

countdown to christmas

was wondering if there was a script that would countdiwn the days until christmas (3 Replies)
Discussion started by: trob
3 Replies

5. Shell Programming and Scripting

Stop! (the countdown!) :-) shell script help

Hi guys, I've found two nifty little scripts on these forums one which detects if the F5 key has been pressed: #/bin/sh _key() { local kp ESC=$'\e' _KEY= read -d '' -sn1 _KEY case $_KEY in "$ESC") while read -d '' -sn1 -t1 kp do _KEY=$_KEY$kp ... (0 Replies)
Discussion started by: rich@ardz
0 Replies

6. Shell Programming and Scripting

Anyone know of any FUN countdown script

Hi all, Does anyone know of any FUN countdown script that I can use for my script? At the moment, am just using sleep 10 or more and then print stuff into the screen to allow more time for the user to decide whether they want to continue running the script or abort? Just thought of wanting... (3 Replies)
Discussion started by: newbie_01
3 Replies

7. Shell Programming and Scripting

Bash - countdown timer

Hello, I have another problem with my script - I would like to have a countdown timer visible on the screen, and at the same time, I want te be able to do something else. And when the time runs out, I need to know about that inside the script somehow and do some action. I guess that would require 2... (3 Replies)
Discussion started by: xqwzts
3 Replies

8. Shell Programming and Scripting

Perl script countdown

In the below bash when the perl is it possible to hide the commands from running on screen and display a process countdown? For example, on the cygwin screen now the user sees each process in the command running as running protocol refGene, running protocol popfreq_all, etc... Could a... (0 Replies)
Discussion started by: cmccabe
0 Replies

9. UNIX for Beginners Questions & Answers

Banner Countdown Timer

Hello. I am pretty new to unix and shell scripting and I was wondering if there might be a way to banner a countdown timer inside a script. We currently have an existing script that does a 2 minute sleep but thought it might be fun to actually make it banner a countdown timer until it is finished.... (3 Replies)
Discussion started by: thumbelina
3 Replies
IO::Async::Timer::Countdown(3pm)			User Contributed Perl Documentation			  IO::Async::Timer::Countdown(3pm)

NAME
"IO::Async::Timer::Countdown" - event callback after a fixed delay SYNOPSIS
use IO::Async::Timer::Countdown; use IO::Async::Loop; my $loop = IO::Async::Loop->new; my $timer = IO::Async::Timer::Countdown->new( delay => 10, on_expire => sub { print "Sorry, your time's up "; $loop->stop; }, ); $timer->start; $loop->add( $timer ); $loop->run; DESCRIPTION
This subclass of IO::Async::Timer implements one-shot fixed delays. The object implements a countdown timer, which invokes its callback after the given period from when it was started. After it has expired the Timer may be started again, when it will wait the same period then invoke the callback again. A timer that is currently running may be stopped or reset. For a "Timer" object that repeatedly runs a callback at regular intervals, see instead IO::Async::Timer::Periodic. For a "Timer" that invokes its callback at a fixed time in the future, see IO::Async::Timer::Absolute. EVENTS
The following events are invoked, either using subclass methods or CODE references in parameters: on_expire Invoked when the timer expires. PARAMETERS
The following named parameters may be passed to "new" or "configure": on_expire => CODE CODE reference for the "on_expire" event. delay => NUM The delay in seconds after starting the timer until it expires. Cannot be changed if the timer is running. A timer with a zero delay expires "immediately". remove_on_expire => BOOL Optional. If true, remove this timer object from its parent notifier or containing loop when it expires. Defaults to false. Once constructed, the timer object will need to be added to the "Loop" before it will work. It will also need to be started by the "start" method. METHODS
$expired = $timer->is_expired Returns true if the Timer has already expired. $timer->reset If the timer is running, restart the countdown period from now. If the timer is not running, this method has no effect. EXAMPLES
Watchdog Timer Because the "reset" method restarts a running countdown timer back to its full period, it can be used to implement a watchdog timer. This is a timer which will not expire provided the method is called at least as often as it is configured. If the method fails to be called, the timer will eventually expire and run its callback. For example, to expire an accepted connection after 30 seconds of inactivity: ... on_accept => sub { my ( $newclient ) = @_; my $watchdog = IO::Async::Timer::Countdown->new( delay => 30, on_expire => sub { my $self = shift; my $stream = $self->parent; $stream->close; }, ); my $stream = IO::Async::Stream->new( handle => $newclient, on_read => sub { my ( $self, $buffref, $eof ) = @_; $watchdog->reset; ... }, on_closed => sub { $watchdog->stop; }, ) ); $stream->add_child( $watchdog ); $watchdog->start; $loop->add( $watchdog ); } Rather than setting up a lexical variable to store the Stream so that the Timer's "on_expire" closure can call "close" on it, the parent/child relationship between the two Notifier objects is used. At the time the Timer "on_expire" closure is invoked, it will have been added as a child notifier of the Stream; this means the Timer's "parent" method will return the Stream Notifier. This enables it to call "close" without needing to capture a lexical variable, which would create a cyclic reference. Fixed-Delay Repeating Timer The "on_expire" event fires a fixed delay after the "start" method has begun the countdown. The "start" method can be invoked again at some point during the "on_expire" handling code, to create a timer that invokes its code regularly a fixed delay after the previous invocation has finished. This creates an arrangement similar to an IO::Async::Timer::Periodic, except that it will wait until the previous invocation has indicated it is finished, before starting the countdown for the next call. my $timer = IO::Async::Timer::Countdown->new( delay => 60, on_expire => sub { my $self = shift; start_some_operation( on_complete => sub { $self->start }, ); }, ); $timer->start; $loop->add( $timer ); This example invokes the "start_some_operation" function 60 seconds after the previous iteration has indicated it has finished. AUTHOR
Paul Evans <leonerd@leonerd.org.uk> perl v5.14.2 2012-10-24 IO::Async::Timer::Countdown(3pm)
All times are GMT -4. The time now is 02:17 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy