?? unix的批處理 shell script.htm
字號:
1. 直接下命令
這個方式和在命令列中直接下命令的效果一樣。
2. 使用sh命令</pre>
<pre> sh command</pre>
<pre> 這個檔案必須是Bourne Shell的Script,但這個檔案并不一定要設(shè)成可執(zhí)行。
除此之外和直接下命令的方式一樣。
3. 使用"."命令</pre>
<pre> . command</pre>
<pre> 這時和使用sh命令相似,只不過它不像sh一般會產(chǎn)生新的process ,相反地,
它會在原有的process 下完成工作。
4. 使用exec命令</pre>
<pre> exec command</pre>
<pre> 此時這個Script將會被所執(zhí)行的命令所取代。當這個命令執(zhí)行完畢之後,這個
Script也會隨之結(jié)束。
5. 使用命令替換
這是一個相當有用的方法。如果想要使某個命令的輸出成為另一個命令的參數(shù)
時,就一定要使用這個方法。我們將命令列於兩個"`" 號之間,而Shell 會以
這個命令執(zhí)行後的輸出結(jié)果代替這個命令以及兩個"`" 符號。</pre>
<pre> <eg>
str='Current directory is '`pwd`
echo $str
結(jié)果如下:
Current directory is /users/cc/mgtsai
這個意思是pwd 這個命令輸出"/users/cc/mgtsai",而後整個字串代替原
來的`pwd` 設(shè)定str 變數(shù),所以str 變數(shù)的內(nèi)容則會有pwd 命令的輸出。
</pre>
<pre> <eg>
number=`expr $number + 1`
這就是先前所提要作數(shù)值運算的方法,基本上expr命令只將運算式解,而
後輸出到標準輸出上。如果要將某變數(shù)設(shè)定成其值,非得靠命令替換的方
式不可。這個例子是將number變數(shù)的值加1 後再存回number變數(shù)。
</pre>
<pre>三、流程控制</pre>
<pre> 在介紹流程控制之前,我們先來看看test命令。test命令的參數(shù)是條件判斷式,當
條件為真時則傳回非零值,而條件為偽時則傳回零。在所有的流程控制都必須用到
test命令來判斷真?zhèn)巍6鴗est命令的使用方法則列於附錄B。</pre>
<pre> <eg>
test $# = 0
如果執(zhí)行這個程式?jīng)]有參數(shù)時,會傳回非零值代表"$# = 0"這個條件成立。反
之則會傳回零。</pre>
<pre> 以下介紹各種流程控制:</pre>
<pre> 1. if then
語法以及流程圖如下</pre>
<pre> 語法以及流程圖如下
│ FALSE
if (condition) <condition>—┐
then │TRUE │
then-commands then-commands │
fi ├————┘
│</pre>
<pre> condition 是一個test命令。往後所介紹的各種流程中的condition 都是test
命令。</pre>
<pre> <eg>
檔名:chkarg
┌———————————┐
│if (test $# != 0) │
│ then │
│ echo Arg1: $1 │
│fi │
└———————————┘
$ chkarg Hello
Arg1: Hello
$ chkarg
$
</pre>
<pre> 2. if then else
語法以及流程圖如下
│ FALSE
if (condition) <condition>—————┐
then │TRUE │
then-commands then-commands else-commands
else ├————————┘
else-commands │
fi</pre>
<pre> 3. if then elif
語法以及流程圖如下
│ FALSE
if (condition1) <condition1>—┐
then │TRUE │ FALSE
commands1 commands1 <condition2>—┐
elif (condition2) │ │TRUE │
then │ commands2 commands3
commands2 ├—————┴————┘
else │
commands3</pre>
<pre> commands3
fi</pre>
<pre> <eg>
echo 'word 1: \c'
read word1
echo 'word 2: \c'
read word2
echo 'word 3: \c'
read word3
if (test "$word1" = "$word2" -a "$word2" = "$word3")
then
echo 'Match: words 1, 2, & 3'
elif (test "$word1" = "$word2")
then
echo 'Match: words 1 & 2'
elif (test "$word1" = "$word3")
then
echo 'Match: words 1 & 3'
elif (test "$word2" = "$word3")
then
echo 'Match: words 2 & 3'
else
echo 'No match'
fi</pre>
<pre> 4. for in
語法以及流程圖如下
│ FALSE
for var in arg-list ┌—<arg-list還有東西嗎?>—┐
do │ │TRUE │
commands │ 從arg-list取得一項 │
done │ 放到變數(shù)var │
│ │ │
│ commands │
<eg> └——————┘ │
┌———————————┐ ┌———————┘
│for a in xx yy zz │ │
│ do │
│ echo $a │
│done │
└———————————┘
結(jié)果如下:
xx
yy</pre>
<pre> yy
zz</pre>
<pre> 5. for
語法以及流程圖如下
│ FALSE
for var ┌—<參數(shù)中還有東西嗎?>—┐
do │ │TRUE │
commands │ 從參數(shù)中取得一項 │
done │ 放到變數(shù)var │
│ │ │
│ commands │
<eg> └—————┘ │
檔名:lstarg ┌———————┘
┌———————————┐ │
│for a │
│ do │
│ echo $a │
│done │
└———————————┘
$lstarg xx yy zz
xx
yy</pre>
<pre> yy
zz</pre>
<pre> 6. while
語法以及流程圖如下
│ FALSE
while (condition) ┌—<condition>—┐
do │ │TRUE │
commands │ commands │
done └————┘ │
┌————┘
│
<eg>
┌———————————————┐
│number=0 │
│while (test $number -lt 10) │
│ do │
│ echo "$number\c" │
│ number=`expr $number + 1` │
│done │
│echo │
└———————————————┘
結(jié)果如下:
0123456789</pre>
<pre> 7. until
語法以及流程圖如下
│ TRUE
until (condition) ┌—<condition>—┐
do │ │FALSE │
commands │ commands │
done └————┘ │
┌————┘
│
它和while 的不同只在於while 是在條件為真時執(zhí)行回圈,而until 是在條件
為假時執(zhí)行回圈。</pre>
<pre> 8. break及continue
這兩者是用於for, while, until 等回圈控制下。break 會跳至done後方執(zhí)行
,而continue會跳至done執(zhí)行,繼續(xù)執(zhí)行回圈。</pre>
<pre> 9. case
語法以及流程圖如下
│ TRUE
case str in <str=pat1>————commands1—┐
pat1) commands1;; │FALSE TRUE │
pat2) commands2;; <str=pat2>————commands2—┤
pat3) commands3;; │FALSE TRUE │
esac <str=pat3>————commands3—┤
│FALSE │
├————————————┘
│
而pat 除了可以指定一些確定的字串,也可以指定字串的集合,如下</pre>
<pre> * 任意字串
? 任意字元
[abc] a, b, 或c三字元其中之一
[a-n] 從a到n的任一字元
| 多重選擇</pre>
<pre> <eg>
┌———————————————┐
│echo 'Enter A, B, or C: \c' │
│read letter │
│case $letter in │
│ A|a) echo 'You entered A.';;│
│ B|b) echo 'You entered B.';;│
│ C|c) echo 'You entered C.';;│
│ *) echo 'Not A, B, or C';; │
│esac │
└———————————————┘</pre>
<pre> 10. 函數(shù)
格式如下</pre>
<pre> function-name()
{
commands
}</pre>
<pre> 而要呼叫此函數(shù),就像在命令列下直接下命令一般。
</pre>
<pre>□C Shell</pre>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -