Wednesday, August 20, 2014

Solaris calculating date difference

It is common necessity to know how long our script takes. In Solaris 11 just as any linux system the below will work:
$ START=$(date +%s)
$ END=$(date +%s)
$ DIFF=$(( $END - $START ))
$ echo $START
1408567010
$ echo $END
1408567017
$ echo echo "Finished in $DIFF seconds"
Finished in 7 seconds
However in Solaris 10 and below it won't and so a hack will be needed.
$ date +%s
%s
$ START=$(/usr/bin/truss /usr/bin/date 2>&1 | nawk -F= '/^time\(\)/ {gsub(/ /,"",$2);print $2}')
$ END=$(/usr/bin/truss /usr/bin/date 2>&1 | nawk -F= '/^time\(\)/ {gsub(/ /,"",$2);print $2}')
$ let DIFF=$(( $END - $START ))
$ echo $END
1408566266
$ echo $START
1408566245
$ echo echo "Finished in $DIFF seconds"
Finished in 21 seconds

No comments:

Followers