Bash script time script


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script time script
# 1  
Bash script time script

I have the following code which i'd like to rewrite in a way that it can be used on all unix systems. meaning, i want it to be portable:

Code:
# turn seconds into real measurable time

                                            num=$1
                                            min=0
                                            hour=0
                                            day=0
                                            if((num>59));then
                                                ((sec=num%60))
                                                ((num=num/60))
                                                if((num>59));then
                                                    ((min=num%60))
                                                    ((num=num/60))
                                                    if((num>23));then
                                                        ((hour=num%24))
                                                        ((day=num/24))
                                                    else
                                                        ((hour=num))
                                                    fi
                                                else
                                                    ((min=num))
                                                fi
                                            else
                                                ((sec=num))
                                            fi
                                            echo "$day"d,"$hour"h,"$min"m,"$sec"s

the shell i intend to use is basic shell..i.e. /bin/sh but as many of you can tell already, the above is written in newer form of bash which is simply not available on some systems.
# 2  
This would be the POSIX equivalent of you bash snippet, which should also run correctly in bash itself.
Give this a try:
Code:
                                            num=$1
                                            min=0 hour=0 day=0
                                            if [ $num -gt 59 ]; then
                                                sec=$((num%60))
                                                num=$((num/60))
                                                if [ $num -gt 59 ];then
                                                    min=$((num%60))
                                                    num=$((num/60))
                                                    if [ $num -gt 23 ];then
                                                        hour=$((num%24))
                                                        day=$((num/24))
                                                    else
                                                        hour=$num
                                                    fi
                                                else
                                                    min=$num
                                                fi
                                            else
                                                sec=$num
                                            fi
                                            printf "%dd,%dh,%dm,%ds\n" "$day" "$hour" "$min" "$sec"

--
Note: on Solaris use /usr/xpg4/bin/sh (/bin/sh on Solaris is the Bourne shell, which is not POSIX compliant and cannot run this code)

--
Note: echo "$day"d,"$hour"h,"$min"m,"$sec"s should probably work too, but printf is the standardized way in POSIX

Last edited by Scrutinizer; 12-22-2016 at 08:11 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
i tried this on aix and it didnt work:

Code:
$ uname -a
AIX pnim01 3 5 00CD78AD4C00
$ 
$ cat mytime.sh
#!/bin/sh

