顯示具有 command 標籤的文章。 顯示所有文章
顯示具有 command 標籤的文章。 顯示所有文章

2015年2月12日 星期四

return value from shell script

ref: http://tldp.org/LDP/abs/html/exit-status.html

是用 exit 指令。
exit 123
然後 caller 用 $? 就可以取得上一個 script exit 的值。

2013年12月24日 星期二

test result . 判斷執行成功或失敗

在 shell script 中,需要知道這個 command 執行成功或失敗。
用 $?
來代表

例如: cd mydir if [ "$?" -ne "0" ]; then echo failed else echo success fi
0 代表成功。



另外,有要用 pipe log ,又要判斷的情況。
例如: make -j4 2>&1 | tee makelog 這時候用 $? 就會是 tee 這個 command 執行的結果,不是 make 的

在bash, 可以用 ${PIPESTATUS[0]} 來代表 make -j4 2>&1 | tee makelog if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo failed else echo success fi

2012年8月20日 星期一

create large file with dd command

要一個大的 file 作 loop back。
要先 create。

以往都是用: dd if=/dev/zero of=10G bs=1G count=10 這樣會花一些時間,硬碟把 00 寫入 10G 的 file。


要是不管file 的內容,只要10G 大小,可以用: dd if=/dev/zero of=10G bs=1 count=0 seek=10G 這樣馬上就可以create 出來。


  • http://www.skorks.com/2010/03/how-to-quickly-generate-a-large-file-on-the-command-line-with-linux/

2012年7月22日 星期日

mount , 並指定 uid, gid 和 charset

mount 並且設定 mount folder 的 uid, gid, (or iochartset).

都是用 -o

uid, gid:
# mount -o uid=charles-chang,gid=charles-chang /dev/sdd1 ~/sd

如果是要 mount windows 的 share (samba, cifs),中文的問題可以用: iocharset:
mount -t cifs -o username=softwaretest,password=softwaretest,iocharset=utf8,uid=charles-chang,gid=charles-chang //192.168.147.225/ODM3 /home/charles-chang/buildingmachine

兩個 -o 可以何在一起,中間用 ',' 隔開

2011年8月28日 星期日

mount partition with read-write permission for all users

mount 時,使用 umask option 可以指定 mount 位置得權限。

#mount -o umask=0002 /dev/sdd1 /sd
這樣 /sd 的權限會是:
rwxrwxr-x


umask 的 內容是 八進制,而且是 "not"。
所以 0002 就是不包含 other 的 write 權限。



所以 -o umask=0000 就是所有人,所有權限

2011年5月31日 星期二

Stop on Error

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



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

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月17日 星期日

string as command -- with variable

要 echo command 出來,又要執行,一般:

cmd='ls a1'
${cmd}

要是 command 中要用到變數

var=a1
cmd='ls ${var}'
${cmd}

結果 ${var} 整個被當作是 string 印出來,而不是代換成 a1。

文章說,要用作變數的話,要用雙引號 "
所以

var=a1
cmd="ls ${var}"
${cmd}

結果就正確了。



ref: http://lowfatlinux.com/linux-script-variables.html