relpipe-data/examples-out-fstab.xml
branchv_0
changeset 244 d4f401b5f90c
parent 241 f71d300205b7
equal deleted inserted replaced
243:9c1d0c5ed599 244:d4f401b5f90c
       
     1 <stránka
       
     2 	xmlns="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/strana"
       
     3 	xmlns:m="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/makro">
       
     4 	
       
     5 	<nadpis>Formatting fstab</nadpis>
       
     6 	<perex>implementing a simple relpipe-out-fstab filter using -in-fstab, -out-nullbyte, xargs and Perl</perex>
       
     7 	<m:pořadí-příkladu>00300</m:pořadí-příkladu>
       
     8 
       
     9 	<text xmlns="http://www.w3.org/1999/xhtml">
       
    10 	
       
    11 		<p>
       
    12 			As we have seen before, we can convert <code>/etc/fstab</code> (or <code>mtab</code>)
       
    13 			to e.g. an XML or a nice and colorful table using <m:name/>.
       
    14 			But we can also convert these data back to the <code>fstab</code> format. And do it with proper indentation/padding.
       
    15 			Fstab has a simple format where values are separated by one or more whitespace characters.
       
    16 			But without proper indentation, these files look a bit obfuscated and hard to read (however, they are valid).
       
    17 		</p>
       
    18 		
       
    19 		<m:pre jazyk="text" src="examples/relpipe-out-fstab.txt"/>
       
    20 		
       
    21 		<p>
       
    22 			So let's build a pipeline that reformats the <code>fstab</code> and makes it more readable.
       
    23 		</p>
       
    24 			
       
    25 		<m:pre jazyk="bash">relpipe-in-fstab | relpipe-out-fstab &gt; reformatted-fstab.txt</m:pre>
       
    26 			
       
    27 		<p>
       
    28 			We can hack together a script called <code>relpipe-out-fstab</code> that accepts relational data and produces <code>fstab</code> data.
       
    29 			Later this will be probably implemented as a regular tool, but for now, it is just an example of a ad-hoc shell script:
       
    30 		</p>
       
    31 		
       
    32 		<m:pre jazyk="bash" src="examples/relpipe-out-fstab.sh" odkaz="ano"/>
       
    33 		
       
    34 		<p>
       
    35 			In the first part, we prepend a single record (<code>relpipe-in-cli</code>) before the data coming from STDIN (<code>cat</code>).
       
    36 			Then, we use <code>relpipe-out-nullbyte</code> to convert relational data to values separated by a null-byte.
       
    37 			This command processes only attribute values (skips relation and attribute names).
       
    38 			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>).
       
    39 			Perl does the actual formatting: adds padding and does some little tunning (merges two attributes and replaces empty values with <em>none</em>).
       
    40 		</p>
       
    41 		
       
    42 		<p>This is formatted version of the <code>fstab</code> above:</p>
       
    43 		
       
    44 		<m:pre jazyk="text" src="examples/relpipe-out-fstab.formatted.txt"/>
       
    45 		
       
    46 		<p>
       
    47 			And using following command we can verify, that the files differ only in comments and whitespace:
       
    48 		</p>
       
    49 		
       
    50 		<pre>relpipe-in-fstab | relpipe-out-fstab | diff -w /etc/fstab -</pre>
       
    51 
       
    52 		<p>
       
    53 			Another check (should print same hashes):
       
    54 		</p>
       
    55 		
       
    56 		<pre><![CDATA[relpipe-in-fstab | sha512sum 
       
    57 relpipe-in-fstab | relpipe-out-fstab | relpipe-in-fstab | sha512sum]]></pre>
       
    58 		
       
    59 		<p>
       
    60 			Regular implementation of <code>relpipe-out-fstab</code> will probably keep the comments
       
    61 			(it needs also one more attribute and small change in <code>relpipe-in-fstab</code>).
       
    62 		</p>
       
    63 		
       
    64 		<p>
       
    65 			For just mere <code>fstab</code> reformatting, this approach is a bit overengineering.
       
    66 			We could skip the whole relational thing and do just something like this:
       
    67 		</p>
       
    68 		
       
    69 		<m:pre jazyk="bash">cat /etc/fstab | grep -v '^#' | sed -E 's/\s+/\n/g' | tr \\n \\0 | xargs -0 -n7 ...</m:pre>
       
    70 		
       
    71 		<p>
       
    72 			plus prepend the comment (or do everything in Perl).
       
    73 			But this example is intended as a demostration, how we can
       
    74 			1) prepend some additional data before the data from STDIN
       
    75 			2) use <m:name/> and traditional tools like <code>xargs</code> or <code>perl</code> together.
       
    76 			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 :-)
       
    77 		</p>
       
    78 		
       
    79 		<blockquote>
       
    80 			<p>
       
    81 				There is more Unix-nature in one line of shell script than there is in ten thousand lines of C.
       
    82 				<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>
       
    83 			</p>
       
    84 		</blockquote>
       
    85 				
       
    86 	</text>
       
    87 
       
    88 </stránka>