2011年6月28日 星期二

xargs - work with cp - specify the argument sequence

用 xargs 可以用來把前一個 command 的輸出轉到另一個 command 的 argument。
但是還有一些細節,例如 作為下一個 command 的第一還是第二個 argument ?

可以用 -I option 來作。

-I option 好像是用來賦予 xargs 傳遞的變數.
以 "把 RefBase.cpp copy 到 test" 為例
$ find . -name RefBase.cpp | xargs -I src cp src ~/test
期中的 -I src 就指定 find. 的結果用 'src'來代表。
所以 cp src ~/test 中的 src,就會被 find 的結果待換掉。




ref : http://www.cyberciti.biz/faq/linux-unix-bsd-xargs-construct-argument-lists-utility/

2011年5月31日 星期二

Stop on Error

shell script default 是在有 Error 時也不會停下來。
如果要在有任何 error 時停下來,可以用
set -e
如果之後又不需要了 (有 error 也不用停),可以再用
set +e



ref : http://www.linuxjournal.com/node/1005733

2011年5月6日 星期五

sed , @ , /

sed 的 command 和 option 用了 '/' 作分別符號,
所以當要 match 的 char 有 '/' 時?

就用 @ 來代替。

當用 @ 代替時, '/' 就恢復一般字元使用。

這裡 :
http://nano-chicken.blogspot.com/2010/01/linux-modules11-sysfs-and-device-node.html
有使用的範例。

2011年5月3日 星期二

find - exclude some folder

是用指定 "path 中不包含某些字串" 完成的:
find . -type f -not -path "./.repo*"
-- 不要找./.repo 下的
如果有一堆,就 一直 -not -path ..



ref: http://blog.tcmacdonald.com/content/exclude-directories-bash-find

也可以用 -prune find . -type d -name Documentation -prune -o -type f -name '*.c' -- 修剪掉所有 Documentation 目錄

多個可以用: find . -type d \( -name Documentation -o -name tools -o -name scripts \) -prune -o -type f -name '*.c' -- 修剪掉 Documentation, tools 和 scripts 三個目錄。
有關 -prune

用 man find 來看, -prune 放在 -path 後面,代表 -path 'XXX' 的這個path 會被 exclude。
--- 但是 -prune 後面好像一定要加 -o
而且要放在前面...

2011年4月29日 星期五

find files contains ***

找內含XXX的檔案

找檔案: (列出所有檔案)
find . -type f -print

交給 grep : 先交給 xargs 把一行一行轉成 argument,再交給 grep 找 XXX
find . -print | xargs grep XXX




所以變化... 找所有 包含 XXX 的 .c 檔:
在找檔時加上選項:
find . -type f -name '*.c' -print | xargs grep XXX

兩種檔案:.c, .c :
每個 -name option 前面加上 -o
find . -type f -name '*.c' -o -name '*.h' -print | xargs grep XXX




ref :

find any files contains xx then touch it

原因: android build system 的 Makefile 好像沒有把 header 列入 depenedency check。
所以當修改一個 .h 檔,就要去找出所有 include 他的 source,作 touch。

這個動作可以分成:
  1. 找出所有包含 aa.h 的檔案
  2. 取出檔名
  3. touch 這些檔案

實例:

touch 所有 include system_properties.h 的 c source code.

找出所有含 system_properties.h 的 c 檔
find .-type f -name '*\.c' | xargs grep 'system_properties\.h
這樣會列出:
./system/core/init/init.c:#include <sys/system_properties.h>
./system/core/init/parser.c:#include <sys/_system_properties.h>
./system/core/init/property_service.c:#include <sys/_system_properties.h>
./build/tools/check_prereq/check_prereq.c:#include <sys/system_properties.h>

grep 的輸出包含 檔名跟 match location。
取出檔名:用 cut 把 ':' 後面都刪除
find . -type f -name '*\.c' | xargs grep 'system_properties\.h' | xargs cut -f1 -d ':'

結果就是:
./system/core/toolbox/watchprops.c
./system/core/toolbox/getprop.c
./system/core/libcutils/properties.c
./system/core/init/init.c
./system/core/init/parser.c
./system/core/init/property_service.c
./build/tools/check_prereq/check_prereq.c

這樣就可以餵給 touch了:
... 最後加上 | xargs touch

2011年4月18日 星期一

${PWD}

shell 內建的 variable
${PWD}
代表目前所在目錄。

用在設 project search path 時可以用:

export PATH=${PWD}/mytool:${PATH}