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
445 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
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 navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now