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:
# 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.
Hi Scrutinizer,
Yes, I apologize. Before the standards were there for the shell, at least one shell treated the simple command:
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:
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:
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:
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.)