87 Record count: 1]]></pre> |
87 Record count: 1]]></pre> |
88 |
88 |
89 <h3>relpipe-in-cli: STDIN</h3> |
89 <h3>relpipe-in-cli: STDIN</h3> |
90 |
90 |
91 <p> |
91 <p> |
92 The number of CLI arguments is limited and their are passed at once to the process. |
92 The number of <abbr title="Command-line interface">CLI</abbr> arguments is limited and they are passed at once to the process. |
93 So there is option to pass the values from STDIN instead of CLI arguments. |
93 So there is option to pass the values from STDIN instead of CLI arguments. |
94 Values on STDIN are expected to be separated by the null-byte. |
94 Values on STDIN are expected to be separated by the null-byte. |
95 We can generate such data e.g. using <code>echo</code> and <code>tr</code> (or using <code>printf</code> or other commands): |
95 We can generate such data e.g. using <code>echo</code> and <code>tr</code> (or using <code>printf</code> or other commands): |
96 </p> |
96 </p> |
97 |
97 |
370 There is more Unix-nature in one line of shell script than there is in ten thousand lines of C. |
370 There is more Unix-nature in one line of shell script than there is in ten thousand lines of C. |
371 <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> |
371 <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> |
372 </p> |
372 </p> |
373 </blockquote> |
373 </blockquote> |
374 |
374 |
|
375 <h2>Writing an output filter in Bash</h2> |
|
376 |
|
377 <p> |
|
378 In previous example we created and output filter in Perl. |
|
379 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 <em>multi-liner</em> in this case). |
|
380 But we can write such output filter in pure Bash without <code>xargs</code> and <code>perl</code>. |
|
381 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>). |
|
382 </p> |
|
383 |
|
384 <p> |
|
385 We will define a function that will help us with reading the <code>\0</code>-separated values and putting them into shell variables: |
|
386 </p> |
|
387 |
|
388 <m:pre jazyk="bash"><![CDATA[read_nullbyte() { for v in "$@"; do export "$v"; read -r -d '' "$v"; done }]]></m:pre> |
|
389 |
|
390 <!-- |
|
391 This version will not require the last \0: |
|
392 read_zero() { for v in "$@"; do export "$v"; read -r -d '' "$v" || [ ! -z "${!v}" ]; done } |
|
393 at least in case when the last value is not missing. |
|
394 Other values might be null/missing: \0\0 is OK. |
|
395 --> |
|
396 |
|
397 <p> |
|
398 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). |
|
399 But it is just a single line function, so not a big deal. |
|
400 </p> |
|
401 |
|
402 <p> |
|
403 And then we just read the values, put them in shell variables and process them in a cycle in a shell block of code: |
|
404 </p> |
|
405 |
|
406 <m:pre jazyk="bash"><![CDATA[relpipe-in-fstab \ |
|
407 | relpipe-out-nullbyte \ |
|
408 | while read_nullbyte scheme device mount_point fs_type options dump pass; do |
|
409 echo "Device ${scheme:+$scheme=}$device is mounted" \ |
|
410 "at $mount_point and contains $fs_type."; |
|
411 done]]></m:pre> |
|
412 |
|
413 <p> |
|
414 Which will print: |
|
415 </p> |
|
416 |
|
417 <pre><![CDATA[Device UUID=29758270-fd25-4a6c-a7bb-9a18302816af is mounted at / and contains ext4. |
|
418 Device /dev/sr0 is mounted at /media/cdrom0 and contains udf,iso9660. |
|
419 Device /dev/sde is mounted at /mnt/data and contains ext4. |
|
420 Device UUID=a2b5f230-a795-4f6f-a39b-9b57686c86d5 is mounted at /home and contains btrfs. |
|
421 Device /dev/mapper/sdf_crypt is mounted at /mnt/private and contains xfs.]]></pre> |
|
422 |
|
423 <p> |
|
424 Using this method, we can convert any single relation to any format (preferably text one, but <code>printf</code> can produce also binary data). |
|
425 </p> |
|
426 |
375 <h2>Rename VG in /etc/fstab using relpipe-tr-sed</h2> |
427 <h2>Rename VG in /etc/fstab using relpipe-tr-sed</h2> |
376 |
428 |
377 <p> |
429 <p> |
378 Assume that we have an <code>/etc/fstab</code> with many lines defining the mount-points (directories) of particular devices (disks) and we are using LVM. |
430 Assume that we have an <code>/etc/fstab</code> with many lines defining the mount-points (directories) of particular devices (disks) and we are using LVM. |
379 If we rename a volume group (VG), we have to change all of them. The lines look like this one: |
431 If we rename a volume group (VG), we have to change all of them. The lines look like this one: |
506 The <code>relpipe-tr-cut</code> tool has similar syntax to its <em>grep</em> and <em>sed</em> siblings and also uses the power of regular expressions. |
558 The <code>relpipe-tr-cut</code> tool has similar syntax to its <em>grep</em> and <em>sed</em> siblings and also uses the power of regular expressions. |
507 In this case it modifies on-the-fly the <code>fstab</code> relation and drops all its attributes except the <code>mount_point</code> one. |
559 In this case it modifies on-the-fly the <code>fstab</code> relation and drops all its attributes except the <code>mount_point</code> one. |
508 </p> |
560 </p> |
509 |
561 |
510 <p> |
562 <p> |
511 Then we pass the data to the Bash while cycle. |
563 Then we pass the data to the Bash <code>while</code> cycle. |
512 In such simple scenario (just <code>echo</code>), we could use <code>xargs</code> as in examples before, |
564 In such simple scenario (just <code>echo</code>), we could use <code>xargs</code> as in examples above, |
513 but in this syntax, we can write whole block of shell commands for each record/value and do more complex actions with them. |
565 but in this syntax, we can write whole block of shell commands for each record/value and do more complex actions with them. |
514 </p> |
566 </p> |
515 |
567 |
516 <h2>More projections with relpipe-tr-cut</h2> |
568 <h2>More projections with relpipe-tr-cut</h2> |
517 |
569 |