为什么所有的NUL从我的脚本文件删除? [英] Why are all NUL removed from my script?
问题描述
好像庆典,也冲,从我的脚本过滤掉任何ASCII NUL。
It seems like bash, and also dash, filter out any ASCII NUL from my scripts.
$ printf 'test="\000a" ; echo ${#test}' | sh
1
$ printf 'test="\001a" ; echo ${#test}' | sh
2
$ printf 'ec\000ho test' | sh
test
$ # (Same for bash)
虽然我同意使用NUL是一个糟糕的坏主意(例如参数传递给程序使用NUL结尾的字符串的作品),我没有看到这种行为是通过的 POSIX标准。
它变得更加恶劣时,这种行为是决定该文件的语法的正确性。
It gets even worse when this behaviour is deciding on the syntactical correctness of the file.
$ printf 'echo "\\\000"' | sh
sh: Syntax error: Unterminated quoted string
$ printf 'echo "\\\000"' | bash
bash: line 1: unexpected EOF while looking for matching `"'
bash: line 2: syntax error: unexpected end of file
$ printf 'echo "\\\134"' | sh
\
我错过了什么重要组成部分,或者就是空去除只能在如何与不确定的行为应付的决定?
What essential part did I miss, or is the NUL-removal only a decision on how to cope with unspecified behaviour?
推荐答案
在 SH 规定:
输入文件应是一个文本文件,所不同的是线的长度应是无限的。如果输入的文件是空的或仅由空白行或意见,或两者兼而有之,SH应以零退出状态退出。
术语文本文件,在这里为第3.395 的定义是:
The term "text file" is defined in section 3.395 here as:
包含组织成零个或多个字符行的文件。该行不包含NULL字符无人能超过{LINE_MAX}字节的长度,包括在<&换行符GT;字符。虽然POSIX.1-2008不文本文件和二进制文件(见ISO C标准)进行区分,许多公用事业上的文本文件进行操作时只产生predictable或有意义的输出。有这样的限制始终指定他们的标准输入文本文件,或输入标准实用程序FILES部分
如果输入不是文本文件(它是不如果它包含零字节),则该行为既不是有意义也不predictable
If the input is not a text file (which it is not if it contains zero bytes), the behavior is neither meaningful nor predictable.
这篇关于为什么所有的NUL从我的脚本文件删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!