relpipe-data/examples.xml
branchv_0
changeset 180 d342de2e09a4
parent 176 46042297e0d6
child 184 9ad44348e6aa
equal deleted inserted replaced
179:029dc972f1f2 180:d342de2e09a4
    36 		
    36 		
    37 		<p>
    37 		<p>
    38 			If an error is found, it is reported on STDERR. So just omit the <code>&amp;</code> in order to see the error message.
    38 			If an error is found, it is reported on STDERR. So just omit the <code>&amp;</code> in order to see the error message.
    39 		</p>
    39 		</p>
    40 		
    40 		
       
    41 		
       
    42 		<h3>/etc/fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl</h3>
       
    43 		
       
    44 		<p>
       
    45 			As we have seen before, we can convert <code>/etc/fstab</code> (or <code>mtab</code>)
       
    46 			to e.g. an XML or a nice and colorful table using <m:name/>.
       
    47 			But we can also convert these data back to the <code>fstab</code> format. And do it with proper indentation/padding.
       
    48 			Fstab has a simple format where values are separated by one or more whitespace characters.
       
    49 			But without proper indentation, these files look a bit obfuscated and hard to read (however, they are valid).
       
    50 		</p>
       
    51 		
       
    52 		<m:pre jazyk="text" src="examples/relpipe-out-fstab.txt"/>
       
    53 		
       
    54 		<p>
       
    55 			So let's build a pipeline that reformats the <code>fstab</code> and makes it more readable.
       
    56 		</p>
       
    57 			
       
    58 		<m:pre jazyk="bash">relpipe-in-fstab | relpipe-out-fstab &gt; reformatted-fstab.txt</m:pre>
       
    59 			
       
    60 		<p>
       
    61 			We can hack together a script called <code>relpipe-out-fstab</code> that accepts relational data and produces <code>fstab</code> data.
       
    62 			Later this will be probably implemented as a regular tool, but for now, it is just an example of a ad-hoc shell script:
       
    63 		</p>
       
    64 		
       
    65 		<m:pre jazyk="bash" src="examples/relpipe-out-fstab.sh" odkaz="ano"/>
       
    66 		
       
    67 		<p>
       
    68 			In the first part, we prepend a single record (<code>relpipe-in-cli</code>) before the data coming from STDIN (<code>cat</code>).
       
    69 			Then, we use <code>relpipe-out-nullbyte</code> to convert relational data to values separated by a null-byte.
       
    70 			This command processes only attribute values (relation and attribute names are skipped).
       
    71 			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>).
       
    72 			Perl does the actual formatting: adds padding and does some little tunning (merges two attributes and replaces empty values with <em>none</em>).
       
    73 		</p>
       
    74 		
       
    75 		<p>This is formatted version of the <code>fstab</code> above:</p>
       
    76 		
       
    77 		<m:pre jazyk="text" src="examples/relpipe-out-fstab.formatted.txt"/>
       
    78 		
       
    79 		<p>
       
    80 			And using following command we can verify, that the files differ only in comments and whitespace:
       
    81 		</p>
       
    82 		
       
    83 		<pre>relpipe-in-fstab | relpipe-out-fstab | diff -w /etc/fstab -</pre>
       
    84 		
       
    85 		<p>
       
    86 			Regular implementation of <code>relpipe-out-fstab</code> will probably keep the comments
       
    87 			(it needs also one more attribute and small change in <code>relpipe-in-fstab</code>).
       
    88 		</p>
       
    89 		
       
    90 		<p>
       
    91 			For just mere <code>fstab</code> reformatting, this approach is a bit overengineering.
       
    92 			Wo could skip the whole relational thing and do just something like this:
       
    93 		</p>
       
    94 		
       
    95 		<m:pre jazyk="bash">cat /etc/fstab | grep -v '^#' | sed -E 's/\s+/\n/g' | tr \\n \\0 | xargs -0 -n7 ...</m:pre>
       
    96 		
       
    97 		<p>
       
    98 			plus prepend the comment (or do everything in Perl).
       
    99 			But this example is intended as a demostration, how we can
       
   100 			1) prepend some additional data before the data from STDIN
       
   101 			2) use <m:name/> and traditional tools like <code>xargs</code> or <code>perl</code> together.
       
   102 			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 :-)
       
   103 		</p>
       
   104 		
       
   105 		<blockquote>
       
   106 			<p>
       
   107 				There is more Unix-nature in one line of shell script than there is in ten thousand lines of C.
       
   108 				<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>
       
   109 			</p>
       
   110 		</blockquote>
       
   111 		
    41 	</text>
   112 	</text>
    42 
   113 
    43 </stránka>
   114 </stránka>