LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-15-2010, 12:34 PM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Rep: Reputation: 16
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."

Last edited by xeon123; 11-15-2010 at 12:37 PM.
 
Old 11-15-2010, 12:37 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Code:
echo "DURATION: $((enddate-startdate)) seconds."
That should help out!
 
Old 11-15-2010, 12:40 PM   #3
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
But how do I get the time in bash?
 
Old 11-15-2010, 12:41 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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)
 
Old 11-15-2010, 12:44 PM   #5
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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.
 
Old 11-15-2010, 12:49 PM   #6
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Jessie 8.4
Posts: 5,873

Rep: Reputation: 348Reputation: 348Reputation: 348Reputation: 348
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.
 
Old 11-15-2010, 12:51 PM   #7
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
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)"
 
Old 11-15-2010, 01:01 PM   #8
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware, Mint
Posts: 1,252

Rep: Reputation: 205Reputation: 205Reputation: 205
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.
 
Old 11-15-2010, 01:03 PM   #9
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
It worked. Thanks.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculate Local Time in seconds omriar Linux - General 3 09-03-2010 10:54 PM
bash calculate repeated lines nushki Programming 5 01-28-2010 05:27 AM
[SOLVED] Getting duration of execution in bash Himilhil Linux - Software 4 11-04-2009 03:21 AM
bash calculate job duration Padawan.AVT Programming 2 10-07-2008 03:33 AM
Bash Script, calculate output. eldaria Programming 13 07-20-2006 10:26 PM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration