# HG changeset patch # User František Kučera # Date 1544963754 -3600 # Node ID 63f31bf2697f0ed3bb940404ddd3071f097e1492 # Parent cbf25a63a43fdda831d216514a0d2ab4578ab292 examples: Writing an output filter in Bash diff -r cbf25a63a43f -r 63f31bf2697f relpipe-data/examples.xml --- a/relpipe-data/examples.xml Thu Dec 13 14:14:17 2018 +0100 +++ b/relpipe-data/examples.xml Sun Dec 16 13:35:54 2018 +0100 @@ -89,7 +89,7 @@

relpipe-in-cli: STDIN

- The number of CLI arguments is limited and their are passed at once to the process. + The number of CLI arguments is limited and they are passed at once to the process. So there is option to pass the values from STDIN instead of CLI arguments. Values on STDIN are expected to be separated by the null-byte. We can generate such data e.g. using echo and tr (or using printf or other commands): @@ -372,6 +372,58 @@

+

Writing an output filter in Bash

+ +

+ In previous example we created and output filter in Perl. + We converted a relation to values separated by \0 and then passed it through xargs to a perl one-liner (or multi-liner in this case). + But we can write such output filter in pure Bash without xargs and perl. + Of course, it is still limited to a single relation (or it can process multiple relations of same type and do something like implicit UNION ALL). +

+ +

+ We will define a function that will help us with reading the \0-separated values and putting them into shell variables: +

+ + + + + +

+ Currently, there is no known way how to do this without a custom function (just with read built-in command of Bash and its parameters). + But it is just a single line function, so not a big deal. +

+ +

+ And then we just read the values, put them in shell variables and process them in a cycle in a shell block of code: +

+ + + +

+ Which will print: +

+ +
+ +

+ Using this method, we can convert any single relation to any format (preferably text one, but printf can produce also binary data). +

+

Rename VG in /etc/fstab using relpipe-tr-sed

@@ -508,8 +560,8 @@

- Then we pass the data to the Bash while cycle. - In such simple scenario (just echo), we could use xargs as in examples before, + Then we pass the data to the Bash while cycle. + In such simple scenario (just echo), we could use xargs as in examples above, but in this syntax, we can write whole block of shell commands for each record/value and do more complex actions with them.