2.1 快速瀏覽Shell腳本:
C shell 和 TC shell 類似于C語言語法
Bourne shell 是基于Algol語言
Bash和Korn shells 混合了Bourne和C shells, 但是起源于Bourne shell.
2.3 C 和TC Shell 語法結構
shbang行 |
"shbang" 行告訴采用哪個shell來解析腳本. 首先是#, !, shell的路徑 例如:#!/bin/csh or #!/bin/tcsh |
||||
注釋 |
用#符號。例如: # This is a comment |
||||
通配符 |
*, ?, and [ ] 用于文件名的擴展. |
||||
輸出顯示 |
echo 輸出顯示 echo "Hello to you\!" |
||||
局部變量 |
局部變量存在于當前shell中。使用set設置。例如 |
||||
全局變量 |
全局變量被稱為環境變量。例如 |
||||
從變量中提取值 |
為了從變量中提取之,使用$在變量前。例如: |
||||
讀取用戶輸入 |
使用 $< 讀入一行到一個變量中,例如: |
||||
參數 |
可以從命令行中傳入參數。兩種方式接受這些參數值:一個是位置參數,另外一個是argv數組。例如: 使用位置參數: |
||||
echo $1 $2 $3 |
arg1 指定為$1, arg2 to $2, etc. |
||||
echo $* |
所有的參數 |
||||
使用argv數組: |
|||||
echo $argv[1] $argv[2] $argv[3] |
|||||
echo $argv[*] |
所有參數 |
||||
echo $#argv |
參數號 |
||||
數組 |
數組是一列被空格分隔的字符。使用()把字符都包含進去。 |
||||
set word_list = ( word1 word2 word3 ) |
|||||
set names = ( Tom Dick Harry Fred ) shift names |
從list中刪除Tom |
||||
echo $word_list[1] echo $word_list[2] echo $word_list or $word_list[*] echo $names[1] echo $names[2] echo $names[3] echo $names or echo $names[*] |
顯示第一個 |
||||
命令替換 |
使用` `例如: |
||||
set variable_name=`command` echo $variable_name |
|||||
set now = `date` echo $now echo "Today is `date`" |
|
||||
算術 |
使用@來表示計算結果的保存變量。例如 |
||||
操作符 |
|||||
相等性: |
|||||
== |
|||||
!= |
|||||
Relational: |
|||||
> |
greater than |
||||
>= |
greater than or equal to |
||||
< |
less than |
||||
<= |
less than or equal to |
||||
邏輯性: |
|||||
&& |
and |
||||
|| |
or |
||||
! |
nSot |
||||
條件語句 |
if then. if 必須用endif結尾. 還可以使用if/else。例如: |
||||
The if construct is: if ( expression ) then block of statements endif The if/else construct is: if ( expression ) then block of statements else block of statements endif |
The if/else/else if construct is: if ( expression ) then block of statements else if ( expression ) then block of statements else if ( expression ) then block of statements else block of statements endif switch ( "$color" ) case blue: echo $color is blue breaksw case green: echo $color is green breaksw case red: case orange: echo $color is red or orange breaksw default: echo "Not a valid color" endsw |
||||
switch 結構: switch variable_name case constant1: statements case constant2: statements case constant3: statements default: statements endsw |
|||||
循環 |
兩種類型循環語句:while和foreach |
||||
文件測試 |
例如: |
||||
–r |
當前用戶能讀文件 |
||||
–w |
當前用戶能寫文件 |
||||
–x |
當前用戶能執行文件 |
||||
–e |
文件存在 |
||||
–o |
當前用戶擁有文件 |
||||
–z |
文件長度為0 |
||||
–d |
文件是目錄 |
||||
–f |
文件是普通文件 |
2.3.1 C/TC Shell 腳本
Example 2.2.
1 #!/bin/csh –f 2 # The Party Program––Invitations to friends from the "guest" file 3 set guestfile = ~/shell/guests 4 if ( ! –e "$guestfile" ) then echo "$guestfile:t non–existent" exit 1 5 endif 6 setenv PLACE "Sarotini's" 7 @ Time = `date +%H` + 1 8 set food = ( cheese crackers shrimp drinks "hot dogs" sandwiches ) 9 foreach person ( `cat $guestfile` ) 10 if ( $person =~ root ) continue 11 mail –v –s "Party" $person << FINIS # Start of here document
Hi $person!Please join me at $PLACE for a party!
Meet me at $Time o'clock. I'll bring the ice cream.
Would you please bring $food[1] and anything else you would like to eat?
Let me know if you can make it. Hope to see you soon. Your pal, ellie@`hostname` # or `uname -n` 12 FINIS 13 shift food 14 if ( $#food == 0 ) then set food = ( cheese crackers shrimp drinks "hot dogs" sandwiches ) endif 15 end echo "Bye..."
解釋:
1.告訴kernel,這是C shell腳本. ”–f“表示快速啟動.就是說不要執行.cshrc.
2.注釋行
3.變量guestfile 被設置為調用guests的全路徑
4.行讀入,如果guests不存在,打印 "guests nonexistent" ,退出腳本。
5.endif
6.PLACE 環境變量
7.Time局部變量。@是內建算術。
8.food數組。
9.foreach循環。命令替換`cat $guestfile`.
10.條件測試
11.foreach循環
12.FINIS是用戶定義的終結符
13.shift命令取得下一個person
14.如果food為空,將會重置。
15.循環結束標志