?? unix的批處理 shell script.htm
字號:
<pre> C Shell 有些特性和Bourne Shell一樣,但有些不相同。這里介紹與Bourne Shell
不相同的地方。
</pre>
<pre>一、變數(shù)</pre>
<pre> 1. 字串變數(shù)
這個部分和Bourne Shell的變數(shù)一樣,只不過在設(shè)定變數(shù)值時不能使用Bourne
Shell的方式,而必須打:</pre>
<pre> set var=value</pre>
<pre> 2. 數(shù)字運(yùn)算
基本上C Shell 沒有數(shù)字變數(shù),但C Shell 卻有簡單的方法處理數(shù)字運(yùn)算:</pre>
<pre> @ var operator expression</pre>
<pre> operator可以是C 語言中的=, +=, -=,......,而expression則是運(yùn)算式。運(yùn)
算式的運(yùn)算子如下:</pre>
<pre> A. () 改變計算的順序
~@
B. ~ 位元NOT運(yùn)算
@~~
! 邏輯否定
C. % 取馀數(shù)</pre>
<pre> C. % 取馀數(shù)
/ 除
* 乘
- 減
+ 加
D. >> 右移
<< 左移
E. > 大於
< 小於
>= 大於等於
<= 小於等於
!= 不等於
== 等於
F. & 位元AND運(yùn)算
^ 位元XOR運(yùn)算
| 位元OR 運(yùn)算
G. && 邏輯AND
|| 邏輯OR</pre>
<pre> 除此之外,我們也可以檢驗一個檔案的狀態(tài),如下</pre>
<pre> -n filename
</pre>
<pre> 而-n可為下列之一</pre>
<pre> -d 檔案是一個目錄檔案
-e 檔案存在
-f 檔案為一般的檔案
-o 使用者擁有這個檔案
-r 使用者可以讀取這個檔案
-w 使用者可以寫入這個檔案
-x 使用者可以執(zhí)行這個檔案
-z 檔案長度為0</pre>
<pre> <eg>
@ count = count + 1
@ flag = -e /users/cc/mgtsai/mail && -e /usr/spool/mail</pre>
<pre> 3. 陣列
在C Shell 中,我們可以宣告陣列變數(shù),方式如下</pre>
<pre> set var=(val1 val2 ......)</pre>
<pre> 而var[1]之值為val1,var[2]之值為val2......。而$var代表整個陣列。我們
可以用$#var 來計算陣列個數(shù),也可以用$?var 來檢查某個變數(shù)是否已宣告。
</pre>
<pre> 4. 特殊變數(shù)
$argv 和Bourne Shell的$*相似,只不過這是一個陣列。
$argv[n] 和Bourne Shell的$n相同,但不受個數(shù)限制。
$#argv 和Bourne Shell的$#相同
$home 和Bourne Shell的$HOME相同
$path 和Bourne Shell的$PATH相似,只不過這是一個陣列
$prompt 和Bourne Shell的$PS1相同
$shell Shell的路徑名稱
$status 和Bourne Shell的$?相同
$$ 和Bourne Shell的$$相同
$< 鍵盤輸入
</pre>
<pre>二、執(zhí)行命令</pre>
<pre> 基本上和Bourne Shell相同,只有一點在Bourne Shell中的"." 命令在C Shell 中
則為"source"命令。
</pre>
<pre>三、流程控制
</pre>
<pre> 在C Shell 中流程控制不像Bourne Shell中一般需要使用test命令。相反地,它和
C 語言類似只要在條件中寫出運(yùn)□式即可。當(dāng)運(yùn)算結(jié)果不為零時,其值為真,為零
時其值為偽。以下是C Shell的流程控制
1. if
語法如下</pre>
<pre> if (expression) simple-command</pre>
<pre> 2. goto
語法如下</pre>
<pre> goto label</pre>
<pre> 這時程式會跳至以l"label:"開頭的那一行執(zhí)行</pre>
<pre> <eg>
if ($#argv == 2) goto goodargs
echo 'Please use two arguments.'
exit
goodrags:
...
</pre>
<pre> 3. if then else
這和Bourne Shell的if then, if then else, if then elif 相似。語法如下</pre>
<pre> A. if (expression) then
commands
endif</pre>
<pre> B. if (expression) then
commands
else
commands
endif</pre>
<pre> C. if (expression) then
commands
else if (expression) then
commands
else
commands
endif
</pre>
<pre> 4. foreach
這和Bourne Shell的for in相似。語法如下</pre>
<pre> foreach var (arg-list)
commands
end</pre>
<pre> 5. while
這和Bourne Shell的while相似。語法如下</pre>
<pre> while (expression)
commands
end</pre>
<pre> 6. break及continue
這和Bourne Shell的break 及continue相似,是用來中斷foreach 及while 回
圈。</pre>
<pre> 7. switch
這和Bourne Shell的case相似。語法如下</pre>
<pre> switch (string)
case pat1:
commands1
breaksw
case pat2:
commands2
breaksw
case pat3:
commands3
breaksw
endsw
</pre>
<pre>□附錄A expr命令
</pre>
<pre>命令格式</pre>
<pre> expr expression</pre>
<pre>敘述</pre>
<pre> expression是由字串以及運(yùn)算子所組成,每個字串或是運(yùn)算子之間必須用空白隔開
。下表是運(yùn)算子的種類及功能,而優(yōu)先順序則以先後次序排列,我們可以利用小括
號來改變運(yùn)算的優(yōu)先次序。其運(yùn)算結(jié)果則輸出至標(biāo)準(zhǔn)輸出上。</pre>
<pre> : 字串比較。比較的方式是以兩字串的第一個字母開始,而以第二個字串的
字母結(jié)束。如果相同時,則輸出第二個字串的字母個數(shù),如果不同時則傳
回0 。
* 乘法
/ 除法
% 取馀數(shù)
+ 加法
- 減法
< 小於
<= 小於等於
= 等於
!= 不等於
>= 大於等於
> 大於
& AND運(yùn)算
| OR運(yùn)算</pre>
<pre> 當(dāng)expression中含有"*", "(", ")" 等符號時,必須在其前面加上"\" ,以免被
Shell 解釋成其它意義。
</pre>
<pre> <eg> expr 2 \* \( 3 + 4 \) 其輸出為14
</pre>
<pre>□附錄B test命令
</pre>
<pre>命令格式</pre>
<pre> test expression</pre>
<pre>敘述</pre>
<pre> expression中包含一個以上的判斷準(zhǔn)則以作為test評詁的標(biāo)準(zhǔn)。兩準(zhǔn)則間用"-a"代
表邏輯AND 運(yùn)算,"-o"代表邏輯OR運(yùn)算,而在準(zhǔn)則前放置一"!" 代表NOT 運(yùn)算。如
果沒有括號,則優(yōu)先權(quán)則為"!" > "-a" > "-o" 。和expr命令相同,相使用左右括
號時,必須在其前面加上"\" 。以下是有關(guān)準(zhǔn)則的敘述(合敘述時傳回真,否則傳
回偽):</pre>
<pre> string string不為空白字串
-n string string的長度大於0
-z string string的長度等於0
string1=string2 string1等於string2
string1!=string2 string1不等於string2
int1 -gt int2 int1大於int2
int1 -ge int2 int1大於等於int2
int1 -eq int2 int1等於int2
int1 -ne int2 int1不等於int2
int1 -le int2 int1小於等於int2
int1 -lt int2 int1小於int2
-r filename 檔案可讀取
-w filename 檔案可寫入
-x filename 檔案可執(zhí)行
-f filename 檔案為一般檔
-d filename 檔案為目錄
-s filename 檔案為非空的一般檔</pre>
<pre> <eg> test -r "$filename" -a -s "$filename"</pre>
<p> </p>
<pre><strong><big> <a href="../hkbx!.htm">返回“新客補(bǔ)習(xí)”</a></big></strong></pre>
</body>
</html>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -