get execution time of a script

Hi,

I have a simple question. How can I get the execution time of a script and maybe put it in a variable?

Another question. How can I get only time and not date and put it in a variable? I tried something with "date" command but with no success...

If someone could help me...

$ script_times=$(/usr/bin/time -p my_script 3>&2 2>&1 1>&3 3>&-)
$ echo "$script_times

real   0.01
user   0.00
sys    0.01
$
$ current_time=$(date +%T)
$ echo $current_time
11:26:20
$ current_time=$(date +'%H.%M.%S')
$ echo $current_time              
11.27.50
$

Jean-Pierre.

ok, thx a lot for your answer.

Another question : what is the difference between real time, user time and system time?

And I just realized that if forgot a part of my question previously so I ask it now :

In fact i run a script which is calling other scripts. And what i'd like to do is to print at the end of each children script something like

Thx for your answers

You can do something like that :

elapsed()
{
   (( seconds  = SECONDS ))
   "$@"
   (( seconds = SECONDS - seconds ))
   (( etime_seconds = seconds % 60 ))
   (( etime_minuts  = ( seconds - etime_seconds ) / 60 % 60 ))
   (( etime_hours   = seconds / 3600 ))
   (( verif = etime_seconds + (etime_minuts * 60) + (etime_hours * 3600) ))
   echo "elapsed time: ${etime_hours}h${etime_minuts}m${etime_seconds}s"
 }

elapsed sleep 67

Output:

elapsed time: 0h1m7s

Jean-Pierre.

Thx a lot Jean Pierre,
I tested it and it works.... in bash shell. And i need it for a csh script and here it doesn't work (snif..)
When I just copying the script it send me this error :

and when I remove the first (), it worst... :

I'll try to find out something by myself, but if you have any suggestion, i'll appreciate it!

And sorry not to tell what kind of shell i was writting at the beginning.... :o:o

Regards

Hi,

I don't know if I can ask, but I found this function very interesting..

elapsed()
{
   (( seconds  = SECONDS ))
   "$@"
   (( seconds = SECONDS - seconds ))
   (( etime_seconds = seconds % 60 ))
   (( etime_minuts  = ( seconds - etime_seconds ) / 60 % 60 ))
   (( etime_hours   = seconds / 3600 ))
   (( verif = etime_seconds + (etime_minuts * 60) + (etime_hours * 3600) ))
   echo "elapsed time: ${etime_hours}h${etime_minuts}m${etime_seconds}s"
 }

At first time I didn't try to make it work, but I was wrong. In fact each time I am using this function on my script I get

elapsed time: 0h0m0s

Do you mind to tell me how to make it work correctly ? can you please help me ?

Thanks

How are you using this function?
if you are doing something like this :

code code
code code
code code
elapsed

I understand why it doesn't works. You have to specify on which script you want to apply elapsed.
e.g :

elapsed yourscript.sh

Jean-Pierre used "sleep 67" as an example.

Regards

Dear Moumou,

You're right. I didn't see this fonction on that way. As I am aleady using a script that needs an input file, I can't using like this :

elapsed myscript.ksh input_file

do you know a way to do the same thing on my script ?

I am looking to monitor the execution time of myscript.ksh

Thanks

You can use this function even with a script who needs parameters.
Indeed, in the example of "sleep 67", "67" is a parameter.

I use it withe a script who has 10 parameters and it works very well!

Regards