![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
| Display Modes | Rate This Thread |
|
|