author | František Kučera <franta-hg@frantovo.cz> |
Mon, 21 Feb 2022 00:43:11 +0100 | |
branch | v_0 |
changeset 329 | 5bc2bb8b7946 |
parent 244 | d4f401b5f90c |
permissions | -rw-r--r-- |
23
0d2729ed16ed
zkouška interního odkazu
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
1 |
<stránka |
0d2729ed16ed
zkouška interního odkazu
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
2 |
xmlns="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/strana" |
0d2729ed16ed
zkouška interního odkazu
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
3 |
xmlns:m="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/makro"> |
0d2729ed16ed
zkouška interního odkazu
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
4 |
|
244
d4f401b5f90c
examples: move each example to a separate page + add generated list of examples
František Kučera <franta-hg@frantovo.cz>
parents:
241
diff
changeset
|
5 |
<nadpis>Formatting fstab</nadpis> |
d4f401b5f90c
examples: move each example to a separate page + add generated list of examples
František Kučera <franta-hg@frantovo.cz>
parents:
241
diff
changeset
|
6 |
<perex>implementing a simple relpipe-out-fstab filter using -in-fstab, -out-nullbyte, xargs and Perl</perex> |
d4f401b5f90c
examples: move each example to a separate page + add generated list of examples
František Kučera <franta-hg@frantovo.cz>
parents:
241
diff
changeset
|
7 |
<m:pořadí-příkladu>00300</m:pořadí-příkladu> |
4
1bb39595a51c
genrování hlavní nabídky #1
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
8 |
|
2
ab9099ff88fa
vkládání zápatí, jmenné prostory, saxon
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
9 |
<text xmlns="http://www.w3.org/1999/xhtml"> |
185
6cd2e54d90ea
examples: relpipe-in-cli
František Kučera <franta-hg@frantovo.cz>
parents:
184
diff
changeset
|
10 |
|
6cd2e54d90ea
examples: relpipe-in-cli
František Kučera <franta-hg@frantovo.cz>
parents:
184
diff
changeset
|
11 |
<p> |
180
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
12 |
As we have seen before, we can convert <code>/etc/fstab</code> (or <code>mtab</code>) |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
13 |
to e.g. an XML or a nice and colorful table using <m:name/>. |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
14 |
But we can also convert these data back to the <code>fstab</code> format. And do it with proper indentation/padding. |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
15 |
Fstab has a simple format where values are separated by one or more whitespace characters. |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
16 |
But without proper indentation, these files look a bit obfuscated and hard to read (however, they are valid). |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
17 |
</p> |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
18 |
|
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
19 |
<m:pre jazyk="text" src="examples/relpipe-out-fstab.txt"/> |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
20 |
|
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
21 |
<p> |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
22 |
So let's build a pipeline that reformats the <code>fstab</code> and makes it more readable. |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
23 |
</p> |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
24 |
|
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
25 |
<m:pre jazyk="bash">relpipe-in-fstab | relpipe-out-fstab > reformatted-fstab.txt</m:pre> |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
26 |
|
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
27 |
<p> |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
28 |
We can hack together a script called <code>relpipe-out-fstab</code> that accepts relational data and produces <code>fstab</code> data. |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
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: |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
30 |
</p> |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
31 |
|
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
32 |
<m:pre jazyk="bash" src="examples/relpipe-out-fstab.sh" odkaz="ano"/> |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
33 |
|
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
34 |
<p> |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
35 |
In the first part, we prepend a single record (<code>relpipe-in-cli</code>) before the data coming from STDIN (<code>cat</code>). |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
36 |
Then, we use <code>relpipe-out-nullbyte</code> to convert relational data to values separated by a null-byte. |
184 | 37 |
This command processes only attribute values (skips relation and attribute names). |
180
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
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>). |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
39 |
Perl does the actual formatting: adds padding and does some little tunning (merges two attributes and replaces empty values with <em>none</em>). |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
40 |
</p> |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
41 |
|
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
42 |
<p>This is formatted version of the <code>fstab</code> above:</p> |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
43 |
|
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
44 |
<m:pre jazyk="text" src="examples/relpipe-out-fstab.formatted.txt"/> |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
45 |
|
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
46 |
<p> |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
47 |
And using following command we can verify, that the files differ only in comments and whitespace: |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
48 |
</p> |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
49 |
|
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
50 |
<pre>relpipe-in-fstab | relpipe-out-fstab | diff -w /etc/fstab -</pre> |
195
65138c5d5a7c
examples: fstab - another check
František Kučera <franta-hg@frantovo.cz>
parents:
187
diff
changeset
|
51 |
|
65138c5d5a7c
examples: fstab - another check
František Kučera <franta-hg@frantovo.cz>
parents:
187
diff
changeset
|
52 |
<p> |
65138c5d5a7c
examples: fstab - another check
František Kučera <franta-hg@frantovo.cz>
parents:
187
diff
changeset
|
53 |
Another check (should print same hashes): |
65138c5d5a7c
examples: fstab - another check
František Kučera <franta-hg@frantovo.cz>
parents:
187
diff
changeset
|
54 |
</p> |
65138c5d5a7c
examples: fstab - another check
František Kučera <franta-hg@frantovo.cz>
parents:
187
diff
changeset
|
55 |
|
65138c5d5a7c
examples: fstab - another check
František Kučera <franta-hg@frantovo.cz>
parents:
187
diff
changeset
|
56 |
<pre><![CDATA[relpipe-in-fstab | sha512sum |
65138c5d5a7c
examples: fstab - another check
František Kučera <franta-hg@frantovo.cz>
parents:
187
diff
changeset
|
57 |
relpipe-in-fstab | relpipe-out-fstab | relpipe-in-fstab | sha512sum]]></pre> |
180
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
58 |
|
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
59 |
<p> |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
60 |
Regular implementation of <code>relpipe-out-fstab</code> will probably keep the comments |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
61 |
(it needs also one more attribute and small change in <code>relpipe-in-fstab</code>). |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
62 |
</p> |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
63 |
|
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
64 |
<p> |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
65 |
For just mere <code>fstab</code> reformatting, this approach is a bit overengineering. |
184 | 66 |
We could skip the whole relational thing and do just something like this: |
180
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
67 |
</p> |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
68 |
|
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
69 |
<m:pre jazyk="bash">cat /etc/fstab | grep -v '^#' | sed -E 's/\s+/\n/g' | tr \\n \\0 | xargs -0 -n7 ...</m:pre> |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
70 |
|
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
71 |
<p> |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
72 |
plus prepend the comment (or do everything in Perl). |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
73 |
But this example is intended as a demostration, how we can |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
74 |
1) prepend some additional data before the data from STDIN |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
75 |
2) use <m:name/> and traditional tools like <code>xargs</code> or <code>perl</code> together. |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
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 :-) |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
77 |
</p> |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
78 |
|
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
79 |
<blockquote> |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
80 |
<p> |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
81 |
There is more Unix-nature in one line of shell script than there is in ten thousand lines of C. |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
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> |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
83 |
</p> |
d342de2e09a4
examples: fstab formatting using -in-fstab, -out-nullbyte, xargs and Perl
František Kučera <franta-hg@frantovo.cz>
parents:
176
diff
changeset
|
84 |
</blockquote> |
208
91a210d3b66a
examples: relpipe-tr-grep
František Kučera <franta-hg@frantovo.cz>
parents:
207
diff
changeset
|
85 |
|
87
25dec6931f18
Lepší odsazení, tabulátory.
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
86 |
</text> |
4
1bb39595a51c
genrování hlavní nabídky #1
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
87 |
|
1 | 88 |
</stránka> |