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
455 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
blank screen when trying to setup Unity on Ubuntu 14.04 9 56
Can't connect to FTP 18 110
IMAP copying tool 14 49
NFS v4 7 20
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

726 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