Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

How to implement stopwatch in bash?

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 09-12-2007
siegfried siegfried is offline
Registered User
 
Join Date: Oct 2005
Last Activity: 18 April 2016, 10:56 PM EDT
Location: Woodinville, WA
Posts: 138
Thanks: 0
Thanked 0 Times in 0 Posts
How to implement stopwatch in bash?

I want to time how long it takes to run some programmes and report the delta time in days, hours, minutes and seconds using a simple echo. Does someone have a simple bash script to do this?

Thanks,
siegfried
Sponsored Links
    #2  
Old Unix and Linux 09-12-2007
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 23 February 2017, 5:57 AM EST
Location: Foxborough, MA
Posts: 8,046
Thanks: 205
Thanked 733 Times in 684 Posts
look into 'man time'
Sponsored Links
    #3  
Old Unix and Linux 11-27-2007
jonwiz jonwiz is offline
Registered User
 
Join Date: Nov 2007
Last Activity: 27 November 2007, 2:07 PM EST
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
bash stopwatch

I found a simple stopwatch script, and then modified it to display the time on one line. (before it was echoing the time every loop in the terminal - a waste of space)

Enjoy!


Code:
#!/bin/bash

BEGIN=$(date +%s)
BACK="\b\b\b\b"


echo Starting Stopwatch...

while true; do
        NOW=$(date +%s)
        let DIFF=$(($NOW - $BEGIN))
        let MINS=$(($DIFF / 60))
        let SECS=$(($DIFF % 60))

#only echo count if its different than the last time
if [ "$DIFF" != "$OLDDIFF" ]
then
        #backspace 4 times to reset stopwatch position
        #The '-e' enables \b to be interpreted correctly
        #The '-n' avoids the newline character at the end
        echo -ne $BACK
        echo -ne $MINS:`printf %02d $SECS`
fi

#define olddiff to current diff
let OLDDIFF=DIFF

done

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Making a stopwatch function ifthanwhile Shell Programming and Scripting 4 06-17-2011 12:31 PM
How to implement scenario? angel12345 Shell Programming and Scripting 3 10-20-2010 09:04 PM
How to implement this? angel12345 Shell Programming and Scripting 12 10-20-2010 01:30 PM
how to implement this kishore_jasthi Shell Programming and Scripting 2 02-22-2008 05:11 AM
PERL: Stopwatch + record perleo Shell Programming and Scripting 1 05-16-2003 12:05 AM



All times are GMT -4. The time now is 07:48 AM.