shell script command
DESCRIPTION
Shell Script commandTRANSCRIPT
-
Application Shell
Kernel
Hardware
Shell Script
UNIX 4 Hardware, Kernel, Shell Application Shell 9 UNIX 99 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 $ vi filename
Example shell script vi ( hello.bash)
99 9 ls l hello.bash (9 hello.bash) d rw-r-xr-x 9 9 9 chmod
( prompt )
Syntax $ ls l filename $ chmod [ u / g / o ] [ + / - ] [ r / w / x] filename
Example ls -l chmod
-
9 ls l hello.bash d 9 -rwxr-xr-x. 9 (x = executable 9).
shell script ( prompt )
Syntax $ . / filename
Example File hello.bash
shell script ( prompt )
yntax $ . hello.bash $ bash hello.bash
Example File hello.bash
-
( variable )
99 9
( vi )
Syntax variable = value
Example
9 = argument
( vi )
Syntax $ variable
Example
99$
-
9 Quote ( ) Double quote ( ) 9 9 Quote ( ) Double quote ( ) 9
( vi )
Syntax variable=value or variable=value
Example
Example File testvariable.bash
-
Quote ( ) Double quote ( ) 99 9 Quote ( ) 99 9 Double quote ( ) 99
-
( environment variable )
9 1. 2. (environment variable) 9 9 9
9 export.
( vi )
Syntax variable = value
Example File testenvironment.bash
-
( prompt )
Syntax $ export variable
Example ENV prompt
shell script shell () shell shell #!/bin/bash 9 file testenvironment.bash shell () 9 ENV VAR VAR 99
9 . (dot) shell 9 testenvironment.bash
9 9 export 9 99
-
( prompt )
Syntax $ export
9999 option n 9 export
( prompt )
Syntax $ export n variable
( environment variable )
99(read-only)
$0 $1 $2 .... shell script Argument shell script
position parameter
$# Argument shell script
$* Argument script
$@ $* position parameter
$? error
0 0
$! process ID shell
$hostname
$pwd directory
$oldpwd directory directory
$randow 0 32767
$home home directory
-
( comment )
9 # () script shell 99 #
# comment ( vi )
Syntax # text
Example File testcomment.bash
99 # 9 9 9 comment
-
( array variable )
array index 9 0 C 9 99 { } 9
array ( vi )
Syntax variable[index]=value
Example File testarray.bash
-
array ( vi )
Syntax variable=(value value value value)
Example File testarray2.bash
index
-
test expression
bash ksh 9 test 9 expression 9
test ( vi )
Syntax test expression [ expression ]
Expression
int1 eq int2 int1 int2
int1 ne int2 int1 int2
int1 gt int2 int1 int2
int1 ge int2 int1 int2
int1 le int2 int1 int2
int1 lt int2 int1 int2
string
str1 = str2 str1 str2
str1 != str2 str1 str2
Str str
-n str str 0
-z str str 0
file
-d filename filename directory
-f filename filename file
-r filename filename
-w filename filename
-x filename filename run
-s filename filename 0
-
Logical
!expr exp
exp1 a exp2 exp1 exp2
exp1 o exp2 exp1 exp2
( if )
9 if 99
if ( vi )
Syntax
if [ expression ]
then
commands
elif [ expression ]
then
commands
else
commands
fi
Example File testif.bash
-
text1 text2 YES, it equal No, it not equal 9 if elif else
( case switch )
9 case 999 if 9
case ( vi )
Syntax
case string in
str1)
commands;;
str2)
commands;;
str3)
commands;;
*)
commands;;
esac
Example File testcase.bash
-
text string 99 case 99
-
loop while
9 while 99 99
while ( vi )
Syntax
while [ expression ]
do
commands
.
Done
Example 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 999 loop
for ( vi )
Syntax
for var in list
do
commands
.
Done
Example 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 99