1

I am trying to get the number of seconds since the epoch in both Solaris 10 and Solaris 11.

On Solaris 11, "date +%s" is giving me the output (from bash), but the same is failing on Solaris 10.

What is the right command in Solaris 10?

2
  • I have a C program for this which uses SIGUSR1 to output to the screen if you want... Commented Mar 9, 2016 at 15:28
  • Please review suggested answers and mark one as solution if it works for you. Otherwise add comments so we can further improve the answers. Commented Mar 10, 2016 at 7:25

3 Answers 3

6

I would use nawk:

nawk "BEGIN{print srand}"

More portably in POSIX shells (such as /usr/xpg4/bin/sh on Solaris 10):

command -p awk 'BEGIN{srand(); print srand()}'

(on Solaris 10, from /bin/sh which was the Bourne shell, you would invoke /usr/xpg4/bin/awk to get a standard awk (the one in /bin being an ancient one from the 70s), or do PATH=`getconf PATH`${PATH+:$PATH} for the standard utilities including awk to be preferred over the ancient ones; that also applies to Solaris 11 where /bin/sh is no longer an ancient shell but most of the other tools in /bin still are).

5
  • Gives a syntax error at my end? Commented Mar 9, 2016 at 13:53
  • I just ran it on my solaris 10 cluster and it worked. Can you verify you don't miss typed it? Commented Mar 9, 2016 at 13:54
  • I appreciate the OP's query is specifically about Solaris, so it's a valid answer, but that format doesn't work on Linux and if I use srand() I get 1 as the output. I believe it's using an undocumented feature, rather than a function for which it's designed and may not be reliable long-term. Are you sure you ran srand and not srand()? Commented Mar 9, 2016 at 13:56
  • Thank you for your remark. I don't think it does matter are you using srand or srand(). Still i tested it both ways on my solaris 10 test server and it worked. Commented Mar 9, 2016 at 15:59
  • 1
    @EightBitTony The use of srand to get time of day via nawk is documented in the Solaris nawk man page. Commented Mar 9, 2016 at 19:02
0

I usually revert to perl for this, assuming it is installed.

perl -e 'print time."\n";'

For example,

$ perl -e 'print time."\n";'
1457531088

Assuming perl is installed, it should be pretty much OS independent .

0

Both Solaris 10 and 11 should come with zsh as long as it's not a minimal install (in which case it wouldn't come with bash either), so you could switch to that there the current epoch time is in the $EPOCHSECONDS special variable after you've loaded the zsh/datetime module.

$ zmodload zsh/datetime
$ echo $EPOCHSECONDS
1684393493

That module also comes with a strftime builtin that can format that $EPOCHSECONDS into a human representation or with -r convert a human representation back to epoch seconds.

In Solaris 11, ksh and sh are ksh93 and in Solaris 10, /usr/dt/bin/dtksh (if a graphical environment is available) should be based on ksh93 (an ancient version though). ksh93's printf builtin can also format and parse dates.

printf '%(%s)T\n' now

Should give you the epoch time in seconds. now can be some other common date specifications, including human representations common in the US, some ISO ones and even crontab time specifications.

bash copied a subset of that (only the formatting part, not the parsing part) in 4.2, and zsh's $EPOCHSECONDS in 5.0 (not strftime).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .