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 and shell scripting languages 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 08:19 AM
bash: "undefined variable" and pipe nagaidhlig Shell Programming and Scripting 6 02-18-2008 10:47 AM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 01:52 AM
bash: cd command to access "strange" directories robotronic Shell Programming and Scripting 3 07-06-2007 05:35 PM
$0 variable using ". file" format pavelmac UNIX for Dummies Questions & Answers 1 02-23-2005 12:37 AM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 09-14-2005
vikingshelmut vikingshelmut is offline
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.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:21 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0