打不死的心态活到老。

Linux程式设计-11.Shell Script(bash)--(1)简介

上一篇 / 下一篇  2007-08-03 12:45:32 / 个人分类:学习资料

E%|DfD3y9| R0J0来自网络51Testing软件测试网7O[M%A{Tb

f+^eMHp S0Linux程式设计-11.Shell scrīpt(bash)--(1)简介51Testing软件测试网J!d y4N,z

%S+S3S_u'} u0http://www.openchess.org/noitatsko/programming/(2001-05-25 08:10:00)51Testing软件测试网*cc OV2YX/R Dp
众所皆知地,UNIX上以小工具着名,利用许多简单的小工具,来完成原本需要大量软体开发的工作,这一点特色,使得UNIX成为许多人心目中理想的系统平台。51Testing软件测试网|!N t1L9E6y
在众多的小工具中,Shell scrīpt算得上是最基本、最强大、运用最广泛的一个。它运用围之广,不但从系统启动、程式编译、定期作业、上网连线,甚至安装整个Linux系统,都可以用它来完成。51Testing软件测试网m0p)p7h%w$wE

51Testing软件测试网+j#Gf4@~M,xFaA

因为Shell scrīpt是利用您平日在使用的一些指令,将之组合起来,成为一个"程式"。如果您平日某些序列的指令下得特别频繁,便可以将这些指令组合起来,成为另一个新的指令。这样,不但可以简化并加速操作速度,甚至还可以乾脆自动定期执行,大大简化系统管理工作。

P)SzA y:i051Testing软件测试网u/fV(a,CV{

51Testing软件测试网]P#h(E$D.}#l
--------------------------------------------------------------------------------

@i8n M5}n051Testing软件测试网OB){ V TR2md2W*G

Bash(GNU Bourne-Again SHell)是许多Linux平台的内定Shell,事实上,还有许多传统UNIX上用的Shell,像tcsh、csh、ash、bsh、ksh等等, Shell scrīpt大致都类同,当您学会一种Shell以後,其它的Shell会很快就上手,大多数的时候,一个Shell scrīpt通常可以在很多种Shell上使用。51Testing软件测试网*[4ar6Z l$\mHK
这里我介绍您bash的使用方法。事实上,当您"man bash"时,就可以看到bash的说明书,不过对许多人来说,这份说明书犹如"无字天书"一样难懂。这份文件,主要资料来源为"man bash",我加上一些实际日常的应用例来说明。希望这样能让那些始终不得其门而入的人们,多多少少能有点概念。51Testing软件测试网&A#b+M0MEF

r-E csXpv051Testing软件测试网&NJ`&A$D` H'u
"Hello world" Shell scrīpt
jp bd |{ ~'uP0照传统程式教学例,这一节介绍Shell scrīpt的"Hello World"如何撰写。

|^k7['s wt)e0

cn8Qrd3F051Testing软件测试网ui.h!SN
#!/bin/sh51Testing软件测试网1_&y;h+g nv;?'?
# Filename : hello
.F.K| k/K8O_F/a9_Z0echo "Hello world!"51Testing软件测试网'hb$?PQ:L a,kT

51Testing软件测试网\"H(L+H x6n-G

--------------------------------------------------------------------------------

z r7F`h'S0HuT051Testing软件测试网(h2WErA

大家应该会注意到第一行的"#!/bin/sh"。在UNIX下,所有的可执行scrīpt,不管是那一种语言,其开头都是"#!",例如Perl是 "#!/usr/bin/perl",tcl/tk是"#!/usr/bin/wish",看您要执行的scrīpt程式位置在那里。您也可以用"#! /bin/bash"、"#!/bin/tcsh"等等,来指定使用特定的Shell。51Testing软件测试网X;J5V3|1K y qmO
echo是个bash的内建指令。

6R4mgF*v;y1G+U7W:B051Testing软件测试网#Ky-f1U4AJ@|0}x

 

4`f:[ E/E2t051Testing软件测试网$o4B6pV6W_(E

--------------------------------------------------------------------------------51Testing软件测试网0xB:}!ur+^S.}

T5gyxJ ^v5U.xS8JN0接下来,执行hello这个scrīpt:51Testing软件测试网,B S1H;[U!?*B'S"uP
要执行一个scrīpt的方式有很多种。51Testing软件测试网(e he`/_S*z

51Testing软件测试网+U9lZ'MS9fh$Iq6T!aA

 51Testing软件测试网;s-UV#ql5K%?VWz

8g9||)Lh[I'v/L0--------------------------------------------------------------------------------51Testing软件测试网:k\;X)qgYU

51Testing软件测试网:[I M7k c5aw ~q {3X

第一种 : 将hello这个档案的权限设定为可执行。
Pn)IV4]w\)X1d0[foxman@foxman bash]# chmod 755 hello51Testing软件测试网(Yc-[R(n k.GXjr
执行51Testing软件测试网1{d-l4^pw_ t
[foxman@foxman bash]# ./hello51Testing软件测试网?+h/SZ vP0{0^
hello world51Testing软件测试网(U}y+OJ/i1J,Om7}

V|Y5k vr0 

/gu~S8K/Z^(vr0

HS)~Y7n0--------------------------------------------------------------------------------51Testing软件测试网Q&{9U uc"wL(sp

MYn {.B,^ f0第二种 : 使用bash内建指令"source"或"."。51Testing软件测试网7i-Z3G8oU/u
[foxman@foxman bash]# source hello
\#hMZ6v}xw&zBR0hello world51Testing软件测试网X`n*YC8]g
51Testing软件测试网8n] CC\C,k
[foxman@foxman bash]# . hello
RS4}3XW(d+h0hello world51Testing软件测试网@ sW,^9in"u

~.@;t.jX Z?k9_Q0 

Zy3bt$Dq051Testing软件测试网6]?+e&PRg,o

--------------------------------------------------------------------------------51Testing软件测试网$pf9X`5F,Zs2i+K

51Testing软件测试网aD:V'E O!DJ2Y)a

第三种 : 直接使用sh/bash/tcsh指令来执行。51Testing软件测试网g&e%MN_V {
[foxman@foxman bash]# sh hello51Testing软件测试网4Gq }.f-l$v&Z
hello world51Testing软件测试网e@ Ug Bp6~$u

:a.@-H#bC0[foxman@foxman bash]# bash hello
vE&o(|p&_T0hello world

2x6kTy(Tn8f {051Testing软件测试网LE+o"qL8Q

 

&q Rw-P!sbP k5p0

5g,I D PA0--------------------------------------------------------------------------------

Ai8y KEorR051Testing软件测试网(c V+x _ g}J

Bash执行选项51Testing软件测试网#PLlk TR ?

NQ LU:I$l'|?;o s!s:K!r0--------------------------------------------------------------------------------51Testing软件测试网4[i*Y |tj!s2_ n

#j%[2t0|5_"}2D_0-c string : 读取string来当命令。51Testing软件测试网k}'D&C7^{0{8f
-i : 互动介面。51Testing软件测试网`1S RaV:_
-s : 由stdin读取命令。51Testing软件测试网,^hNZTVy l3` h
- : 取消往後选项的读取。51Testing软件测试网 _,t TxI#q d,M
-norc : 不要读~/.bashrc来执行。51Testing软件测试网"s(YxC f? V
-noprofile : 不要读/etc/profile、~/.bash_profile、~/.bash_login、~/.profile等等来执行。
]1Vx5QW;[0-rcfile filename : 执行filename,而非~/.bashrc
7u0y&[+Jx)L7o0-version : 显示版本。51Testing软件测试网7r4K+[4h i FQ
-quiet : 启动时不要哩唆。51Testing软件测试网#j QOr7yN
-login : 确保bash是个login shell。
U8|#yyB0-nobraceexpansion : 不要用curly brace expansion({}符号展开)。
X2cCMo/r}-kk0-nolineediting : 不用readline来读取命令列。51Testing软件测试网Q+R"}|6o`"i6oa L
-posix : 改采Posix 1003.2标准。

*{}4|"L1J0e(i#S2e2f051Testing软件测试网.k\%Ro!C

 51Testing软件测试网vQ)@0@ |Ky9f7_

51Testing软件测试网/B/k*G(d2e'X r6A8k

Bash最常见的激活模式51Testing软件测试网*XA[9C A:CQ4q2V1F
Bash最常见的激活模式是交互式和非交互式:51Testing软件测试网N7SEpv%sA
1.交互式激活
7h4wuU\0指其标准输入和输出都连接在终端上.又分为三种类型:login,非login,posix51Testing软件测试网0Dh"s4wM$tRif&IE
以及受限的shell.51Testing软件测试网&a4v(Xv IP.k
确定一个shell是否是交互式的可以通过检测$PS1环境变量.51Testing软件测试网]uy#Y T$Qx-l
(1) 当Bash作为login shell被激活时,它将依次读取并执行以下文件:51Testing软件测试网Sh{ l`ce&Zc6O
/etc/profile,~/.bash_profile,~/.bash_login,~/.profile
GJLO3UBvyXA0使用--noprofile选项激活Bash可以禁止其读任何初始化文件51Testing软件测试网"Y7ok.aodUKou\
在退出时执行 ~/.bash_logout 文件51Testing软件测试网5V&pJ#w I4v
(2) 当Bash作为非login shell被激活时,它将读取并执行~/.bashrc
U:m,F;p(Qr0使用--norc选项激活Bash可以禁止执行任何初始化文件,使用51Testing软件测试网 d?+eEv
--rcfile file可以指定执行的初始化文件.
:p;Cws*? k0e pM0(3) 当Bash作为posix模式被激活时,它将先检查$ENV变量是否定义.51Testing软件测试网\ p^/Lr.G7c@
若定义,Bash读取并执行有$ENV变量扩展而得到的文件,否则不再
C!Tl%N0m,qp:V;|9i0执行任何初始化文件。51Testing软件测试网z{XdDU3]t
(4) 当Bash作为受限模式被激活时,它除了禁止某些操作以外,其他行为
o|_c'~7p*W ['B]0其他模式相同.受限操作有:改变目录,修改$SHELL和$PATH变量,
$jo3q2h G%vJ0运行exec,以绝对路径运行程序以及使用重定向.Bash 1.x中不包含51Testing软件测试网f"W@Vy_"a7q
受限模式.
|U c A[_Ez4p02.非交互式激活51Testing软件测试网[f Zm0\5z!aEP,s
主要用来运行Shell脚本.启动后,Bash检查$BASH_ENV变量,若定义,
P G'UJ%Q fiI0这执行该变量指定文件中包含的命令.

3U*~OQ*v9bVzqn0

Q'v pZ J0Bash 的激活选项
+z F*j t#lK*k0} k0-c string 该选项表明string中包含了一条命令.如 bash -c ls ~
}h+SE&@E'i2S+e0-i 使Bash以交互式方式运行51Testing软件测试网[p$H{/U S4`B
-r 使Bash以受限方式运行
l&N xxN#ZWee2S0--login 使Bash以登录Shell方式运行51Testing软件测试网1q7NV4t8A6vv6{'`W"j
--posix 使Bash遵循POSIX标准51Testing软件测试网/T#E { k.L
--verbose 使Bash显示所有其读入的输入行51Testing软件测试网PKW1P}zO
--help 打印Bash的使用信息51Testing软件测试网C-@*S9p}
--version 打印版本信息51Testing软件测试网 k:E)f T N)o*~nF
--noprofile
5JDd1^ \3[ S Pi }%F0--norc51Testing软件测试网Tk8J ^g7~(Tz0q5[(L
--rcfile file51Testing软件测试网2{2?&z-uDZw+G

2[f'e5`)T3sd9Gq0

TAG: 学习资料

 

评分:0

我来说两句

Open Toolbar