read nul delimited fields

♀尐吖头ヾ 提交于 2019-12-07 08:41:02

问题


Given this file

printf 'alpha\0bravo\0charlie' > delta.txt

I would like to read the fields into separate variables. The reason I am using a null separator is because the fields will contain file paths, which can contain any character except null. I tried these commands:

IFS= read mike november oscar < delta.txt
IFS=$'\0' read mike november oscar < delta.txt

However the fields are not being properly split

$ echo $mike
alphabravocharlie

回答1:


The assignment IFS=$'\0' doesn't make the null character a delimiter, since Bash variables cannot hold null characters. IFS=$'\0' is equivalent to IFS=, which you can verify by doing:

bash-4.3$ IFS=$'\0'
bash-4.3$ echo ${#IFS}
0

And IFS= by definition means no word splitting at all (see Bash Reference Manual).

What you can do is read the null delimited items one by one using the -d option of the read builtin. According to linked documentation,

-d delim

The first character of delim is used to terminate the input line, rather than newline.

We can use a null string for delim to get desired behavior.

Example (I took the liberty to add whitespace to your example to demonstrate how it achieves what to want — no splitting on whitespace):

bash-4.3$ printf 'alpha with whitespace\0bravo with whitespace\0charlie with whitespace' > delta.txt
bash-4.3$ { read -r -d '' mike; IFS= read -r -d '' november; IFS= read -r -d '' oscar; echo $mike; echo $november; echo $oscar; } < delta.txt
alpha with whitespace
bravo with whitespace
charlie with whitespace

I'm also using the -r option to preserve backslashes in the input file. Of course you can replace < delta.txt with a cat delta.txt | at the beginning.

I know reading one by one is annoying, but I can't think of anything better.




回答2:


As a workaround, I created this function

function read_loop {
  while [ "$#" -gt 0 ]
  do
    read -d '' "$1"
    shift
  done
}

Example use

read-nul mike november oscar < delta.txt


来源:https://stackoverflow.com/questions/29885176/read-nul-delimited-fields

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!