c shell programming

14
C Shell Programming

Upload: gavin

Post on 05-Jan-2016

31 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: C Shell Programming

C Shell Programming

Page 2: C Shell Programming

特殊字元• $ : 代表 shell 變數名稱的開頭 .

• # : 註解的開始 .

• & : 在背景執行行程 .

• ? : 對應 1 個字元 .

• * : 對應 1 個或多個字元 .

• [ ]: 對應一個限定範為的字元 .

• { }: 對應列舉的字串 .

Page 3: C Shell Programming

特殊字元 ( 續 )

• 輸入與輸出字元 :– command < file : 表示由 file 輸出的結果將傳入

command 當成輸入– command > file : 表示 command 的結果輸出到

file 中 .– command1 | command2 : command1 的輸出

會成為 command2 的輸入 .

Page 4: C Shell Programming

設定變數• 利用 set 來取得變數 :

– set ABC = "I am ABC"

• 也可以利用 `command` 來取得命令 :– set dv=`date`// 將變數 dv 的值設為 date 命令

所取得之值 .– set name= “john”– set info = `who | grep $name` // 從 who 所輸

出的上線訊息去找是否有” john” 這個字串 , 若有就將此訊息設定給變數 info.

Page 5: C Shell Programming

如何執行 script

• 第一行的第一個字必須是 #, 它代表此檔案是一個 C shell script 。

• 同時 # 在 script 中也是註解符號 , 在 # 後面的同一行內容均不會執行。

• 當編輯完 script 之後還要去更改它的權限 ,讓它可以被執行 .

• % chmod u+x script( 檔名 )

• 直接鍵入 script( 檔名 ) 即可執行

Page 6: C Shell Programming

If 條件• 語法 :

1. if (expression) command // 若 expression 的結果為真,將執行 command

2. If (expression) then // if 與 then 必須在同一行commands

endif //endif 必須單獨在一行2. If (expression) then commands else

commands endif

Page 7: C Shell Programming

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

Page 8: C Shell Programming

簡易重複執行的指令• repeat n 指令 // 重複執行指令 n 次,且

指令只能有一行。repeat 3 echo hello;echo yoyo

執行結果:

上面的那行其實就等於

repeat 3 echo hello

echo yoyo

Page 9: C Shell Programming

foreach 迴圈• foreach variable (wordlist)

commands

end

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

Page 10: C Shell Programming

foreach 迴圈範例EX.

#!/usr/bin/csh //kitty 的設定環境 #show “Test foreach construct” in 3 times

foreach var(1 2 3)

echo “Test foreach construct”

end

Page 11: C Shell Programming

Foreach 迴圈 ( 續 )

Result:

• Test foreach construct

• Test foreach construct

• Test foreach construct

Page 12: C Shell Programming

While 迴圈• while (expression)

commandsend

• 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

在在 shellshell 必須使用必須使用 exprexpr 指指令來輔助做四則運算,如果令來輔助做四則運算,如果要將結果指定給變數,必須要將結果指定給變數,必須使用使用 `̀ 包起來,且在包起來,且在 + - * /+ - * /兩邊都要有空白,否則將會兩邊都要有空白,否則將會產生產生 ERRORERROR 。。

此符號為與 ~ 同一鍵盤的 `

Page 13: C Shell Programming

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

Page 14: C Shell Programming

switch

• switch (string) case pattern1

commands breaksw;

case pattern2 commands breaksw;

………default:

commands breaksw;

endsw