Using The Run Function; Using The Get Function - Oracle ZFS Storage Appliance Administration Manual

Hide thumbs Also See for ZFS Storage Appliance:
Table of Contents

Advertisement

Using the Run Function

Using the Run Function
1.
The simplest way for scripts to interact with the larger system is to use the "run"
function: it takes a command to run, and returns the output of that command as
a string. For example:
dory:> configuration version script dump(run('get boot_time'))
'
2.
The built-in dump function dumps the argument out, without expanding any
embedded newlines. ECMAScript's string handling facilities can be used to take
apart output. For example, splitting the above based on whitespace:
dory:> configuration version script dump(run('get boot_time').split(/\s+/))
['', 'boot_time', '=', '2009-10-12', '07:02:17', '']

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.
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
2.
Assuming the above is saved as a "uptime.aksh", you could run it this way:
% ssh root@dory < uptime.aksh
Pseudo-terminal will not be allocated because stdin is not a terminal.
Password:
up 2 days, 10 hours, 47 minutes, 48 seconds
46
Oracle ZFS Storage Appliance Administration Guide, Release OS8.6.x • September 2016
boot_time = 2009-10-12 07:02:17\n'
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': '');

Advertisement

Table of Contents
loading

Table of Contents