zfs und zpool: Ausgabe parsen

Mit Hilfe der Option -H bei zfs und zpool lässt sich die Ausgabe leicht mit regulären Ausdrücken verarbeiten.

Mit der Option -H wird der Scriptmodus bei den Befehlen
  1. 
  2. zpool list [-H] [-o props[,...]] [pool] ...
  3. 
  4. zfs get [-r|-d depth] -H [-p] [-o field[,...] [-s source[,...] "all" |
  5. property[,...] filesystem|volume|snapshot ...
  6. 
  7. zfs list [-r|-d depth] -H [-o property[,...]] [-t type[,...]]
  8. [-s property] ... [-S property] ... [filesystem|volume|snapshot] ...
  9. 
  10. zfs userspace -H [-nip] [-o field[,...]] [-sS field]... [-t type [,...]]
  11. filesystem | snapshot

aktivieren. Damit werden keine Kopfzeilen ausgegeben und Spalten durch Tab getrennt.

Ein paar Zeilen PHP-Code verdeutlichen das Parsen:

<?php&#13;
# Get all properties, use scripting mode&#13;
# Output will be saved in the array $out&#13;
exec('zfs get -H all zroot/usr', $out);&#13;
&#13;
# Loop over all lines of the output&#13;
foreach ($out as $row) {&#13;
    // split the line, use tab as delimiter&#13;
    $p = preg_split("/\t/", $row);&#13;
    // print the current property and the  associated value&#13;
    printf("%s =&gt; %s\n", $p[1], $p[2]);&#13;
}&#13;
?>