author | František Kučera <franta-hg@frantovo.cz> |
Mon, 21 Feb 2022 01:21:22 +0100 | |
branch | v_0 |
changeset 330 | 70e7eb578cfa |
parent 290 | e73765513aec |
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>Writing an output filter in Bash</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>processing relational data in GNU Bash or some other shell</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>00600</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"> |
176
46042297e0d6
relpipe-tr-validator: specification and examples
František Kučera <franta-hg@frantovo.cz>
parents:
140
diff
changeset
|
10 |
|
214
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
11 |
<p> |
216 | 12 |
In previous example we created an output filter in Perl. |
13 |
We converted a relation to values separated by <code>\0</code> and then passed it through <code>xargs</code> to a perl <em>one-liner</em> (or a <em>multi-liner</em> in this case). |
|
214
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
14 |
But we can write such output filter in pure Bash without <code>xargs</code> and <code>perl</code>. |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
15 |
Of course, it is still limited to a single relation (or it can process multiple relations of same type and do something like implicit <code>UNION ALL</code>). |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
16 |
</p> |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
17 |
|
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
18 |
<p> |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
19 |
We will define a function that will help us with reading the <code>\0</code>-separated values and putting them into shell variables: |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
20 |
</p> |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
21 |
|
290
e73765513aec
fix read_nullbyte() to avoid trimming whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
22 |
<m:pre jazyk="bash"><![CDATA[read_nullbyte() { local IFS=; for v in "$@"; do export "$v"; read -r -d '' "$v"; done }]]></m:pre> |
214
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
23 |
|
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
24 |
<!-- |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
25 |
This version will not require the last \0: |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
26 |
read_zero() { for v in "$@"; do export "$v"; read -r -d '' "$v" || [ ! -z "${!v}" ]; done } |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
27 |
at least in case when the last value is not missing. |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
28 |
Other values might be null/missing: \0\0 is OK. |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
29 |
--> |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
30 |
|
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
31 |
<p> |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
32 |
Currently, there is no known way how to do this without a custom function (just with <code>read</code> built-in command of Bash and its parameters). |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
33 |
But it is just a single line function, so not a big deal. |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
34 |
</p> |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
35 |
|
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
36 |
<p> |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
37 |
And then we just read the values, put them in shell variables and process them in a cycle in a shell block of code: |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
38 |
</p> |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
39 |
|
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
40 |
<m:pre jazyk="bash"><![CDATA[relpipe-in-fstab \ |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
41 |
| relpipe-out-nullbyte \ |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
42 |
| while read_nullbyte scheme device mount_point fs_type options dump pass; do |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
43 |
echo "Device ${scheme:+$scheme=}$device is mounted" \ |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
44 |
"at $mount_point and contains $fs_type."; |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
45 |
done]]></m:pre> |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
46 |
|
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
47 |
<p> |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
48 |
Which will print: |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
49 |
</p> |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
50 |
|
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
51 |
<pre><![CDATA[Device UUID=29758270-fd25-4a6c-a7bb-9a18302816af is mounted at / and contains ext4. |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
52 |
Device /dev/sr0 is mounted at /media/cdrom0 and contains udf,iso9660. |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
53 |
Device /dev/sde is mounted at /mnt/data and contains ext4. |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
54 |
Device UUID=a2b5f230-a795-4f6f-a39b-9b57686c86d5 is mounted at /home and contains btrfs. |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
55 |
Device /dev/mapper/sdf_crypt is mounted at /mnt/private and contains xfs.]]></pre> |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
56 |
|
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
57 |
<p> |
215
c7e88edaedc5
examples: Writing an output filter in Bash: comment
František Kučera <franta-hg@frantovo.cz>
parents:
214
diff
changeset
|
58 |
Using this method, we can convert any single relation to any format (preferably some text one, but <code>printf</code> can produce also binary data). |
c7e88edaedc5
examples: Writing an output filter in Bash: comment
František Kučera <franta-hg@frantovo.cz>
parents:
214
diff
changeset
|
59 |
This is good for ad-hoc conversions and single-relation data. |
c7e88edaedc5
examples: Writing an output filter in Bash: comment
František Kučera <franta-hg@frantovo.cz>
parents:
214
diff
changeset
|
60 |
More powerful tools can be written in C++ and other languages like Java, Python, Guile etc. (when particular libraries are available). |
214
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
61 |
</p> |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
62 |
|
87
25dec6931f18
Lepší odsazení, tabulátory.
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
63 |
</text> |
4
1bb39595a51c
genrování hlavní nabídky #1
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
64 |
|
1 | 65 |
</stránka> |