![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | 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 here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
without going on the tangent of re-writting the function....
one way...... Code:
printf "%02d:%02d:%02d" "$HOUR" "$MINUTE" "$SECOND" |
|
#3
|
|||
|
|||
|
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! |
|
#4
|
||||
|
||||
|
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)) |
||||
| Google The UNIX and Linux Forums |