Please help formatting bash "time" variable to HH:MM:SS format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Please help formatting bash "time" variable to HH:MM:SS format
# 1  
Old 09-14-2005
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.
# 2  
Old 09-14-2005
without going on the tangent of re-writting the function....
one way......
Code:
printf "%02d:%02d:%02d" "$HOUR" "$MINUTE" "$SECOND"

# 3  
Old 09-15-2005
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  
Old 09-15-2005
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))

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

"Help with bash script" - "License Server and Patch Updates"

Hi All, I'm completely new to bash scripting and still learning my way through albeit vey slowly. I need to know where to insert my server names', my ip address numbers through out the script alas to no avail. I'm also searching on how to save .sh (bash shell) script properly.... (25 Replies)
Discussion started by: profileuser
25 Replies

3. Shell Programming and Scripting

Storing output of "time" command to a variable

Hi all, I am new to Linux/shell scripting having moderate knowledge. In my script, I need to get execution time of a command (say 'ls') in mili seconds level. For this i tried using "time" command to retrieve the total execution time in milli seconds. But, the problem is that, how to save... (9 Replies)
Discussion started by: happening_linux
9 Replies

4. Shell Programming and Scripting

Simplify Bash Script Using "sed" Or "awk"

Input file: 2 aux003.net3.com error12 6 awn0117.net1.com error13 84 aux008 error14 29 aux001.ha.ux.isd.com error12 209 aux002.vm.ux.isd.com error34 21 alx0027.vm.net2.com error12 227 dux001.net5.com error123 22 us008.dot.net2.com error121 13 us009.net2.com error129Expected Output: 2... (4 Replies)
Discussion started by: sQew
4 Replies

5. Shell Programming and Scripting

How to store the output of "time dd if= of=" in a variable

Hi All, I need to store the real seconds of the following command in a variable. How could it be done? time $(dd if=/dev/zero of=/dev/sda1 bs=512 count=2048;sync) Thanks, Amio (12 Replies)
Discussion started by: amio
12 Replies

6. Shell Programming and Scripting

How to remove "New line characters" and "spaces" at a time

Dear friends, following is the output of a script from which I want to remove spaces and new-line characters. Example:- Line1 abcdefghijklmnopqrstuvwxyz Line2 mnopqrstuvwxyzabcdefghijkl Line3 opqrstuvwxyzabcdefdefg Here in above example, at every starting line there is a “tab” &... (4 Replies)
Discussion started by: anushree.a
4 Replies

7. UNIX for Advanced & Expert Users

add seconds to: date"|"time"|"HHMMSS

Hey all, I have a shell that invokes a AWK. In this AWK i want invoke a function that receives 3 parameters: date: 20080831 time: 235901 duration: 00023 that function receive this 3 parameters and sum to this value two more seconds: 2008083123590100025 Remember that in case that... (3 Replies)
Discussion started by: anaconga
3 Replies

8. Shell Programming and Scripting

bash: "undefined variable" and pipe

Hi, haven't found anything about this through searching, so may be a new topic: when doing this: set -o nounset set -o errexit find . -name "*.lib" | while read library; do echo ${libary} done echo "after while" I expect the script to exit within the while loop (because of nounset and... (6 Replies)
Discussion started by: nagaidhlig
6 Replies

9. UNIX for Dummies Questions & Answers

$0 variable using ". file" format

I need to access the script name within a korn shell script. cat tst1.sh echo Script Name: $0 when tst1.sh is run normally the script reports "Script Name: tst1.sh" However when I run the shell as ". tst1.sh" the script reports: "Script Name: -ksh" How do I access the script name when... (1 Reply)
Discussion started by: pavelmac
1 Replies
Login or Register to Ask a Question