num=5600
                                            min=0 hour=0 day=0
                                            if [ $num -gt 59 ]; then
                                                sec=$((num%60))
                                                num=$((num/60))
                                                if([ $num -gt 59 ];then
                                                    min=$((num%60))
                                                    num=$((num/60))
                                                    if([ $num -gt 23 ];then
                                                        hour=$((num%24))
                                                        day=$((num/24))
                                                    else
                                                        hour=$num
                                                    fi
                                                else
                                                    min=$num
                                                fi
                                            else
                                                sec=$num
                                            fi
                                            echo "$day"d,"$hour"h,"$min"m,"$sec"s
$ 
$ 
$ ./mytime.sh  
./mytime.sh[5]: 0403-057 Syntax error at line 9 : `then' is not expected.

# 4  
Yes, I was still in the process of editing my code when you copy/pasted it. Try the code as it is now in post #2 again
# 5  
Code:
if([ $num -gt 59 ];then

Remove that `(' from the ifs. There are several.
This User Gave Thanks to Aia For This Post:
# 6  
Note also that:
Code:
                                            min=0 hour=0 day=0

should be changed to either:
Code:
                                            min=0;hour=0;day=0

or:
Code:
                                            min=0
                                            hour=0
                                            day=0

This User Gave Thanks to Don Cragun For This Post:
# 7  
Quote:
Originally Posted by Don Cragun
Note also that:
Code:
                                            min=0 hour=0 day=0

should be changed to either:
Code:
                                            min=0;hour=0;day=0

or:
Code:
                                            min=0
                                            hour=0
                                            day=0

Hi Don,

From the Shell Command Language: Shell Grammar Rules:

Code:
simple_command   : cmd_prefix cmd_word cmd_suffix
                 | cmd_prefix cmd_word
                 | cmd_prefix
                 | cmd_name cmd_suffix
                 | cmd_name
[..]
cmd_prefix       :            io_redirect
                 | cmd_prefix io_redirect
                 |            ASSIGNMENT_WORD
                 | cmd_prefix ASSIGNMENT_WORD


So I do not understand why this could be a problem. It certainly works fine wherever I try it.
These 2 Users Gave Thanks to Scrutinizer For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #133
Difficulty: Easy
The first prototypes of Linux were publicly released in 1991.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

2. Shell Programming and Scripting

Controlling time stamps in a bash script

Hi, I have a bash script that generates CSV (.txt) files at fairly regular time intervals. I'm currently time stamping each batch of measurements at the time I write the rows into a MySQL database. As the result, one set of data might get the time 12:01:32 and the next set of data gets the time... (18 Replies)
Discussion started by: Zooma
18 Replies

3. Shell Programming and Scripting

How to add missing date and time in a bash script?

Hi Again, I have a file that contains date and time for the past 2 hours. What i need is add missing date and time in a file. INPUT 2016-01-13 01:33 10 2016-01-13 01:31 10 2016-01-13 01:30 10 2016-01-13 01:29 10 2016-01-13 01:28 10 2016-01-13 01:27 10 2016-01-13 01:26 10 2016-01-13... (14 Replies)
Discussion started by: ernesto
14 Replies

4. Shell Programming and Scripting

Script in bash that works only some of the time

I ran this script yesterday (in the background) /usr/bin/nohup myfilelocation/myscriptname.sh & the script worked perfectly. i ran it today (also in the background) and just sat there. So i killed it and ran it normally and it worked perfectly. Anyone suggest why it just sat there and... (8 Replies)
Discussion started by: twinion
8 Replies

5. Shell Programming and Scripting

Bash Script Looping all the time

Hello, I have a database file, named data.txt, and a shell script (convert.sh) to convert data.txt from columns to row. Output file name will be column_to_row.txt In this example data.txt has only four rows. Format of data.txt is: info name surname telefon_nr Data.txt info boris... (1 Reply)
Discussion started by: baris35
1 Replies

6. Shell Programming and Scripting

Need bash script to ping the servers and rename the output file each time the script is ran

HI, I have a file serverlist in that all host names are placed. i have written a small script #./testping #! /bin/bash for i in `cat serverlist` do ping $i >> output.txt done so now it creates a file output.txt till here fine.. now each time i run this script the output file... (4 Replies)
Discussion started by: madhudeva
4 Replies

7. Shell Programming and Scripting

How to compare time in bash script?

Hi, Anyone know how to compare the time in bash script? I want to compare say 30 min. to 45 min. ( AIX ) Thanks. (1 Reply)
Discussion started by: sumit30
1 Replies

8. Shell Programming and Scripting

Changing File Time Stamp (Bash Script)

I need some help recovering from a "slight" screwup. We just moved 3 TB of data from one RAID Array to another. Low lever archive files. This was done with a regular cp (for some reason) and now we have lost all the timestamps on the files, and we urgently need to get the timestamps back on these... (7 Replies)
Discussion started by: chj
7 Replies

9. Shell Programming and Scripting

execution time / runtime -- bash script please help!

Hello, I'm running a bash script and I'd like to get more accurate a runtime information then now. So far I've been using this method: STARTM=`date -u "+%s"` ......... *script function.... ......... STOPM=`date -u "+%s"` RUNTIMEM=`expr $STOPM - $STARTM` if (($RUNTIMEM>59)); then... (6 Replies)
Discussion started by: TehOne
6 Replies

10. Shell Programming and Scripting

bash script to count the time of transaction

Halo, Bash Script can get the time of process the trasaction or not? For example, bash script use to procee the trasaction, like select and checking.. then generate the XML. after it, i need to get the time which to count the process. Anyone can help me? Thank you (1 Reply)
Discussion started by: ryanW
1 Replies

Featured Tech Videos