relpipe-data/examples-out-bash.xml
branchv_0
changeset 244 d4f401b5f90c
parent 241 f71d300205b7
child 290 e73765513aec
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/relpipe-data/examples-out-bash.xml	Tue Feb 05 19:18:28 2019 +0100
@@ -0,0 +1,65 @@
+<stránka
+	xmlns="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/strana"
+	xmlns:m="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/makro">
+	
+	<nadpis>Writing an output filter in Bash</nadpis>
+	<perex>processing relational data in GNU Bash or some other shell</perex>
+	<m:pořadí-příkladu>00600</m:pořadí-příkladu>
+
+	<text xmlns="http://www.w3.org/1999/xhtml">
+		
+		<p>
+			In previous example we created an output filter in Perl. 
+			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).
+			But we can write such output filter in pure Bash without <code>xargs</code> and <code>perl</code>.
+			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>).
+		</p>
+		
+		<p>
+			We will define a function that will help us with reading the <code>\0</code>-separated values and putting them into shell variables:
+		</p>
+		
+		<m:pre jazyk="bash"><![CDATA[read_nullbyte() { for v in "$@"; do export "$v"; read -r -d '' "$v"; done }]]></m:pre>
+		
+		<!--
+			This version will not require the last \0:
+				read_zero() { for v in "$@"; do export "$v"; read -r -d '' "$v" || [ ! -z "${!v}" ]; done }
+			at least in case when the last value is not missing.
+			Other values might be null/missing: \0\0 is OK.
+		-->
+		
+		<p>
+			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).
+			But it is just a single line function, so not a big deal.
+		</p>
+		
+		<p>
+			And then we just read the values, put them in shell variables and process them in a cycle in a shell block of code:
+		</p>
+		
+		<m:pre jazyk="bash"><![CDATA[relpipe-in-fstab \
+	| relpipe-out-nullbyte \
+	| while read_nullbyte scheme device mount_point fs_type options dump pass; do
+		echo "Device ${scheme:+$scheme=}$device is mounted" \
+		     "at $mount_point and contains $fs_type.";
+	done]]></m:pre>
+	
+		<p>
+			Which will print:
+		</p>
+		
+		<pre><![CDATA[Device UUID=29758270-fd25-4a6c-a7bb-9a18302816af is mounted at / and contains ext4.
+Device /dev/sr0 is mounted at /media/cdrom0 and contains udf,iso9660.
+Device /dev/sde is mounted at /mnt/data and contains ext4.
+Device UUID=a2b5f230-a795-4f6f-a39b-9b57686c86d5 is mounted at /home and contains btrfs.
+Device /dev/mapper/sdf_crypt is mounted at /mnt/private and contains xfs.]]></pre>
+
+		<p>
+			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).
+			This is good for ad-hoc conversions and single-relation data.
+			More powerful tools can be written in C++ and other languages like Java, Python, Guile etc. (when particular libraries are available).
+		</p>
+		
+	</text>
+
+</stránka>