Professional Documents
Culture Documents
Shell Script
Shell Script
Shell
Bourne shell (/bin/sh) shell 9
(Algo) shell script
starndard shell 9 UNIX shell script UNIX 9
default prompt 9 $
C shell (/bin/csh) shell 9 Bourne shell 9
C function 9
Bourne shell
99 default prompt 9 %
Korn shell (/bin/ksh) shell 9 Bourne shell C shell
function Bourne shell shell script
99 execute Korn
shell Bourne shell C shell
UNIX default prompt 9 $
Bourne again shell (/bin/bash /usr/local/bin/bash) Bourne shell
line editing 9
bash shell standard UNIX shell default shell
linux default prompt 9 $
Hello World
Shell script editor vi, nano,
gedit, emacs
vi ( prompt )
Syntax
Example
$ vi filename
shell script vi ( hello.bash)
9 9
9 ls l hello.bash (9 hello.bash) d rw-rxr-x 9 9
9 chmod
( prompt )
Syntax
Example
9 ls l hello.bash d 9 -rwxr-xr-x.
9 (x = executable 9 ).
shell script ( prompt )
Syntax
Example
$ . / filename
File hello.bash
( variable )
9 9
9
( vi )
Syntax
Example
variable = value
9 =
argument
( vi )
Syntax
Example
$ variable
99 $
variable=value or variable=value
Example
File testvariable.bash
( environment variable )
9 1. 2.
(environment variable)
9 9 9
9 export.
( vi )
Syntax
Example
variable = value
File testenvironment.bash
( prompt )
Syntax
Example
$ export variable
ENV prompt
( prompt )
Syntax
$ export
9 9 9
9 option n
9 export
( prompt )
Syntax
$ export n variable
( environment variable )
99(read-only)
$0 $1 $2 ....
$#
$*
$@
$?
$!
$hostname
$pwd
$oldpwd
$randow
$home
( comment )
9 # (
) script shell 9 9 #
# comment ( vi )
Syntax
Example
# text
File testcomment.bash
9 9 # 9 9
9 comment
( array variable )
array index 9 0 C
9 9 9 { }
9
array ( vi )
Syntax
Example
variable[index]=value
File testarray.bash
array ( vi )
Syntax
Example
index
test expression
bash ksh 9 test 9 expression
9
test ( vi )
Syntax
int1 eq int2
int1 ne int2
int1 gt int2
int1 ge int2
int1 le int2
int1 lt int2
str1 = str2
str1 != str2
Str
-n str
-z str
-d filename
-f filename
-r filename
-w filename
-x filename
-s filename
int1 int2
int1 int2
int1 int2
int1 int2
int1 int2
int1 int2
string
str1 str2
str1 str2
str
str 0
str 0
file
filename directory
filename file
filename
filename
filename run
filename 0
!expr
exp1 a exp2
exp1 o exp2
Logical
exp
exp1 exp2
exp1 exp2
( if )
9 if 99
if ( vi )
Syntax
Example
if [ expression ]
then
commands
elif [ expression ]
then
commands
else
commands
fi
File testif.bash
text1 text2
YES, it equal No, it
not equal 9 if elif else
( case switch )
9 case 99 9 if 9
case ( vi )
Syntax
Example
case string in
str1)
commands;;
str2)
commands;;
str3)
commands;;
*)
commands;;
esac
File testcase.bash
text string 9 9
case 9 9
loop while
9 while 99
99
while ( vi )
Syntax
Example
while [ expression ]
do
commands
.
Done
File testwhile.bash
loop for
9 for 99 9 while 9
99 loop
for ( vi )
Syntax
Example
9 C 9
9