In previous example we created an 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 a 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:
read_nullbyte() { local IFS=; for v in "$@"; do export "$v"; read -r -d '' "$v"; done }
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:
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
Which will print:
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.
Using this method, we can convert any single relation to any format (preferably some text one, but printf
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).
Relational pipes, open standard and free software © 2018-2022 GlobalCode