Bash script time script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script time script
# 8  
Old 12-22-2016
Hi Scrutinizer,
Yes, I apologize. Before the standards were there for the shell, at least one shell treated the simple command:
Code:
a=1 b=2 c=3

as the command c=3 with the other two assignments being treated as a command prefix (so a and b did not end up being present in the current shell execution environment; they were only available in the execution environment of the assignment command assigning a value to c). I don't remember if any shell still has this strange behavior, but since we're talking about POSIX shells in this thread it doesn't matter. With any POSIX conforming shell all three of the following produce the same results:
Code:
a=1 b=2 c=3

Code:
a=1;b=2;c=3

Code:
a=1
b=2
c=3

These 2 Users Gave Thanks to Don Cragun For This Post:
# 9  
Old 12-22-2016
thanks everyone. it works now! Smilie
# 10  
Old 12-22-2016
This should be basic /bin/sh compatible

Code:
#!/bin/sh

# turn seconds into real measurable time

num=${1:-0}
min=0
hour=0
day=0
if [ $num -gt 59 ]
then
    sec=`expr $num % 60`
    num=`expr $num / 60`
    if [ $num -gt 59 ]
    then
        min=`expr $num % 60`
        num=`expr $num / 60`
        if [ $num -gt 23 ]
        then
            hour=`expr $num % 24`
            day=`expr $num / 24`
        else
            hour=$num
        fi
    else
        min=$num
    fi
else
    sec=$num
fi
echo "$day"d,"$hour"h,"$min"m,"$sec"s

or it could be simplified a little (at the expense of some performance):

Code:
#!/bin/sh

# turn seconds into real measurable time

num=${1:-0}
day=`expr $num / 86400`
num=`expr $num % 86400`
hour=`expr $num / 3600`
num=`expr $num % 3600`
min=`expr $num / 60`
sec=`expr $num % 60`
echo "$day"d,"$hour"h,"$min"m,"$sec"s


Last edited by Chubler_XL; 12-22-2016 at 11:10 PM..
# 11  
Old 12-23-2016
Quote:
Originally Posted by Don Cragun
[..] With any POSIX conforming shell all three of the following produce the same results:
Code:
a=1 b=2 c=3

Code:
a=1;b=2;c=3

Code:
a=1
b=2
c=3

Thanks for looking into this and confirming this..

One possible caveat to note is that the order in which these assignments are being made within a single simple command is not defined by the standards as far as I know, so if assignments are interdependent then I think it may be best to use separate simple commands.

For example:
Code:
a=1 b=$a

will typically produce a=1 and b=1, but I do not think it is guaranteed by the standards. It might also be a=1 and b="", which would be the case if it is done right to left or in arbitrary order. Even though I have only come across implementations that go left to right.. ( Simple Commands )

Last edited by Scrutinizer; 12-23-2016 at 02:39 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 12  
Old 12-23-2016
Quote:
Originally Posted by Scrutinizer
... ... ...

One possible caveat to note is that the order in which these assignments are being made within a single simple command is not defined by the standards as far as I know, so if assignments are interdependent then I think it may be best to use separate simple commands.

For example:
Code:
a=1 b=$a

will typically produce a=1 and b=1, but I do not think it is guaranteed by the standards. It might also be a=1 and b="", which would be the case if it is done right to left or in arbitrary order. Even though I have only come across implementations that go left to right.. ( Simple Commands )
Hi Scrutinizer,
The standards do guarantee it... From The Commands and Utilities Volume of the 2016 edition of the POSIX Standards:
Quote:
2.9.1 Simple Commands
A ‟simple command'' is a sequence of optional variable assignments and redirections, in any sequence, optionally followed by words and redirections, terminated by a control operator.

When a given simple command is required to be executed (that is, when any conditional construct such as an AND-OR list or a case statement has not bypassed the simple command), the following expansions, assignments, and redirections shall all be performed from the beginning of the command text to the end:
From the beginning ... to the end in shell scripts means left to right. (In some other places in the standard referring to text that could be from a language that is written from right to left, beginning to end means left to right for text that is written from left to right and it means right to left for text that is written from right to left.)
These 3 Users Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

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
Login or Register to Ask a Question