Visit Our UNIX and Linux User Community

Please Help ( Calculate time taken in a script)

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Please Help ( Calculate time taken in a script)
# 1  
Old 10-01-2001
Question Please Help ( Calculate time taken in a script)

I want to know how to calculate time difference in unix shell script. Time command in unix will not solve my problem because it calculates total time taken by a script.

See the examle below.

example :-
# This is just example
# Start the timer
Shell commands ..
Shell commands .

# End of timer

# I want to know how much time taken between start the timer and end of timer.

Is there unix built in commands to do it easily.

Unix Guru's please suggest.
# 2  
Old 10-01-2001

i think u can use the timex command. for more details just go thru the "man".Smilie
# 3  
Old 10-01-2001
Hello sskb,
Thanks for your response. I have to write the time or timex command for every shell command. What I want to do is take the difference of time between some line of commands in shell script.

See the example below.

example :-
# This is just example
# Start the timer
Shell commands ..
Shell commands .

# End of timer

Did you get what I am trying to say.

# 4  
Old 10-01-2001
Yes sanjay! i got ur point. but as far as i know there is no such built in command. but if you don't mind .. i would tell u what i would do in that case.

just get the date and time store it!
execute the stream of commands
then again get the date and time.

get the difference from that.

I hope u have better idea than this. if so, please let me know.

# 5  
Old 10-01-2001
Hello sskb,

Is there any straight forward method in Unix to subtract two time/date values. I know I can store date/time and in some temp variables but I have to write a programme to subtract these two values. I was looking for some Unix build in method for this. I know in unix there is something called difftime but I do not know how to use it in shell scripts.

# 6  
Old 10-02-2001
Can you tell me where the timex command is located. It is not in my path list. I can see man timex. I can see time command but unable to find out timex command in my system.
One question about time command.
# This command does not work in ksh but works in sh or bash.

$time -p echo sanjay

Why -p option of time command does not work in ksh.

# 7  
Old 10-02-2001
I have timex in /usr/bin. ksh has a built-in "time". Specify /usr/bin/time to get the version you see on the man page.

ksh has a variable called SECONDS that is the seconds since midnight. You can save the value, do some stuff, then save the value again. Then just subtract the two values. It's very easy if it doesn't need to work across midnight...and even that isn't a terribly hard problem.

You can also do stuff like:
time ksh -c "command1 ; command2"
although you will want to subtract the overhead for ksh itself which you can get with something like:
time ksh -c ":"

This last idea is not very far from simply putting the two commands into a separate script and then running time on that script.

Previous Thread | Next Thread
Test Your Knowledge in Computers #377
Difficulty: Medium
macOS is UNIX V7 compliant.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculate Time diff in milli milliseconds(Time format : HH:MM:SS,NNN)

Hi All, I have one file which contains time for request and response. I want to calculate time difference in milliseconds for each line. This file can contain 10K lines. Sample file with 4 lines. for first line. Request Time: 15:23:45,255 Response Time: 15:23:45,258 Time diff... (6 Replies)
Discussion started by: Raza Ali
6 Replies

2. Shell Programming and Scripting

Calculate time difference

I have time in a file in HH:MM:SS format as it contents(its not the file creation time). i need this to be converted to epoch time or time since 1970. The time is written into that file by a script, which i cannot modify. Im using AIX machine $ cat abc.txt 10:29:34 (2 Replies)
Discussion started by: gpk_newbie
2 Replies

3. Shell Programming and Scripting

How to calculate time

Hello Guys, I am trying to calculate total hours and minutes a given user has used the system since the beginning of the current month. #!/usr/bin/sh hr=0 min=0 last $1 | grep -w `date "+%b"` | grep -v '\(0:.*\)' | grep -vw sshd | cut -c 66- | tr -d "\(\)" | cut -f1 -d ":" | grep -v '.*' |... (9 Replies)
Discussion started by: kasparov
9 Replies

4. Shell Programming and Scripting

Calculate age of a file | calculate time difference

Hello, I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes... I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes. To do... (10 Replies)
Discussion started by: worm
10 Replies

5. Shell Programming and Scripting

How to calculate time difference between start and end time of a process!

Hello All, I have a problem calculating the time difference between start and end timings...! the timings are given by 24hr format.. Start Date : 08/05/10 12:55 End Date : 08/09/10 06:50 above values are in mm/dd/yy hh:mm format. Now the thing is, 7th(08/07/10) and... (16 Replies)
Discussion started by: smarty86
16 Replies

6. Shell Programming and Scripting

Calculate average time using a script

Hello, I'm hoping to get some help on calculating an average time from a list of times (hour:minute:second). Here's what my list looks like right now, it will grow (I can get the full date or change the formatting of this as well): 07:55:31 09:42:00 08:09:02 09:15:23 09:27:45 09:49:26... (4 Replies)
Discussion started by: jaredhanks
4 Replies

7. Programming

Calculate time to some date

Hello! I need to find how many days, hours and minutes remain to some specific date and I wonder why the following program shows incorrect values, such as 4 days 23 hours etc to 14:00 this Saturday from 17:33 today... #include <stdio.h> #include <time.h> int main() { time_t elaps,... (4 Replies)
Discussion started by: Sapfeer
4 Replies

8. UNIX for Dummies Questions & Answers

calculate the time

hello i want to display the time firstly when i run my shell script and after 25 min i want to display a message it says that the time left is 5 min. When the calculated time is 30 mins, the script should exit. can any one help me with that! Thanks in advance Regards :o (5 Replies)
Discussion started by: dndoon
5 Replies

9. Shell Programming and Scripting

How to calculate the time difference.

Hi All, I've written a script which reads all the systems backup information and saves it in a log file. ssh -l ora${sid} ${primaryhost} "tail -1 /oracle/$ORACLE_SID/sapbackup/back$ORACLE_SID.log" | awk '{print $3,$4,$5,$6}' >> ${RESULTFILE} The output comes as below: 2008-09-30 06.00.01... (2 Replies)
Discussion started by: suri.tyson
2 Replies

10. AIX

calculate time

Hi, How do I calculate time? I need to create an alert if a process is running more than 30 minutes. I need to get the first time and then get another, calculate it if more than 30 mins and then alert it to pager. Can't find it in internet. Thanks in advance, itik (2 Replies)
Discussion started by: itik
2 Replies

Featured Tech Videos