help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] Readig multiple null-separated values


From: František Kučera
Subject: [Help-bash] Readig multiple null-separated values
Date: Sat, 27 Jul 2019 10:58:55 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2

Hello,

in my Bash pipelines I sometimes pass data separated by null byte \0 and it is not only lists (like in find -print0) but matrices/tables/relations with fixed number of columns/attributes. The \0 is used to both separate values and records. The advantage is that the only forbidden character in values is the \0 and there are no collisions with text separators like ,;:\t\n etc. and no need for escaping or quoting. So it is a safe and simple way to pass structured data between processes.

To read a record in a cycle I wanted to use the read command with proper parameters first, but it seemed impossible and I ended with a custom function which work as I need:

read_nullbyte() { for v in "$@"; do export "$v"; read -r -d '' "$v"; done }

Usage example:

printf 'a\0aaa\0b\0bbb\0c\0ccc' | while read_nullbyte a1 a2; do echo a1=$a1 a2=$a2; done

(usually it is much more complicated and data are generated by some program/script, not a single printf call)

My question is:

1) Is there a simpler solution than my read_nullbyte() function?

2) Do you consider it generic and useful enough to make it built-in part of GNU Bash?

Franta




reply via email to

[Prev in Thread] Current Thread [Next in Thread]