The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Why generate "ash and bash" different output for same bash script? s. murat Shell Programming and Scripting 0 05-26-2008 04:19 AM
bash: "undefined variable" and pipe nagaidhlig Shell Programming and Scripting 6 02-18-2008 07:47 AM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-19-2007 10:52 PM
bash: cd command to access "strange" directories robotronic Shell Programming and Scripting 3 07-06-2007 01:35 PM
$0 variable using ". file" format pavelmac UNIX for Dummies Questions & Answers 1 02-22-2005 09:37 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-14-2005
Registered User
 

Join Date: Sep 2005
Posts: 6
Please help formatting bash "time" variable to HH:MM:SS format

Ok, this is going to be hard to describe, but here it goes.

I have written a bash script that, while executing starts a timer, and when done stops the timer. The $RUNTIME variable value is in seconds, so the variable usually equals a number like 126 (equals 2 minutes 6 seconds). In my script I want to format the variable into something like the output of "date "+%H:%M:%S" (usually a time formatted as HH:MM:SS). I have writtin/borrowed a function that almost does what I want, except that when called, it does not prepend a "0" to any value that is only a single digit, so my function returns values like:
12:56:17 (all two digit values seperated by colons)
or
1:5:18 (single digits do not have the preceding "0")
I want to force the output of this function to always display all values as two character numbers (ie 01, 02, 07...). Here is my function:
secondsConvert ()
{
RUNTIME=`expr $STOP - $START`
TEMP0=`expr $RUNTIME / 60`
SECOND=`expr $RUNTIME - $TEMP0 \* 60`
TEMP1=`expr $TEMP0 / 60`
MINUTE=`expr $TEMP0 - $TEMP1 \* 60`
TEMP2=`expr $TEMP1 / 24`
HOUR=`expr $TEMP1 - $TEMP2 \* 24`
echo ""$HOUR":"$MINUTE":"$SECOND""
}
So again, how to I force the value of the variables $HOUR, $MINUTE, &$SECOND to always output two digits, even if the hour is less than 10? I thought I could pass the values to sed, then allow sed to convert single digit numbers into two digit numbers, but to be honest, I don't have any clue how to do that.

So, just to clarify all the above, I'm looking for a function that will take a value in seconds and convert it to the same format as the date command (HH:MM:SS).

I apologize if this makes no sense, as I am new at this, and this was hard to explain. Please any feedback is appreciated.
Reply With Quote
Forum Sponsor
  #2  
Old 09-14-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
without going on the tangent of re-writting the function....
one way......
Code:
printf "%02d:%02d:%02d" "$HOUR" "$MINUTE" "$SECOND"
Reply With Quote
  #3  
Old 09-15-2005
Registered User
 

Join Date: Sep 2005
Posts: 6
Thanks!

Thanks, that worked perfectly. BTW I did rewrite my function as follows, including your input:
elapsedTotal ()
{
RUNTIME=`expr $STOP - $START`
HOURS=`expr $RUNTIME / 3600`
REMAINDER=`expr $RUNTIME % 3600`
MINS=`expr $REMAINDER / 60`
SECS=`expr $REMAINDER % 60`
printf "%02d:%02d:%02d\n" "$HOURS" "$MINS" "$SECS"
}
Thanks again!
Reply With Quote
  #4  
Old 09-15-2005
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,249
You can use the shell for integer arithmetic instead of expr, e.g....
Code:
printf "%02d:%02d:%02d\n" $((RUNTIME/3600)) $((RUNTIME/60%60)) $((RUNTIME%60))
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:04 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0