标签:android bash “source insight"
开头写了简单的脚本,后来发现有的目录是带空格的,这样就没法处理了。于是查资料做调整.
shell脚本的内容是:
#!/bin/bash
test=$1
[ "$test" != "" ] && action=echo
l="c h cpp java"
#owing to folder with space, so the find ... is a little complex
if [ "$action" != "echo" ]; then
for i in $l; do
$action find . -name "*.$i" -print0|xargs -0 -i sed -i "s:// TODO:// TODO:g" {}
done
else
echo find . -name "*.$i" -print0\|xargs -0 -i sed -i "s:// TODO:// TODO:g" {}
fi
原因在这里:
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; ls -l
total 0
-rw-r--r-- 1 root root 0 2010-08-02 18:12 file 1.log
-rw-r--r-- 1 root root 0 2010-08-02 18:12 file 2.log
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; find -name ‘*.log‘
./file 1.log
./file 2.log
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; find -name ‘*.log‘ | xargs rm
rm: cannot remove `./file‘: No such file or directory
rm: cannot remove `1.log‘: No such file or directory
rm: cannot remove `./file‘: No such file or directory
rm: cannot remove `2.log‘: No such file or directory
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; bye
原因其实很简单, xargs 默认是以空白字符 (空格, TAB, 换行符) 来分割记录的, 因此文件名 ./file 1.log 被解释成了两个记录./file 和 1.log, 不幸的是 rm 找不到这两个文件.
为了解决此类问题, 聪明的人想出了一个办法, 让 find 在打印出一个文件名之后接着输出一个 NULL 字符 (‘\0‘) 而不是换行符, 然后再告诉 xargs 也用 NULL 字符来作为记录的分隔符. 这就是 find 的 -print0 和 xargs 的 -0 的来历吧.
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; ls -l
total 0
-rw-r--r-- 1 root root 0 2010-08-02 18:12 file 1.log
-rw-r--r-- 1 root root 0 2010-08-02 18:12 file 2.log
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; find -name ‘*.log‘ -print0 | hd
0 1 2 3 4 5 6 7 8 9 A B C D E F |0123456789ABCDEF|
--------+--+--+--+--+---+--+--+--+---+--+--+--+---+--+--+--+--+----------------|
00000000: 2e 2f 66 69 6c 65 20 31 2e 6c 6f 67 00 2e 2f 66 |./file 1.log../f|
00000010: 69 6c 65 20 32 2e 6c 6f 67 00 |ile 2.log. |
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; find -name ‘*.log‘ -print0 | xargs -0 rm
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; find -name ‘*.log‘
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; bye
你可能要问了, 为什么要选 ‘\0‘ 而不是其他字符做分隔符呢? 这个也容易理解: 一般的编程语言中都用 ‘\0‘ 来作为字符串的结束标志, 文件的路径名中不可能包含 ‘\0‘ 字符.
为了source insight显示android源码正确的“// TODO“做调整
标签:android bash “source insight"
原文地址:http://8202061.blog.51cto.com/8192061/1674195