2012年4月24日 星期二

string operation -- replace file extension name

如果只是簡單的filename 操作,可以用 "${filename...}"
例如:把所有 mp4 檔 rename 成 avi: for file in *.mp4;do mv "$file" "${file/.mp4/.avi}";done
像這個 ${file/.mp4/.avi} 就是把 $file 中 .mp4 換成 .avi,其中的兩個 "/" 符號就是 replace。


ref:
  1. http://www.thegeekstuff.com/2010/07/bash-string-manipulation/
  2. http://www.faqs.org/docs/abs/HTML/string-manipulation.html

2012年4月17日 星期二

sed -- delete lines containing string

刪除包含某些字串的內容(行)

這個用 sed 來作 sed '/match string/d' file > out
sed 是用 '/.../' 來指定操作。

/../ 內是 match 的 pattern,
最後的/ 後面指定操作。
/../d 代表刪除。

所以 sed '/match string/d' infile > out
會把infile 中,所有包含 "match string" 的字行都刪除。



要是要刪除很多字串,可以用 -e 選項把所有 pattern command 連起來.. sed '/stringA/d' -e '/stringB/d' -e '/stringC/d' infile > out

但是要是 pattern command 有很多,也可以寫在 file 裡,一行代表一個command.. # delabc : del stringA.B.C /stringA/d /stringB/d /stringC/d
然後用以下command: sed -f delabc infile > out

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年7月21日 星期四

test condition : file, directory

用在 if, while 等 流程控制 的判斷,像
if [ -f /etc/passwd ]; then
echo "found"
else
echo "not found"
fi

其中的 -f 就代表 is a file.

在 https://sites.google.com/site/tiger2000/home 有列出所有的 file test 選項:
[ -e file ] file exists
[ -f file ] file is a file
[ -d file ] file is a directory
[ -c file ] file is a character device
[ -b file ] file is a block device
[ -p file ] file is a named pipe
[ -s file ] file is not empty
[ -k file ] file's sticky bit is set
[ -S file ] file is a socket
[ -L file ] file is a symbolic link
[ -r file ] file is readable by user
[ -w file ] file is writeable by user
[ -x file ] file is executeable by user
[ -O file ] file is owner by user
[ -G file ] file is group owned by a greoup
[ -u file ] file has its set user ID bit set
[ -g file ] file has its group user ID bit set
[ file1 -nt file2 ] file1 is newer than file2
[ file1 -ot file2 ] file1 is older than file2
[ file -ef file2 ] file1 is another name for file2

[ $str1 = $str2 ] str1 string is the same as string str2

另外一個,是 and , or 的 邏輯運算:

-a and
-o or
! not

* 奇怪的是 這些 邏輯運算後面的 operand 好像要加"" 括號

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
有使用的範例。