将以null结尾的字符串传递给bash函数
我的导出功能:将以null结尾的字符串传递给bash函数,bash,Bash,我的导出功能: myfunc(){ # Some operation with null terminated string # If I use $1, is it guaranteed that it is a null terminated string? } 在本例中,请考虑 find . -type -f -print0 | while read -rd '' line do myfunc "$line" done 如果您使用的是print0,那么请确保除了在read中使用-d'之
myfunc(){
# Some operation with null terminated string
# If I use $1, is it guaranteed that it is a null terminated string?
}
在本例中,请考虑
find . -type -f -print0 | while read -rd '' line
do
myfunc "$line"
done
如果您使用的是
print0
,那么请确保除了在read
中使用-d'
之外,还使用空的IFS=
,以将空值作为分隔符:
while IFS= read -r -d '' line; do
myfunc "$line"
done < <(find . -type f -print0)
而IFS=read-r-d''行;做
myfunc“$line”
完成<如果您使用“$1”
,那么我认为是这样的。Bash字符串总是以NUL结尾,这意味着它们不能包含NUL字符。也许你不是这个意思,谢谢。。为什么IFS=
除了d'
?@mona_-sax,因为除非IFS
被清除,否则后面的空白可以被read
@anubhava剪除,….虽然只有一个参数传递给read
并且没有-a
,但实际上并没有进行拆分。。。