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
458 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Fine Tune your automatic Updates for Ubuntu / Debian
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.:

688 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