Using the Get Function

The run function is sufficiently powerful that it may be tempting to rely exclusively on parsing output to get information about the system, but this has the decided disadvantage that it leaves scripts parsing human-readable output that may or may not change in the future. To more robustly gather information about the system, use the built-in get function. In the case of the boot_time property, this will return not the string but rather the ECMAScript Date object, allowing the property value to be manipulated programmatically. For more reliable scriptable values, see Using the Prop Function.

  1. For example, you might want to use the boot_time property in conjunction with the current time to determine the time since boot:
    script
           run('configuration version');
           now = new Date();
           uptime = (now.valueOf() - get('boot_time').valueOf()) / 1000;
           printf('up %d day%s, %d hour%s, %d minute%s, %d second%s\n',
               d = uptime / 86400, d < 1 || d >= 2 ? 's' : '',
               h = (uptime / 3600) % 24, h < 1 || h >= 2 ? 's': '',
               m = (uptime / 60) % 60, m < 1 || m >= 2 ? 's': '',
               s = uptime % 60, s < 1 || s >= 2 ? 's': '');
  2. Assuming the above is saved as uptime.aksh, you could run it this way:
    $ ssh root@hostname < uptime.aksh
    Pseudo-terminal will not be allocated because stdin is not a terminal.
    Password:
    up 2 days, 10 hours, 47 minutes, 48 seconds

    The message about pseudo-terminal allocation is due to the SSH client; the issue that this message refers to can be managed by specifying the T option to SSH.