LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Calculate duration in seconds in bash (https://www.linuxquestions.org/questions/linux-newbie-8/calculate-duration-in-seconds-in-bash-844392/)

xeon123 11-15-2010 11:34 AM

Calculate duration in seconds in bash
 
Hi,

I would like to subtract the end time with a start time in bash. How can I do it?

Here's a script of example of what I want:
startdate = GETTIME
ls -la
enddate = GETTIME
echo "DURATION: $enddate-$startdate seconds."

GrapefruiTgirl 11-15-2010 11:37 AM

Code:

echo "DURATION: $((enddate-startdate)) seconds."
That should help out!

xeon123 11-15-2010 11:40 AM

But how do I get the time in bash?

GrapefruiTgirl 11-15-2010 11:41 AM

Oh! :)

Probably you want to use the `date` command. For example, to print the UNIX time, do:
Code:

sasha@reactor: date +%s
1289842351
sasha@reactor:

So let's say you want the current UNIX time in a variable:
Code:

VARIABLE=$(date +%s)

GrapefruiTgirl 11-15-2010 11:44 AM

Oh hey, P.S.: I notice you are saying "OS: Free BSD" in your signature or under your post, but you are posting from an Ubuntu-type of OS. You may want to specify which this question applies to (and specify in any thread you create), and or change your signature accordingly so as to lessen the chance of confusion.

Cheers.

bigrigdriver 11-15-2010 11:49 AM

Open your favorite browser, go to www.google.com/linux, and search for "measuring run times". You will find a wealth of information to read on the subject, probably with sample code in many of them.

xeon123 11-15-2010 11:51 AM

I made this script, but it's is not working, why? (I'm newbie in bash)

#!bin/bash

START=date +%s
sleep 2
END=date +%s

echo "DURATION: $(END-START)"

w1k0 11-15-2010 12:01 PM

A lot of proofreading needed:

Code:

#!/bin/bash

START=`date +%s`
sleep 2
END=`date +%s`

echo "DURATION: $(($END-$START))"

Follow carefully all the changes.

xeon123 11-15-2010 12:03 PM

It worked. Thanks.


All times are GMT -5. The time now is 03:03 AM.