#!/bin/sh
echo $((`ioreg -c IOHIDSystem | sed -e '/HIDIdleTime/
!{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'` / 1000000000))
NOTE: Remove the line break from the above echo line (i.e. ...HIDIdleTime/!{...); it's been split here for a narrower display width.
Obviously, running this script directly from the command line will always evaluate to zero, but you can test it with a sleep command:
%> sleep 4; system_idle_time
4
Replace system_idle_time with whatever you named the script, of course. The returned value is in seconds; adjust the divisor accordingly for other time increments.

