How to implement stopwatch in bash?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to implement stopwatch in bash?
# 1  
Old 09-12-2007
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
# 2  
Old 09-12-2007
look into 'man time'
# 3  
Old 11-27-2007
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Implement ps command in C

Hello, could anybody explain how the ps command works? I know something about the proc file system. But I'm still not sure about how it exactly works. Like ps without any option will print out the current user's processes, but it never displays my web browsers such as firefox or my LibreOffice... (3 Replies)
Discussion started by: freedombird9
3 Replies

2. Shell Programming and Scripting

Making a stopwatch function

#The first part of this works fine a period prints every 15 seconds (ping –c 3, takes 2 seconds). However no “Minutes since start = “ message prints. #By all rights it should print 2 times once when it is outputted from “grep” and a second time when cat reads it. I would use cron but I am not... (4 Replies)
Discussion started by: ifthanwhile
4 Replies

3. Shell Programming and Scripting

How to implement this?

hi i have a file like 1,"A","B" 2,"C","D" 1,"E","F" 3,"G","H" in output i need like 3,"G","H" 1,"E","F" 2,"C","D" 1,"A","B" (12 Replies)
Discussion started by: angel12345
12 Replies

4. Solaris

new - How i can implement Mirroring

Unix is something new for me, I trying learn about it now. I have download the sun virtualbox software and install solaris 10 into it. It is is like wizard with step by step installation guideline. :) NOw how can i implement mirroring(RAID) into the my new SunOS? can anyone give a briefly list... (14 Replies)
Discussion started by: webster5u
14 Replies

5. Shell Programming and Scripting

Trying to implement 'more' command

Hi all, i'm quite new in the UNIX world. So maybe i'm going to ask simple questions but they are unsolvable for me... I'm trying to implement the 'more' function or kinda of that, for improving my knowledges. But I encountered some problems that i hope u will help me to solve. The code i... (0 Replies)
Discussion started by: Cellofan
0 Replies

6. Shell Programming and Scripting

how to implement this

Hi all, could any of you please help me on my problem.. we are doing FTP (one report out put) from one server to another server through unix shell script program. Due to the network issues, some times FTP process is hanging. So we planned to modify the existing program with the following... (2 Replies)
Discussion started by: kishore_jasthi
2 Replies

7. AIX

how to implement timer

anyone can help me how to implement the timer on AIX? I tried with 'setitimer' and its related functions, but it does not work correctly,the program exited each time. thanks (2 Replies)
Discussion started by: Frank2004
2 Replies

8. Shell Programming and Scripting

PERL: Stopwatch + record

Is it possible to make a perl or cgi script that does the following ? 1) have a stopwatch with a start / start / pause feature and record the time its paused on. but be able to record up to ten different times 2)print the recorded time to .txt file (1 Reply)
Discussion started by: perleo
1 Replies

9. Programming

how does va_arg implement ?

1 . How does va_arg implemented by system? (2 Replies)
Discussion started by: chenhao_no1
2 Replies

10. UNIX for Advanced & Expert Users

how can i implement rlogin

how can i use a rlogin with out entered a password, someone tell me about configure the next files /.rhosts /etc/hosts.equiv and /etc/hosts but i not sure about that, or there are not enough could you tell me how to do that? (3 Replies)
Discussion started by: jav_v
3 Replies
Login or Register to Ask a Question