- 課本: Introduction to Linux
- complexity v.s. simplicity (figure from "Starting Forth")
- 課本 第二章 概述
- 課本 第三章指令簡表
- summary of shell programming
- demo sh script
- demo awk scripts
- summary of awk man page
- GNU document for
make
make.
- GNU document for
bash
- GNU document for
sed
- GNU document for
awk
- GNU document for
GNU core utils
- note of tar and gzip
- vi 的參考資料
- 作業一: finding files
從 /var/tmp/exercise-one 目錄底下找出自己的檔案,
並在 ~/unix-1031/exer1 下建立一檔案, 名為 "myfiles", 內容為找到的檔名.
一行一個, 共有四個.
如為 /var/tmp/exercise-one/Sam/likes/a-duck
則只需輸入 Sam/likes/a-duck 即可. 注意大小寫.
編輯器可用 joe or vi
deadline: 2014 Sep 26, 00:05 am
- 作業二: cloning files and directories
從 /var/tmp/exercise-two 目錄底下找出自己的目錄及檔案,
並複製到 ~/unix-1031/exer2 下. 如果有以下目錄及檔案
/var/tmp/exercise-two/Alice/likes/an-ox <---- a file
/var/tmp/exercise-two/Emily/has-bought/a-horse <---- a file
/var/tmp/exercise-two/Gary/has-bought/a-rhino <---- a directory
/var/tmp/exercise-two/Gary/has-bought/a-rhino/less <---- a file
/var/tmp/exercise-two/Gary/has-bought/a-rhino/is <---- a file
/var/tmp/exercise-two/Gary/has-bought/a-rhino/more <---- a file
/var/tmp/exercise-two/Mary/is/a-snake <---- a file
則在你的 exer2 下建立如下對應的結構.
~/unix-1031/exer2/
|---Alice/
| `---likes/
| `---an-ox
|---Emily/
| `---has-bought/
| `---a-horse
|---Gary/
| `---has-bought/
| `---a-rhino/
| |---is
| |---less
| `---more
`---Mary/
`---is/
`---a-snake
deadline: 2014 Oct 3, 00:05 am
- 作業三: cloning a directory
在 ~/unix-1031/exer3 建出一個目錄結構, 使其與
/var/tmp/exercise-three 一樣
也就是說除了最上層的目錄不同外, ~/unix-1031/exer3 與
/var/tmp/exercise-three 要一模一樣. (當然 owner 不可能一樣)
- 若目錄或檔名開頭為 ex-, 則新目錄名或檔名為移除 ex- 後剩餘部分.
- 若為 file, 內容必須一樣.
- 若為 hard link, 則新建出的檔案間的關係也是 hard link.
- 若為 soft link, 則新建出的檔案間的關係也是 soft link, 而
且 link file 的 內容也必須一樣.
deadline: 2014 Oct 10, 00:05 am
- 作業四: at and crontab
- 用 at 在 11/07 01:00 至 03:00 間執行一個程式. 時間自選, 作一次即可.
輸出的第一行要包含當時執行的時間.
- 設定 crontab, 在一小時內執行三次程式, 時間任選, 每次 run
的程式不一定都要一樣.
第一次的輸出會新增或蓋掉既有的檔案, 第二次及第三次的輸出則
會接到前一次的後面.
每次輸出的第一行要包含當時執行的時間.
- at 及 crontab 所需的程式及輸出檔案皆放在 ~/unix-1031/exer4 下.
- 請參閱 README-exer4.
deadline: 2014 Nov 07, 00:05 am
- 作業五: practicing regular expression with grep and sed
用 grep 及 sed 寫一個程式, 從 stdin 讀進, 過濾後寫到 stdout.
從 stdin 找出含有 klim 或 milk 的那幾行, 然後把 klim 和 milk 互換.
範例如下:
===prompt===> cat IN-5
klim is good.
But oak is unknown.
Really? How about redcow?
Drinking klim makes you healthy.
So?
At least you drink much milk.
Hmm.
klim is not milk.
===prompt===> ./ex5.sh < IN-5
milk is good.
Drinking milk makes you healthy.
At least you drink much klim.
milk is not klim.
===prompt===>
程式請放至 ~/unix-1031/exer5 下.
請務必用 chmod 將檔案設成可執行.
deadline: 2014 Nov 21, 00:05 am
- 作業六: awk programming
與前一個作業相同, 但改用 awk 來作.
程式請放至 ~/unix-1031/exer6 下.
請務必用 chmod 將檔案設成可執行.
deadline: 2013 Nov 28, 00:05 am
- 作業七: awk programming, associative array
如下例所示, 計算每個人的總和.
$ cat IN-7
# sample input file of exercise 7
# this is an blank line.
1234 Alice # Alice wins 1234
999 Bob # Bob wins 999
Alice 200 # Alice spends 200
Bob 100 Alice # Bob loses 100 to Alice
4000 Bob 250 Carol # Bod wins 4000 and loses 250 to Carol
3000 Carol 300 # Carol wins 3000 and spends 300
$ ./ex7.awk IN-7
Carol has 2950
Bob has 4649
Alice has 1134
$
deadline: 2014 Dec 12, 00:05 am
- 作業八: shell and awk programming
- 寫一個 awk script, 讀進一個 file. 其中 # 以後為 comment.
不是 comment 的那些行包含兩個 patterns, 若第一行含 A1 B1, 第二
行含 A2 B2, 則 awk 會產出二個檔, 一為 grep 用的 pattern file, 一為
sed 用的 command file. 若將 grep 執行的結果用 pipe 送給 sed,
則會取出 stdin 含 A1 或 A2 的那幾行, 並將 A1 換成 B1, A2 換成 B2.
- 寫一個 shell script, 判斷是否有一個參數. 若有, 則將其視為 awk 的
input. 呼叫 awk, 並結合 grep 與 sed 以達成上述功能.
$ cat IN-data-8
This is klim speaking.
klim is good, klim is healthy, and klim is not milk.
Haha, it is funny.
$ cat IN-pat-8
# Each line is composed of two strings as the following shows.
klim toostrong
milk klim
toostrong milk
$ cat IN-data-8 | ./ex8.sh
./ex8.sh pattern-file
$ cat IN-data-8 | ./ex8.sh IN-pat-8
This is milk speaking.
milk is good, milk is healthy, and milk is not klim.
$
deadline: 2014 Dec 19, 00:05 am
- 作業九: calculating the sum for someone (sh)
寫一 sh script, 計算某人(第一個參數) 的總和. 第二個參數後為多個檔名,
若無指定檔名則從 stdin 讀入. input 的格式見下列例子.
$ ./ex9.sh
./ex9.sh name [file1 file2] ....
$ ./ex9.sh cow
moo 1234 9833
cow 200 400
klim 200
cow 1000
cow has total 1600 dollars.
$ cat Week-1
# first week
klim 120 100 900 #not much
milk 98 33 100 120
moo 10000 200 20
cow 2000 10 100000 # Wow!
$ cat Week-2
moo 20 100 20 1
cow 299 44 22
cow 333 22 90
klim 49 22
moo 333
$ ./ex9.sh cow Week-1
cow has total 102010 dollars.
$ ./ex9.sh cow Week-2
cow has total 810 dollars.
$ ./ex9.sh cow Week-1 Week-2
cow has total 102820 dollars.
$ cat Week-1 Week-2 | ./ex9.sh cow
cow has total 102820 dollars.
$
注意: 此作業不能用 awk.
deadline: 2014 Dec 26, 00:05 am
作業請放至 ~/unix-1031/exer9 下.