Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
467 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses

610 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