How to do simple date (time) calculation in shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to do simple date (time) calculation in shell script?
# 1  
Old 09-16-2013
How to do simple date (time) calculation in shell script?

Hi,

I'm looking for a way to do a simple math calc during a shell script as a means of logging how long a particular task takes.

For example...
Code:
STARTTIME=whenever this script starts
./path/to/command.sh >>logfile.log
TOTALTIME=<time at this stage of the script after above command finished> - $STARTTIME
ECHO This command took $TOTALTIME to complete. >>logfile.log

I know that's a crude representation, but hopefully enough to communicate what I'm looking to accomplish.

Thank you!!
# 2  
Old 09-16-2013
Most shells have a SECONDS special variable you can use.

Code:
echo "Seconds since start of script:  $SECONDS"
sleep 10
echo "Seconds since start of script:  $SECONDS"

# 3  
Old 09-16-2013
Wow. That's super easy. Is there away to format that number to minutes? A simple divide by 60 is all I need...
# 4  
Old 09-16-2013
Depends on your shell, in BASH or KSH it's not hard:

Code:
printf "%02dH %02dM %02dS\n" $(( SECONDS/(60*60) )) $(( SECONDS/60 ))  $(( SECONDS%60 ))

# 5  
Old 09-16-2013
And yet I couldn't find that by doing web searches. I do try before I post. Thank you SO much!

I tried to make that a variable in order to insert it into a sentence, and it didn't work.

Code:
#!/bin/bash

sleep 10

SCRIPTTIME= printf "%02dH %02dM %02dS\n" $(( SECONDS/(60*60) )) $(( SECONDS/60 ))  $(( SECONDS%60 ))

echo "This script took $SCRIPTTIME to run."

The output was:
Code:
00H 00M 10S
This script took  to run.

# 6  
Old 09-16-2013
Variables don't work that way. You'd want to put it in `backticks` to put in in a variable.

Also, you don't want spaces after the equals sign. VARIABLE=something is good, VARIABLE=" something" is good, VARIABLE= something does something very different -- it blanks VARIABLE before attempting to run the program something...

But why not just jam it right into the printf statement, get it all done in one go?

Code:
printf "This script took %02dH %02dM %02dS to run\n" \
        $(( SECONDS/(60*60) )) $(( SECONDS/60 ))  $(( SECONDS%60 ))

The \ on the end of the line lets you continue it on the next line, instead of making one super-long line...
# 7  
Old 09-16-2013
Using your logic.

Code:
STARTTIME=`date +"%s"`
./path/to/command.sh >>logfile.log
TOTALTIME=$(expr `date +"%s"`  - $STARTTIME)
ECHO This command took $TOTALTIME to complete. >>logfile.log


Last edited by Aia; 09-16-2013 at 06:44 PM.. Reason: Syntax error.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date time difference in UNIX shell script

There are 2 dates, Tue Oct 1 13:40:19 2013 Sun Sept 30 10:26:23 2013 I have multiple dates like the above one. How do I calculate the date time difference and display in another column in Shell script. Please help. (3 Replies)
Discussion started by: tanmoysays
3 Replies

2. Shell Programming and Scripting

Date / Time difference in shell script

Request ID GMDCOMTM GMDRRSTIME GMDRESTIME <36812986> : : :I want to display the date -time difference in other fields. Above I have given for only 1 record. I want to calculate for all the records. (GMCOMTM - GMDRRSTM) ,(GMDRRSTM-GMDRESTM) and... (5 Replies)
Discussion started by: ghosh_tanmoy
5 Replies

3. Shell Programming and Scripting

Date / Time difference in shell script

================================================================================ Request ID GMDCOM TIME GMDRRS TIME COM-RRS ================================================================================ <36812974> Tue Oct 1 13:32:40 2013 Tue Oct 1 20:36:42 2013... (1 Reply)
Discussion started by: ghosh_tanmoy
1 Replies

4. Shell Programming and Scripting

Date and Time comparison using shell script

Hi, I'm having two fields in the file F1|F2 20111220|102000 F1 ->YYYYMMDD F2 ->HHMMSS Now, I need to compare this with current date & time and need to return the difference value in hours. Already, I checked with datecalc from the forum. So, need hints from Shell Gurus. Thanks (10 Replies)
Discussion started by: buzzusa
10 Replies

5. Shell Programming and Scripting

time calculation in ksh script

I"m trying to calculate the duration of of backup within a ksh shell script but I get an error. #!/bin/ksh STTIM=`date '+%T'` EDTIM=`date '+%T'` .... .... echo "DURATION OF BACKUP: $((EDTIM - STTIM))" (5 Replies)
Discussion started by: Bperl1967
5 Replies

6. Shell Programming and Scripting

Date calculation script

hello! I need a date calculation script that need to do that: ./date.sh 20090312 and the script need to give me which day is it for example monday friday or what else! can anyone help me?? its really urgent :S thx the help! (7 Replies)
Discussion started by: impish
7 Replies

7. Shell Programming and Scripting

Date time calculation

hello guys, I had been to many forums and many topics in this site as well for my question but did not get any solution. My question is how i can get y'day date with time stamp today is 20100729103819 and i am looking for output as 20100728103819. in simple words as we do in oracle sysdate-1... (4 Replies)
Discussion started by: lokaish23
4 Replies

8. Shell Programming and Scripting

Is it possible to get the date and time of mail which we get into our inbox using shell script?

Hello All, I need a script which extarct the date and time of the mail which is there in our inbox... I can export the mail copy into desktop making it as a textfile or something like that.. So is there anyway to get the date and time from that? (3 Replies)
Discussion started by: smarty86
3 Replies

9. Shell Programming and Scripting

Execute a part of shell script only after particular date and time

I have created a simple shell script... say test.sh Contents of test.sh ================ service named restart cp /etc/imp.conf /backup/test/ #-- if date > 15 July 2007 11:23 pm , then only issue the commans below, else exit --- cp /etc/secondimp.conf /backup/test/ rm -f... (2 Replies)
Discussion started by: fed.linuxgossip
2 Replies

10. Shell Programming and Scripting

script execution time calculation

I am writting a script in the ksh shell and am trying to find a way to report the total execution time of the script without requiring the user to specify the time function when executing the script. Does anyone have any examples they have used. I have been setting up two date variables (one at... (9 Replies)
Discussion started by: johnsonbryce
9 Replies
Login or Register to Ask a Question