Using the List Function
In a context with dynamic children, it can be very useful to iterate over those children programmatically. This can be done by using the list function, which returns an array of dynamic children.
-
The following example script iterates over every share in every project,
printing out the amount of space consumed and space available:
script run('shares'); projects = list(); for (i = 0; i < projects.length; i++) { run('select ' + projects[i]); shares = list(); for (j = 0; j < shares.length; j++) { run('select ' + shares[j]); printf("%s/%s %1.64g %1.64g\n", projects[i], shares[j], get('space_data'), get('space_available')); run('cd ..'); } run('cd ..'); } - Here is the output of running the script, assuming it was saved to a file named
space.aksh:$ ssh root@hostname < space.aksh Password: admin/accounts 18432 266617007104 admin/exports 18432 266617007104 admin/primary 18432 266617007104 admin/traffic 18432 266617007104 admin/workflow 18432 266617007104 aleventhal/hw_eng 18432 266617007104 bcantrill/analytx 1073964032 266617007104 bgregg/dashbd 18432 266617007104 bgregg/filesys01 26112 107374156288 bpijewski/access_ctrl 18432 266617007104 ... - If you would prefer a "pretty printed" (although more difficult to handle programmatically) variant of this, you could directly parse the output of the
getcommand:script run('shares'); projects = list(); printf('%-40s %-10s %-10s\n', 'SHARE', 'USED', 'AVAILABLE'); for (i = 0; i < projects.length; i++) { run('select ' + projects[i]); shares = list(); for (j = 0; j < shares.length; j++) { run('select ' + shares[j]); share = projects[i] + '/' + shares[j]; used = run('get space_data').split(/\s+/)[3]; avail = run('get space_available').split(/\s+/)[3]; printf('%-40s %-10s %-10s\n', share, used, avail); run('cd ..'); } run('cd ..'); } - Here is the output of running this new script, assuming it was named
prettyspace.aksh:$ ssh root@hostname < prettyspace.aksh Password: SHARE USED AVAILABLE admin/accounts 18K 248G admin/exports 18K 248G admin/primary 18K 248G admin/traffic 18K 248G admin/workflow 18K 248G aleventhal/hw_eng 18K 248G bcantrill/analytx 1.00G 248G bgregg/dashbd 18K 248G bgregg/filesys01 25.5K 100G bpijewski/access_ctrl 18K 248G ... - The
listfunction supports optional argumentsdepthandfilter.The format is:
list ([depth, [filter]]). The argumentdepthcan be defined by a number. The greater number ofdepth, the more details will be returned. The argumentfilteris formatted as{<prop1>:<val1>, <prop2>:<val2> ...}. Iffilteris specified,depthmust also be specified.Usage and input behavior:
-
list()- Returns only node names. -
list(0)- Return properties of node and only children names. -
list(0, {kiosk_mode: true})- Return a filtered list forkiosk_modeistruewith names of children. -
list(1)- Return properties of node, names and properties of children, only names of grandchildren. -
list(1, {kiosk_mode: true})- Return a filtered list forkiosk_modeistruewith details up todepth=1. -
list(2)- Return properties of node, names and properties of children andlist(0)output of grandchildren. -
list(2, {fullname:'Super*', kiosk_mode: true})- Return a filtered list forfullnamecontainingSuperandkiosk_modeistruewith details up todepth=2.
-
- This is an example output for a list with
depth=2:The label
nameshows the name of the list item (that is, a node). The labelpropertiesshows the properties of the list item. The labelchildrenshows static children of the list item. The labellistshows dynamic children of the list item.script ("." to run)> dump(list(2)); ("." to run)> . [{ name: 'restuser', properties: { kiosk_screen: 'status/dashboard', kiosk_mode: false, roles: ['basic'], require_annotation: false, initial_password: 'DummyPassword', fullname: 'REST User', logname: 'restuser' }, children: [{ name: 'preferences', properties: { advanced_analytics: false, session_timeout: 15, login_screen: 'status/dashboard', locale: 'C' } }, { name: 'exceptions', list: [{ name: 'auth-000', properties: { allow_configure: false, scope: 'alert' } }, { name: 'auth-001', properties: { allow_workgroup: false, allow_domain: false, name: '*', scope: 'ad' } }] }] }]