RSS Subscription
Linux Howtos & Tutorials

Enter your email:

Delivered by


NOTE:New tutorials are from LinuxCareer.com

Poll

Do you own or wish to have iPhone?
 


Linux eBooks FREE Download
A guide to programming Linux kernel modules
Introduction to Linux - A Hands on Guide
A Newbie's Getting Started Guide to Linux

Linux from Scratch - Create Your Own Linux System - Free eBook

Linux: The Hacking Solution (v.3.0)

SQLite 3 with PHP Essential Training – Free Video Training Tutorials

This guide will introduce you to the world of GNU/Linux

The GNU/Linux Advanced Administration

A Complete Beginner's Manual for Ubuntu 10.04 (Lucid Lynx)

Advanced Bash-Scripting Guide

Set up, maintain, and secure a small office email server

Partner Linux Sites:
How-To.LinuxCareer.com
Jobs.LinuxCareer.com
TuxMachines
Monsterb
LinuxBloggers
AdamsInfo
LinuxScrew
All For Linux

Time countdown bash script example

This is a simple skeleton of a bash countdown script. The script takes two arguments . Here are some examples of its usage:

  • countdown time to 90 minutes from now:
./bash-countdown.sh -m 90
  • countdown time to 23.3.2036 from now:
./bash-countdown.sh -d "Mar 23 2036"
  • countdown time to 21:06 from now:
./bash-countdown.sh -d 21:06
  • countdown time to 21:06:45 from now:
./bash-countdown.sh -d 21:06:45

Feel free to modify this script according to your needs:

bash-countdown.sh :

#!/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 
 
 
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 
	sleep 1 
done 

Do not forget to make bash-countdown.sh script executable before execution:

$ chmod +x bash-countdown.sh

Exectute:

./bash-countdown.sh -d 22:34

Output:

Fri Jun 11 20:55:19 EST 2010
----------------------------
Seconds:  40
Minutes:  38
Hours:    1
Days:     0
Weeks:    0

Share this linux post:

Submit Time countdown bash script example in Delicious Submit Time countdown bash script example in Digg Submit Time countdown bash script example in FaceBook Submit Time countdown bash script example in Google Bookmarks Submit Time countdown bash script example in Stumbleupon Submit Time countdown bash script example in Technorati Submit Time countdown bash script example in Twitter
 
Comments for this page are closed !!!
Please visit our new Linux Forum for additional help or discussion.


Linux eBooks FREE Download