您的脚本可以接受STDIN
的输入吗?也许使用read
。
如果你不能这样做,你可以编码你的空值,并逃避你的编码。
E.G. 48656c6c6f0020576f726c64可以编码为48656c6c6f200102020576f726c64
这反过来,如果双方都同意2020 = 20,2001年将再次解码= 00
更新我觉得编码是你必须做的,因为我尝试使用read
什么,结果有点太难了。可能还有另一种选择,但我还没有看到它。
这里是我的脚本和两个测试流程:
dlamblin$ cat test.sh
echo "reading two lines of input, first line is length of second."
read len
read ans
echo "C string length of second line is:" ${#ans}
for ((c=0; c<$len; c++))
do
/bin/echo -n "${ans:$c:1},"
done
echo ' '
exit
dlamblin$ echo -e '12\0012Hello \0040World' | sh test.sh
reading two lines of input, first line is length of second.
C string length of second line is: 12
H,e,l,l,o, , ,W,o,r,l,d,
dlamblin$ echo -e '12\0012Hello \0000World' | sh test.sh
reading two lines of input, first line is length of second.
C string length of second line is: 5
H,e,l,l,o,,,,,,,,
#Octals \0000 \0012 \0040 are NUL NL and SP respectively
哦,我假设'fade.sh'是一个真正的'sh'脚本。因此,我用'sh'测试。显然,ruby可以更好地读取'stdin'中的字节。 – dlamblin 2009-10-18 19:42:43