LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices


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

Rep: Reputation: 50
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:
Code:
NOW=`date +%s`
TS=1102922460
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
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

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

Code:
#!/bin/bash
 
now=`date +%s`
ts=1102922460
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))

days=$tmp

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


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:06 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
Open Source Consulting | Domain Registration