Sponsored Content
Top Forums Shell Programming and Scripting Please help formatting bash "time" variable to HH:MM:SS format Post 83426 by vikingshelmut on Wednesday 14th of September 2005 05:28:34 PM
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.
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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

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

7. 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

8. 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

9. 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
BASHBUG(1)						      General Commands Manual							BASHBUG(1)

NAME
bashbug - report a bug in bash SYNOPSIS
bashbug [--version] [--help] [email-address] DESCRIPTION
bashbug is a shell script to help the user compose and mail bug reports concerning bash in a standard format. bashbug invokes the editor specified by the environment variable EDITOR on a temporary copy of the bug report format outline. The user must fill in the appropriate fields and exit the editor. bashbug then mails the completed report to bug-bash@gnu.org, or email-address. If the report cannot be mailed, it is saved in the file dead.bashbug in the invoking user's home directory. The bug report format outline consists of several sections. The first section provides information about the machine, operating system, the bash version, and the compilation environment. The second section should be filled in with a description of the bug. The third sec- tion should be a description of how to reproduce the bug. The optional fourth section is for a proposed fix. Fixes are encouraged. ENVIRONMENT
bashbug will utilize the following environment variables if they exist: EDITOR Specifies the preferred editor. If EDITOR is not set, bashbug defaults to emacs. HOME Directory in which the failed bug report is saved if the mail fails. TMPDIR Directory in which to create temporary files and directories. SEE ALSO
bash(1) AUTHORS
Brian Fox, Free Software Foundation bfox@gnu.org Chet Ramey, Case Western Reserve University chet@po.cwru.edu GNU Bash-4.0 1998 July 30 BASHBUG(1)
All times are GMT -4. The time now is 09:41 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy