Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Shell Programming.

Similar presentations


Presentation on theme: "C Shell Programming."— Presentation transcript:

1 C Shell Programming

2 特殊字元 $ : 代表shell變數名稱的開頭. # : 註解的開始. & : 在背景執行行程. ? : 對應1個字元.
* : 對應1個或多個字元. [ ]:對應一個限定範為的字元. { }:對應列舉的字串.

3 特殊字元(續) 輸入與輸出字元: command < file :表示由file輸出的結果將傳入command當成輸入
command > file :表示command的結果輸出到file中. command1 | command2 : command1的輸出會成為command2的輸入.

4 設定變數 利用 set 來取得變數: 也可以利用 `command` 來取得命令: set ABC = "I am ABC"
set dv=`date`//將變數dv的值設為date命令所取得之值. set name= “john” set info = `who | grep $name` //從who所輸出的上線訊息去找是否有”john”這個字串,若有就將此訊息設定給變數info.

5 如何執行script 第一行的第一個字必須是#,它代表此檔案是一個C shell script。
% chmod u+x script(檔名) 直接鍵入script(檔名)即可執行

6 If 條件 語法: if (expression) command
If (expression) then // if 與then必須在同一行 commands endif //endif必須單獨在一行 If (expression) then else endif

7 If範例 #!/usr/bin/csh //kitty的設定環境,使用pico或vi編輯
#show the relation between n1 and n2 set n1 = 1 set n2 = 2 if ($n1 > $n2) then echo "$n1 is bigger than $n2" else echo "$n1 is not bigger than $n2" endif 使用pico,照此內容編輯, 存檔(給定副檔名.csh)後, 直接在提示符號之處, 輸入檔名與副檔名,即可執行此shell檔

8 簡易重複執行的指令 repeat n 指令 //重複執行指令n次,且指令只能有一行。
repeat 3 echo hello;echo yoyo 執行結果: 上面的那行其實就等於 repeat 3 echo hello echo yoyo

9 foreach迴圈 foreach variable (wordlist) commands end
//從wordlist的第一個字開始,每次會指定wordlist的下一個字給variable,一直到最後一個字被指定至variable且commands執行為止, wordlist 之間要以空白隔開。

10 foreach迴圈範例 EX. #!/usr/bin/csh //kitty的設定環境
#show “Test foreach construct” in 3 times foreach var(1 2 3) echo “Test foreach construct” end

11 Foreach迴圈(續) Result: Test foreach construct

12 While迴圈 while (expression) EX: #!/usr/bin/csh #test while
commands end EX: #!/usr/bin/csh #test while set counter = 0 while ($counter <= 5) echo "sleeping for 2 seconds" sleep 2 set counter = `expr $counter + 1 ` 在shell必須使用expr指令來輔助做四則運算,如果要將結果指定給變數,必須使用`包起來,且在+ - * /兩邊都要有空白,否則將會產生ERROR。 此符號為與~同一鍵盤的`

13 While迴圈結果 sleeping for 2 seconds

14 switch switch (string) case pattern1 commands breaksw; case pattern2
……… default: endsw


Download ppt "C Shell Programming."

Similar presentations


Ads by Google