【发布时间】:2012-08-05 00:36:18
【问题描述】:
好像 bash 和 dash 一样,从我的脚本中过滤掉任何 ASCII NUL。
$ 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 结尾的字符串),但我看不出the POSIX standard 认可这种行为的哪些地方。
当这种行为决定文件的语法正确性时,情况会变得更糟。
$ 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
\
我错过了哪些重要部分,或者 NUL 删除只是关于如何应对未指定行为的决定?
【问题讨论】:
-
我熟悉的所有 sh 实现都使用 C 字符串,它 - 以 NUL 结尾 - 本质上无法保存 NUL 值。
-
zsh 可以很好地处理所有这些测试用例。
-
如果@CharlesDuffy 的回答不够,我也会尝试询问 Unix 和 Linux。
-
我查看了 POSIX 规范,令人惊讶的是,我没有看到任何禁止使用
NUL的内容。 -
@AlanCurry:先试试
printf 'test="\000"; printf "$test" | wc -c' | zsh,然后再试试printf 'test="\000"; /usr/bin/printf "$test" | wc -c' | zsh。不太好:-)