# HG changeset patch # User František Kučera <franta-hg@frantovo.cz> # Date 1543959216 -3600 # Node ID d342de2e09a48d0757aad1baf17b6028094389dd # Parent 029dc972f1f2dd93c91dc994d2b9aef1a70e3dd1 examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl diff -r 029dc972f1f2 -r d342de2e09a4 relpipe-data/examples.xml --- a/relpipe-data/examples.xml Tue Dec 04 18:43:01 2018 +0100 +++ b/relpipe-data/examples.xml Tue Dec 04 22:33:36 2018 +0100 @@ -38,6 +38,77 @@ If an error is found, it is reported on STDERR. So just omit the <code>&</code> in order to see the error message. </p> + + <h3>/etc/fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl</h3> + + <p> + As we have seen before, we can convert <code>/etc/fstab</code> (or <code>mtab</code>) + to e.g. an XML or a nice and colorful table using <m:name/>. + But we can also convert these data back to the <code>fstab</code> format. And do it with proper indentation/padding. + Fstab has a simple format where values are separated by one or more whitespace characters. + But without proper indentation, these files look a bit obfuscated and hard to read (however, they are valid). + </p> + + <m:pre jazyk="text" src="examples/relpipe-out-fstab.txt"/> + + <p> + So let's build a pipeline that reformats the <code>fstab</code> and makes it more readable. + </p> + + <m:pre jazyk="bash">relpipe-in-fstab | relpipe-out-fstab > reformatted-fstab.txt</m:pre> + + <p> + We can hack together a script called <code>relpipe-out-fstab</code> that accepts relational data and produces <code>fstab</code> data. + Later this will be probably implemented as a regular tool, but for now, it is just an example of a ad-hoc shell script: + </p> + + <m:pre jazyk="bash" src="examples/relpipe-out-fstab.sh" odkaz="ano"/> + + <p> + In the first part, we prepend a single record (<code>relpipe-in-cli</code>) before the data coming from STDIN (<code>cat</code>). + Then, we use <code>relpipe-out-nullbyte</code> to convert relational data to values separated by a null-byte. + This command processes only attribute values (relation and attribute names are skipped). + Then we used <code>xargs</code> to read the null-separated values and execute a Perl command for each record (pass to it a same number of arguments, as we have attributes: <code>--max-args=7</code>). + Perl does the actual formatting: adds padding and does some little tunning (merges two attributes and replaces empty values with <em>none</em>). + </p> + + <p>This is formatted version of the <code>fstab</code> above:</p> + + <m:pre jazyk="text" src="examples/relpipe-out-fstab.formatted.txt"/> + + <p> + And using following command we can verify, that the files differ only in comments and whitespace: + </p> + + <pre>relpipe-in-fstab | relpipe-out-fstab | diff -w /etc/fstab -</pre> + + <p> + Regular implementation of <code>relpipe-out-fstab</code> will probably keep the comments + (it needs also one more attribute and small change in <code>relpipe-in-fstab</code>). + </p> + + <p> + For just mere <code>fstab</code> reformatting, this approach is a bit overengineering. + Wo could skip the whole relational thing and do just something like this: + </p> + + <m:pre jazyk="bash">cat /etc/fstab | grep -v '^#' | sed -E 's/\s+/\n/g' | tr \\n \\0 | xargs -0 -n7 ...</m:pre> + + <p> + plus prepend the comment (or do everything in Perl). + But this example is intended as a demostration, how we can + 1) prepend some additional data before the data from STDIN + 2) use <m:name/> and traditional tools like <code>xargs</code> or <code>perl</code> together. + And BTW we have implemented a (simple but working) <em>relpipe output filter</em> – and did it without any serious programming, just put some existing commands together :-) + </p> + + <blockquote> + <p> + There is more Unix-nature in one line of shell script than there is in ten thousand lines of C. + <m:podČarou>see <a href="http://www.catb.org/~esr/writings/unix-koans/ten-thousand.html">Master Foo and the Ten Thousand Lines</a></m:podČarou> + </p> + </blockquote> + </text> </stránka> diff -r 029dc972f1f2 -r d342de2e09a4 relpipe-data/examples/relpipe-out-fstab.formatted.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/relpipe-data/examples/relpipe-out-fstab.formatted.txt Tue Dec 04 22:33:36 2018 +0100 @@ -0,0 +1,6 @@ +# <file system> <mount point> <type> <options> <dump> <pass> +UUID=29758270-fd25-4a6c-a7bb-9a18302816af / ext4 relatime,user_xattr,errors=remount-ro 0 1 +/dev/sr0 /media/cdrom0 udf,iso9660 user,noauto 0 0 +/dev/sde /mnt/data ext4 relatime,user_xattr,errors=remount-ro 0 2 +UUID=a2b5f230-a795-4f6f-a39b-9b57686c86d5 /home btrfs relatime 0 2 +/dev/mapper/sdf_crypt /mnt/private xfs relatime 0 2 diff -r 029dc972f1f2 -r d342de2e09a4 relpipe-data/examples/relpipe-out-fstab.sh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/relpipe-data/examples/relpipe-out-fstab.sh Tue Dec 04 22:33:36 2018 +0100 @@ -0,0 +1,22 @@ +#!/bin/bash + +( + # Just troll-in the first record: + relpipe-in-cli generate Heathers 7 \ + Duke string McNamara string Chandler string Veronica string J.D. string \ + Big string Fun string \ + '' '# <file system>' '<mount point>' '<type>' '<options>' '<dump>' '<pass>'; + # relpipe-out-nullbyte processes only attribute values + # Read the actual pipe's input: + cat +) | relpipe-out-nullbyte \ + | xargs --null --max-args=7 perl -e \ + 'printf("%-*s %-*s %-*s %-*s %-*s %s\n", + # following numbers define paddings: + 50, ($ARGV[0] eq "" ? $ARGV[1] : "$ARGV[0]=$ARGV[1]"), + 40, ($ARGV[2] eq "" ? "none" : $ARGV[2]), + 16, $ARGV[3], + 50, $ARGV[4], + 8, $ARGV[5], + $ARGV[6] + );' diff -r 029dc972f1f2 -r d342de2e09a4 relpipe-data/examples/relpipe-out-fstab.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/relpipe-data/examples/relpipe-out-fstab.txt Tue Dec 04 22:33:36 2018 +0100 @@ -0,0 +1,13 @@ +# /etc/fstab: static file system information. +# +# Use 'blkid' to print the universally unique identifier for a +# device; this may be used with UUID= as a more robust way to name devices +# that works even if disks are added and removed. See fstab(5). +# +# <file system> <mount point> <type> <options> <dump> <pass> +# / was on /dev/vda1 during installation +UUID=29758270-fd25-4a6c-a7bb-9a18302816af / ext4 relatime,user_xattr,errors=remount-ro 0 1 +/dev/sr0 /media/cdrom0 udf,iso9660 user,noauto 0 0 +/dev/sde /mnt/data ext4 relatime,user_xattr,errors=remount-ro 0 2 +UUID=a2b5f230-a795-4f6f-a39b-9b57686c86d5 /home btrfs relatime 0 2 +/dev/mapper/sdf_crypt /mnt/private xfs relatime 0 2 \ No newline at end of file