Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 12-16-2004, 09:43 AM   #1
Senior Member
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 49
Calculate in shell script

I have a shell script with a variable that is set to "date +%s" and I have the timestamp of a file.

Now I want the script to calculate now-timestamp and then use the modulus (%) to calc the time elapsed. Like this:
NOW=`date +%s`
tmp = now-ts

s = tmp%60
tmp -= s
tmp /= 60

m = tmp%60
tmp -= m
tmp /= 60

h = tmp%24
tmp -= h
tmp /= 24

days = tmp

echo elapsed $days days, $h:$m:$s
How can I calc this in a shell script?
Old 12-16-2004, 11:11 AM   #2
Senior Member
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
I hope this isn't too far off the wall as I haven't tested the calcs.

now=`date +%s`
tmp=$(($now - $ts))

s=$(($tmp %60))
tmp=$(($tmp - $s))
tmp=$(($tmp / 60))

m=$(($tmp %60))
tmp=$(($tmp - $m))
tmp=$(($tmp / 60))

h=$(($tmp %24))
tmp=$(($tmp - $h))
tmp=$(($tmp / 24))


echo elapsed $days days, $h:$m:$s


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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script inside shell script treotan Linux - General 4 02-19-2009 06:34 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
shell script to calculate the time a user is logged in racer_mec Linux - Newbie 2 02-08-2005 11:40 AM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 03:00 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:48 PM.

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