Solved

Is there any easy way to convert seconds into hh:mm:ss in a bash script using standard tools like DATE, GAWK, PRINTF etc etc?

Posted on 2006-06-14
10
451 Views
Last Modified: 2013-12-16
I have a few shell scripts that run on my webserver which runs CentOS release 4.3 (redhat).  I am interested in timing these scripts to see how long they are taking, but I have only been able to figure out how to determine total # of seconds, like:

#!/bin/bash
beginTime=$(date +%s)
sleep 90
endTime=$(date +%s)
echo `expr $endTime - $beginTime`

so this script will output "90".  I would like to make it output "1:30" or perhaps "00:01:30"

is there any way??
0
Comment
Question by:LuckMan212
  • 5
  • 3
  • 2
10 Comments
 
LVL 16

Expert Comment

by:xDamox
ID: 16901305
Hi,

The best thing to do is run the time command e.g.

time ls

This will time how long ls took to execute you will get output similar to:

real    0m0.015s
user    0m0.004s
sys     0m0.000s

0
 

Author Comment

by:LuckMan212
ID: 16902869
Yes  I am aware of the time command but how can I use it to "wrap" the entire script... I need to time the whole script and I would like to capture the output into a variable so it can be part of the report that gets emailed to me at the end of the execution.

Would I need 2 scripts to do this?
like:

#!/bin/bash
#timer_script
let TIME_TAKEN=`time actual_script | head -n1 | awk '{print $2}'`

.
.
.

#!/bin/bash
#actual_script
...blah blah

is there some way to do this with only 1 script?
0
 
LVL 16

Expert Comment

by:xDamox
ID: 16903293
Hi,

this works fine:

#!/bin/bash

MIN=`date +%M`;
SEC=`date +%S`;

sleep 90;

min=`date +%M`;
sec=`date +%S`;

echo "The number of minutes " `expr $min - $MIN`;
echo "The number of seconds " `expr $sec - $SEC`;
echo ""


0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:LuckMan212
ID: 16903886
that method has a number of problems... if the script starts at 6:50:06 pm and ends at 7:11:39pm, it will tell you that it took -39:33 minutes.  THere are an almost unlimited number of other circumstances in which that method will also not work.
0
 
LVL 22

Expert Comment

by:pjedmond
ID: 16907224
echo $sec | awk {'h=int($0/3600);r=($0-(h*3600));m=int(r/60);s=(r-(m*60)); print h ":" m ":" s'}

Should do the job:)
0
 
LVL 22

Expert Comment

by:pjedmond
ID: 16907229
...obviously wher $sec is the number of seconds:)
0
 
LVL 22

Expert Comment

by:pjedmond
ID: 16907276
Which makes your program above (timetest):

------------------8X--------------------------------
#!/bin/bash
beginTime=$(date +%s)
sleep 90
endTime=$(date +%s)
echo `expr $endTime - $beginTime` | awk {'h=int($0/3600);r=($0-(h*3600));m=int(r/60);s=(r-(m*60)); print h ":" m ":" s'}
------------------8X--------------------------------
giving:

[pje@bigserver tmp]# ./timetest
0:1:30
0
 
LVL 22

Accepted Solution

by:
pjedmond earned 50 total points
ID: 16907550
Noticed that you need the hours/mins/secs padded:

hh:mm:ss, so:

echo 103 | awk '{h=int($0/3600);r=($0-(h*3600));m=int(r/60);s=(r-(m*60)); printf "%02.0f:%02.0f:%02.0f\n", h, m, s}'

or ammending your program to be:

------------------8X--------------------------------
#!/bin/bash
beginTime=$(date +%s)
sleep 90
endTime=$(date +%s)
echo `expr $endTime - $beginTime` | awk '{h=int($0/3600);r=($0-(h*3600));m=int(r/60);s=(r-(m*60)); printf "%02.0f:%02.0f:%02.0f\n", h, m, s}'

------------------8X--------------------------------
giving:

[pje@bigserver tmp]# ./timetest
00:01:30

Also note that for this awk statement, I needed to move the position of the external 's in order to get printf to work correctly! Obfiously you can change the awk printf formating to get the format that you require if something different is required.

HTH:)

0
 

Author Comment

by:LuckMan212
ID: 16908683
wow pjedmond, that is some fancy footwork there..  that is awesome!
ok, this is the 1st question I've ever posted at EE.  How do I award the points?
0
 
LVL 22

Expert Comment

by:pjedmond
ID: 16909672
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Debug VNC connection on CentOS7 server 22 84
i can not do linux commands in cygwin windows 10 6 74
Coding C# in Linux 8 61
bash file 10 36
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

775 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question