You are on page 1of 21

Shell Script 

 UNIX 4 Hardware, Kernel,


Shell Application
Application
Shell
Kernel
Hardware

Shell 9 UNIX 9 9 interface user UNIX


(Kernel) user 9 UNIX Shell  Shell
Shell Programming Language user 9 Shell
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

$ ls l filename $ chmod [ u / g / o ] [ + / - ] [ r / w / x] filename


 ls -l chmod

9 ls l hello.bash d 9 -rwxr-xr-x.
9  (x = executable 9 ).
shell script ( prompt )
Syntax
Example

$ . / filename
File hello.bash

shell script  ( prompt )


yntax
Example

$ . hello.bash $ bash hello.bash


File hello.bash

( variable )
9 9
9  

 ( vi )
Syntax
Example

variable = value

9 =
argument
( vi )
Syntax
Example

$ variable

99 $

9 Quote ( ) Double quote ( ) 


9 9 Quote ( ) Double quote
( ) 9
  ( vi )
Syntax
Example

variable=value or variable=value



Example

File testvariable.bash

Quote ( ) Double quote ( ) 9 9 9


Quote ( ) 9 9 9 Double quote ( )
9 9

( 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

 shell script shell


() shell shell  #!/bin/bash 9
file testenvironment.bash shell () 9
ENV VAR
VAR 9
9
9 . (dot) shell 9
testenvironment.bash 
9 9 export 9
99

 ( 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

 shell script Argument shell script


position parameter
Argument  shell script 
Argument script 
$*  position parameter
  error
0  0

process ID shell 
 
directory 
directory directory
 0 32767
 home directory

( 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

variable=(value value value value)


File testarray2.bash

index

test expression
bash ksh 9 test 9 expression
9
 test ( vi )
Syntax

test expression [ expression ]


Expression

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

Hello World 10 num


1  loop while 9 $num 10 loop while
Hello World 9 9 num
1 9 loop 9
Hello World 10

loop for
9 for 99 9 while 9
99 loop
 for ( vi )

Syntax

Example

for var in list


do
commands
.
Done
File testfor1.bash

Hello World 9 for  i


9 in 9  1 2 3 4 5 (
9 9 argument9 ) loop for
argument 9 in 9 1 2 3 4 5 9
9 Hello World
5 9  $i 9 echo
Hello World $i 9 Loop for 9

9 C 9
9

You might also like