📄 unix的批处理 shell script.htm
字号:
1. 直接下命令
这个方式和在命令列中直接下命令的效果一样。
2. 使用sh命令</pre>
<pre> sh command</pre>
<pre> 这个档案必须是Bourne Shell的Script,但这个档案并不一定要设成可执行。
除此之外和直接下命令的方式一样。
3. 使用"."命令</pre>
<pre> . command</pre>
<pre> 这时和使用sh命令相似,只不过它不像sh一般会产生新的process ,相反地,
它会在原有的process 下完成工作。
4. 使用exec命令</pre>
<pre> exec command</pre>
<pre> 此时这个Script将会被所执行的命令所取代。当这个命令执行完毕之後,这个
Script也会随之结束。
5. 使用命令替换
这是一个相当有用的方法。如果想要使某个命令的输出成为另一个命令的参数
时,就一定要使用这个方法。我们将命令列於两个"`" 号之间,而Shell 会以
这个命令执行後的输出结果代替这个命令以及两个"`" 符号。</pre>
<pre> <eg>
str='Current directory is '`pwd`
echo $str
结果如下:
Current directory is /users/cc/mgtsai
这个意思是pwd 这个命令输出"/users/cc/mgtsai",而後整个字串代替原
来的`pwd` 设定str 变数,所以str 变数的内容则会有pwd 命令的输出。
</pre>
<pre> <eg>
number=`expr $number + 1`
这就是先前所提要作数值运算的方法,基本上expr命令只将运算式解,而
後输出到标准输出上。如果要将某变数设定成其值,非得靠命令替换的方
式不可。这个例子是将number变数的值加1 後再存回number变数。
</pre>
<pre>三、流程控制</pre>
<pre> 在介绍流程控制之前,我们先来看看test命令。test命令的参数是条件判断式,当
条件为真时则传回非零值,而条件为伪时则传回零。在所有的流程控制都必须用到
test命令来判断真伪。而test命令的使用方法则列於附录B。</pre>
<pre> <eg>
test $# = 0
如果执行这个程式没有参数时,会传回非零值代表"$# = 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 │ 放到变数var │
│ │ │
│ commands │
<eg> └——————┘ │
┌———————————┐ ┌———————┘
│for a in xx yy zz │ │
│ do │
│ echo $a │
│done │
└———————————┘
结果如下:
xx
yy</pre>
<pre> yy
zz</pre>
<pre> 5. for
语法以及流程图如下
│ FALSE
for var ┌—<参数中还有东西吗?>—┐
do │ │TRUE │
commands │ 从参数中取得一项 │
done │ 放到变数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 │
└———————————————┘
结果如下:
0123456789</pre>
<pre> 7. until
语法以及流程图如下
│ TRUE
until (condition) ┌—<condition>—┐
do │ │FALSE │
commands │ commands │
done └————┘ │
┌————┘
│
它和while 的不同只在於while 是在条件为真时执行回圈,而until 是在条件
为假时执行回圈。</pre>
<pre> 8. break及continue
这两者是用於for, while, until 等回圈控制下。break 会跳至done後方执行
,而continue会跳至done执行,继续执行回圈。</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. 函数
格式如下</pre>
<pre> function-name()
{
commands
}</pre>
<pre> 而要呼叫此函数,就像在命令列下直接下命令一般。
</pre>
<pre>□C Shell</pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -