1 / 14

C Shell Programming

C Shell Programming. 特殊字元. $ : 代表 shell 變數名稱的開頭 . # : 註解的開始 . & : 在背景執行行程 . ? : 對應 1 個字元 . * : 對應 1 個或多個字元 . [ ]: 對應一個限定範為的字元 . { }: 對應列舉的字串. 特殊字元 ( 續 ). 輸入與輸出字元 : command < file : 表示由 file 輸出的結果將傳入 command 當成輸入 command > file : 表示 command 的結果輸出到 file 中 .

gavin
Download Presentation

C Shell Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

  5. 如何執行script • 第一行的第一個字必須是#,它代表此檔案是一個C shell script。 • 同時#在script中也是註解符號,在#後面的同一行內容均不會執行。 • 當編輯完script之後還要去更改它的權限,讓它可以被執行. • % chmod u+x script(檔名) • 直接鍵入script(檔名)即可執行

  6. If 條件 • 語法: • if (expression) command //若expression的結果為真,將執行command • If (expression) then // if 與then必須在同一行 commands endif //endif必須單獨在一行 • If (expression) then commands else commands 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

  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 • Test foreach construct • Test foreach construct

  12. While迴圈 • while (expression) 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 ` end • 在shell必須使用expr指令來輔助做四則運算,如果要將結果指定給變數,必須使用`包起來,且在+ - * /兩邊都要有空白,否則將會產生ERROR。 此符號為與~同一鍵盤的`

  13. While迴圈結果 • sleeping for 2 seconds • sleeping for 2 seconds • sleeping for 2 seconds • sleeping for 2 seconds • sleeping for 2 seconds

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

More Related