You are on page 1of 54

SHELL

www.chinaunix.net

HAWK.Li

http://bbs.chinaunix.net/forum/24/20031209/218853.html

1) shell ..............................................................................................................2
2) shell prompt(PS1) Carriage Return(CR) ......................................................4
3) echo echo echo .............................................................6
4) " "() ' '() ..................................................................................9
5) var=valueexport ....................................................................................14
6) exec source ................................................................................................19
7) ( ) { } ..........................................................................................................23
8) $(( )) $( ) ${ } ...................................................................................25
9) $@ $* ........................................................................................................30
10) && || ........................................................................................................33
11) > < ............................................................................................................38
12) if case ................................................................................................47
13) for what? while until ................................................................................51

1) shell
shell
(FIXME)

"(Operating System)"
linux "
(kernel)"
kernel
kernel "" shell kernel
kernel shell
(FIXME)
shell (interface)
(command line)
shell ---(Command Interpreter)
*
*
(log in) shell
login shell primary shell
(process) shell shell
fork
(shell script)
shell (sub shell)
primary shell sub shell sub shell script

()
kernel shell

* kernel
* kernel shell
linux shell

/etc/shells
shell ""
shell
sh
burne shell (sh)
burne again shell (bash)
csh
c shell (csh)
2

tc shell (tcsh)
korn shell (ksh)
(FIXME)
Linux shell bash
*
*
bash gnu project Unix

2) shell prompt(PS1) Carriage Return(CR)

()
**(coursor)

**(prompt)
Linux

$
# root ()
shell prompt
* shell
shell prompt
cursor cursor

CR(Carriage Return Enter )


CR
* shell

* shell prompt CR
( CR Enter
)

command-name options argument


shell IFS(Internal Field Seperator) command line
""(word)
(meta) command line
()
IFS shell

* (White Space)
* (Tab)
* (Enter)
4

(command-name)
*
* (alias)
* (function)
* shell (built-in)
* $PATH

3) echo echo echo


command line echo

--- command line


* command_name option argument
echo Linux
* argument (STDOUT)(monitor)
(stdout
http://www.chinaunix.net/forum/viewtopic.php?t=191375 )
echo
:
$ echo
$
shell prompt
echo argument
(new-line charactor)
command argument ...
echo -n option
:
$ echo -n
$
command line echo
* command line command_name(echo) option(-n)
argument
echo argument
:
$ echo first line
first line
$ echo -n first line
first line $
echo argument
-n option
echo shell prompt
... ^_^
echo -n options
6

-e ()
-E()
-n ( -e \c )
echo
\aALERT / BELL ()
\bBACKSPACE
\c
\EESCAPE
\fFORMFEED
\nNEWLINE
\rRETURN
\tTAB
\vVERTICAL TAB
\nASCII ( x )
\\
( O'Reilly Learning the Bash Shell, 2nd Ed.)
echo

:
$ echo -e "a\tb\tc\nd\te\tf"
a
b
c
d
e
f
\t abc def \n def

:
$ echo -e "\141\011\142\011\143\012\144\011\145\011\146"
a
b
c
d
e
f
ASCII

:
$ echo -e "\x61\x09\x62\x09\x63\x0a\x64\x09\x65\x09\x66"
a
b
c
d
e
f
ASCII
7


:
$ echo -ne "a\tb\tc\nd\te\bf\a"
a
b
c
d
f$
e (\b) e
\a
-n shell prompt
-n \a \c
shell shell script echo

echo
:
$ A=B
$ echo $A
B
$ echo $?
0
()
command line echo
...

4) " "() ' '()


command line ...
shell prompt
Enter
command line shell

command line shell

()command line charactor

* literal shell
* meta shell
( bash shell command line
O'Reilly Learning the Bash Shell, 2nd Edition 177 - 180

178 Figure 7-1 ... )


Literal abcd123456 "" literal ... (easy)
meta ..... (confused?)
command line
meta
* IFS <space> <tab> <enter> ( space )
* CR <enter>
IFS command line (word) shell command line

CR command line <enter>

IFS CR meta
=
$ ( shell prompt )
> stdout
< stdin
|
& file descriptor
( ) nested subshell
{ } non-named function

;
&& true
|| false
! history
....
9

command line
quoting
bash quoting
* hard quote' ' () hard quote meta
* soft quote " " () soft quoe meta
( $ )()
* escape \ () escape () meta

( soft quote meta


)
quoting
:
$ A=B C # IFS
$ C: command not found. (FIXME)
$ echo $A
$ A="B C"
$ echo $A
BC

A command line
* A=B <IFS> C
A soft quote
IFS
* A=B<space>C
soft quote hard quote Enter

:
$ A='B
>C
>'
$ echo $A
B
C
<enter> hard quote CR

<enter> (new-line) command line


CR
shell prompt (PS2 > )command line
<enter> hard quote
command line CR shell
10

<enter> soft quote escape

:
$ A=B\
> C\
>
$ echo $A
B
C
<enter> <enter> escape
CR
<enter> CR command line
soft quote hard quote meta $

:
$ A=B\ C
$ echo "$A"
BC
$ echo '$A'
$A
echo $ soft quote

echo A "B C"


echo $ hard quote $
$
$ A $A
-------------------------------------
:
$ A=B\ C
$ echo '"$A"' #
"$A"
$ echo "'$A'" #
'B C'
( quoting ?#93;)
--------------------------------------

11

CU shell quoting
awk sed

* shell meta command meta


meta command line
{ } command line (
command block )
awk { } awk (BEGIN, MAIN, END)
command line
:
$ awk {print $0} 1.txt
{ } shell shell {print $0} command block
" ; " awk
hard quote
:
$ awk '{print $0}' 1.txt
hard quote {<space>$()} shell
meta
shell awk command meta
( $0 awk field number awk
awk $ )
hard quote soft quote escape
:
awk "{print \$0}" 1.txt
awk \{print\ \$0\} 1.txt

awk $0 0 shell
$A 0 command line awk $$A

hard quoe
:
$ awk '{print $$A}' 1.txt
$A $ hard quote
(!)

12

:
A=0
awk "{print \$$A}" 1.txt
awk \{print\ \$$A\} 1.txt
awk '{print $'$A'}' 1.txt
awk '{print $'"$A"'}' 1.txt

# "$A" soft quote

.... ^_^

13

5) var=valueexport
command line bash (variable)...
""(name)"
"(value)
*(set)*
bash "="
name=value

* (IFS) shell (meta


charactor)
* $
* (number)
* 256
* (case sensitive)

A= B IFS
1A=B
$A=B $
a=B a=b

A=" B" IFS ( quoting )


A1=B
A=$B $
This_Is_A_Long_Name=b _
*(substitution)*
Shell
(substitution)
$ ( =
)

:
$ A=ls
$ B=la
$ C=/tmp
$ $A -$B $C
( $ shell prompt )
command line (
14

command line )
command line ( CR
)
$ ()

:
ls -la /tmp
""

:
shell IFS(Internal Field Seperator) command line
""(word)
(meta) command line
$ command line meta
shell echo
:
$ echo $A -$B $C
echo argument ""(STDOUT
)

:
ls -la /tmp
echo $A(ls)$B(la) $C(/tmp)

shell
A=B
B=$A
B A ""
""
A=B
B=C
A C
A=B
B=$A
A=C
B C
A B B C
(""
^_^)
15

""(append)
A=B:C:D
A=$A:E
A "B:C:D"
"A:B:C:E"
( : )

A=BCD
A=$AE
A $AE $A E

A=BCD
A=${A}E
{}
A BCD BCDE
( ${name}

CU
http://www.chinaunix.net/forum/viewtopic.php?t=201843
)
* export *
shell ""(local variable)
export ""(environment variable)
:
$ A=B
$ export A

:
$ export A=B
export A
export shell ""(substitution)

:
$ A=B
16

$ B=C
$ export $A
A B
$A B "" export

export process
process
**
bash unset
:
unset A
export unset ( shell
)

:
$ A=B
$ B=C
$ unset $A
B A
unset

:
$ A=
$ unset A
A ""(null value) A

:
$ A=
$ echo $A
$ unset A
$ echo $A

17

null value unset

:
$ str=
# null
$ var=${str=expr}
# var
$ echo $var
$ echo $str
$ unset str
#
$ var=${str=expr}
# var
$ echo $var
expr
$ echo $str
expr
(yes, you!)
var=${str=expr} null unset

a.
b. var=${str=expr}
c.
e.
.... ^_^

18

6) exec source
CU Shell
( http://www.chinaunix.net/forum/viewtopic.php?t=194191 )

:
cd /etc/aa/bb/cc
shell shell

(process)
(parent process)
(child process)
Linux fork
( fork ... ^_^ )
(
)
""
*
""

(shell script)
shell script shell prompt
command line

script ""
script

(process + script)
* shell script sub-shell
sub-shell

:
cd /etc/aa/bb/cc
shell shell
19

:
shell script subshell
process parent process child process
child parent parent child

effective id, variable, workding dir ...


workding dir ($PWD)
subshell script sub shell $PWD cd
primary shell $PWD

^_^
source
fork source
* source script shell sub-shell

shell script

script source

script
:
./my.script

:
source ./my.script

. ./my.script

/etc
script
script
script ""... ^_^
okay fork source
20

---- exec source/fork


... exec File Descriptor ...

* exec script
exec source/fork
"+"

script 1.sh 2.sh


1.sh
:
#!/bin/bash
A=B
echo "PID for 1.sh before exec/source/fork:$$"
export A
echo "1.sh: \$A is $A"
case $1 in
exec)
echo "using exec..."
exec ./2.sh ;;
source)
echo "using source..."
. ./2.sh ;;
*)
echo "using fork by default..."
./2.sh ;;
esac
echo "PID for 1.sh after exec/source/fork:$$"
echo "1.sh: \$A is $A"
2.sh
:
#!/bin/bash
echo "PID for 2.sh: $$"
echo "2.sh get \$A=$A from 1.sh"
A=C
export A
echo "2.sh: \$A is $A"

21


:
$ ./1.sh fork
$ ./1.sh source
$ ./1.sh exec

CU
http://www.chinaunix.net/forum/viewtopic.php?t=191051
...
~~~
happy scripting! ^_^

22

7) ( ) { }
... ^_^
( ) { }
shell

2*(3+4) ...
""(command group)
shell command line ( ) { }

( ) command group sub-shell nested sub-shell


{ } shell non-named command group
fork source

command group
( ) { }

nested sub-shell
non-named command group
command line ( ) { } ~~~ ^_^
meta command meta ( Regular
Expression)
...
function
function command group
command group
non-named command group { }
(yes! ^_^ )
bash function

:
function function_name {
command1
command2
command3
....
}

23

:
fuction_name () {
command1
command2
command3
....
}
(Alias)

function ()
... ^_^
function ""
(library)
""...
shell
(shell script)
function command line function_name
script
shell function unset function_name
shell function
script function script
()
...
script function
script script ... ^_^
source
function
script source
RedHat Linux /etc/rc.d/init.d/functions
~~~ ^_^
okay ^_^

24

8) $(( )) $( ) ${ }
( ) { }
$( ) ${ }
bash shell $( ) ` ` ( ) (command
substitution)

:
$ echo the last sunday is $(date -d "last sunday" +%Y-%m-%d)
... ^_^
$( ) ` ` "" $( )
1, ` ` ' ' ( )
()

^_^
2, ` ` ( \` ) $( )

:
command1 `command2 `command3` `
command2 `command3` command3
command 2
command1 `command2 ...`
`command2 ` ``

:
command1 `command2 \`command3\` `

$( )
:
command1 $(command2 $(command3))
~~~ ^_^

25

$( ) ...
` ` unix shell shell script

$( ) shell bash2
... ^_^
${ } ...
$var ${var}
${ }
:
$ A=B
$ echo $AB

$A B
AB ...
${ }
:
$ echo ${A}B
BB

${ } bash

cu
http://www.chinaunix.net/forum/viewtopic.php?t=201843
${ }

file=/dir1/dir2/dir3/my.file.txt
${ }
${file#*/} / dir1/dir2/dir3/my.file.txt
${file##*/} / my.file.txt
${file#*.} . file.txt
${file##*.} . txt
${file%/*} / /dir1/dir2/dir3
${file%%/*} / ()
${file%.*} . /dir1/dir2/dir3/my.file
${file%%.*} . /dir1/dir2/dir3/my

# ( # $ )
% ( % $ )
26


${file:0:5} 5 /dir1
${file:5:5} 5 5 /dir2

${file/dir/path} dir path/path1/dir2/dir3/my.file.txt


${file//dir/path} dir path/path1/path2/path3/my.file.txt
${ } ()
${file-my.file.txt} $file my.file.txt (
)
${file:-my.file.txt} $file my.file.txt
()
${file+my.file.txt} $file my.file.txt (
)
${file:+my.file.txt} $file my.file.txt (
)
${file=my.file.txt} $file my.file.txt $file
()
${file:=my.file.txt} $file my.file.txt
$file ()
${file?my.file.txt} $file my.file.txt STDERR (
))
${file:?my.file.txt} $file my.file.txt
STDERR ()
${#var}
${#file} 27 /dir1/dir2/dir3/my.file.txt 27 ...
bash (array)
A="a b c def" $A
A=(a b c def) $A ...
bash
${A[@]} ${A[*]} a b c def ()
${A[0]} a ()${A[1]} ...
${#A[@]} ${#A[*]} 4 ()
${#A[0]} 1 ((a))${A[3]} 3 (
(def))
A[3]=xyz 4 xyz ...
....
bash $( ) ${ } shell
~~~ ^_^
27

$(( ))
bash $(( ))
+ - * / ""
%
& | ^ ! "ANDORXORNOT"

:
$ a=5; b=7; c=2
$ echo $(( a+b*c ))
19
$ echo $(( (a+b)/c ))
6
$ echo $(( (a*b)%c))
1

$(( )) $
$(( $a + $b * $c)) 19
$(( )) ()

echo $((16#2a)) 42 (16 )

umask 022
:
$ umask 022
$ echo "obase=8;$(( 8#666 & (8#777 ^ 8#$(umask)) ))" | bc
644

(( )) testing
a=5; ((a++)) $a 6
a=5; ((a--)) a=4
a=5; b=7; ((a < b)) 0 (true)
(( ))
<
>
<=
>=
==
!=
28

(( )) [ ] (
)
.. ^_^ okay...
...

29

9) $@ $*
$@ $* shell script positional parameter ...
(variable)
shell

positional parameter
shell script $0, $1, $2, $3 ...

:
script_name parameter1 parameter2 parameter3 ...
$0 shell script () $1
....
IFS IFS quoting positional
parameter

:
my.sh p1 "p2 p3" p4
p2 p3 soft quote my.sh $2
"p2 p3" $3 p4 ...
fucntion script script
^_^
function ( script ) postitional parameter
$0
my.sh fucntion my_fun , script
my_fun fp1 fp2 fp3
function $0 my.sh $1 fp1 p1 ...
my.sh script
:
#!/bin/bash
my_fun() {
echo '$0 inside function is '$0
echo '$1 inside function is '$1
echo '$2 inside function is '$2
}
echo '$0 outside function is '$0
echo '$1 outside function is '$1
30

echo '$2 outside function is '$2


my_fun fp1 "fp2 fp3"
command line script
:
chmod +x my.sh
./my.sh p1 "p2 p3"
$0 outside function is ./my.sh
$1 outside function is p1
$2 outside function is p2 p3
$0 inside function is ./my.sh
$1 inside function is fp1
$2 inside function is fp2 fp3

positional parameter
* $10 10 ($1) 0

my.sh one two three four five six seven eigth nine ten command
line
my.sh $10 ten one0 ...
ten
${ } ${10}
shift
shift positional parameter
( $0 )
1 shift shift 1 $1 $2
$1$3 $2 ...
shift 3 $4 $1 ...
shift $1 ${10} ^_^
okay positional parameter

$# positional parameter
my.sh p1 "p2 p3"
p2 p3 IFS soft quote $# 2
p2 p3 quoting $# 3
function ...
shell script script
:
31

[ $# = 0 ]
0 script ...
$@ $*
soft quote ""( $0
)

command line my.sh p1 "p2 p3" p4


$@ $* p1 p2 p3 p4
soft quote
"$@" "p1" "p2 p3" "p4" (word)
"$*" "p1 p2 p3 p4"
my.sh
:
#!/bin/bash
my_fun() {
echo "$#"
}
echo 'the number of parameter in "$@" is '$(my_fun "$@")
echo 'the number of parameter in "$*" is '$(my_fun "$*")

./my.sh p1 "p2 p3" p4 $@ $* ... ^_^

32

10) && ||
... ^_^
return value
shell command function
return value
shell command line $? "" return value

Return Value(RV) 0-255 ( script)


* script exit RV
RV
* function return RV exit RV
Return Value (exit status)
* 0 ""( true )
* 0 ""( false )

my.file no.file
:
$ touch my.file
$ ls my.file
$ echo $? # first echo
0
$ ls no.file
ls: no.file: No such file or directory
$ echo $? # second echo
1
$ echo $? # third echo
0
echo ls my.file RV 0 true
echo ls no.file RV 0 false
echo echo $? RV 0 true
command return value
...
"" return value true
false
test
bash command line man test man bash
test
...
33

man
test expression
:
test expression
or:
[ expression ]
( [ ] )
(...)
bash test
* string
* integer( 0 )
* file
test expression

A=123
* [ "$A" = 123 ] $A 123 "
"
* [ "$A" -eq 123 ] $A ""
* [ -e "$A" ] 123 ""
expression ""test 0 (true) return value
0 (false)
expression " ! "() expression ""
0 0
test
* expression1 -a expression2 exrepssion true 0
0
* expression1 -o expression2 exrepssion true 0
false 0

:
[ -d "$file" -a -x "$file" ]
$file x test true
command line test ""
meta meta (
)
test expression

34

[ string1 = string2 ] test


= (null)( soft quote hard quote
)
$A
:
$ unset A
$ [ $A = abc ]
[: =: unary operator expected
$ meta $A

[ = abc ]
= test

:
$ [ "$A" = abc ]
$ echo $?
1

[ "" = abc ]
= soft quote test ...
test

test test "


"
* test soft quote
quoting ... ^_^
okay test man page ^_^
.... ... return value

return value shell ""

* return value shell ...


~~~ ^_^
&& || "" command line
* command1 && command2 command2 RV 0 (true)

* command1 || command2 command2 RV 0 (false)


35

:
$ A=123
$ [ -n "$A" ] && echo "yes! it's ture."
yes! it's ture.
$ unset A
$ [ -n "$A" ] && echo "yes! it's ture."
$ [ -n "$A" ] || echo "no, it's NOT ture."
no, it's NOT ture.
([ -n string ] string 0 true )
&& echo
test 0 RV
test 0 ...
|| echo test 0
&& ||
:
$ A=123
$ [ -n "$A" ] && echo "yes! it's ture." || echo "no, it's NOT ture."
yes! it's ture.
$ unset A
$ [ -n "$A" ] && echo "yes! it's ture." || echo "no, it's NOT ture."
no, it's NOT ture.
shell "" ^_^

$A 100 too big!


:
$ A=123
$ [ -n "$A" ] && [ "$A" -lt 100 ] || echo 'too big!'
too big!
A ()...
:
$ unset A
$ [ -n "$A" ] && [ "$A" -lt 100 ] || echo 'too big!'
too big!

36

( command group ...)


....

37

11) > <


CU shell
http://bbs.chinaunix.net/forum/24/20031030/191375.html
""...
-------------11.1
I/O redirection File Descriptor (FD)
(data)

file descriptor (FD)


shell FD
0: Standard Input (STDIN)
1: Standard Output (STDOUT)
2: Standard Error Output (STDERR)
FD (device)
stdin(0): keyboard
stdout(1): monitor
stderr(2): monitor

:
$ mail -s test root
this is a test mail.
please skip.
^d ( crtl d )
mail stdin keyboard
stdin mail keyboard
stdin
:
$ cat /etc/passwd
cat
.... ^_^
:
$ cat

38

( ^d ...)
stdout stderr ... ... ^_^

-------------11.2
... ^_^
stdin stdout
stderr
stderr ""...
monitor
:
$ ls no.such.file
ls: no.such.file: No such file or directory

stdout stderr
monitor
:
$ touch my.file
$ ls my.file no.such.file
ls: no.such.file: No such file or directory
my.file

okay FD
FD
< (stdin)
> (stdout, stderr)

:
$ cat < my.file
my.file
:
$ mail -s test root < /etc/passwd
/etc/passwd ...
stdin keyboard ...
< FD ()
39

0 < < 0<


okay
<<
HERE Document <<

:
$ cat <<FINISH
first line here
second line there
third line nowhere
FINISH
cat 3 keyboard ^d

>
....
-------------11.3
okay~~~
0< stdin
redirection
* 1>
* 2>
stdout stderr
monitor
1 > 1> > stdout
ls
:
$ ls my.file no.such.file 1>file.out
ls: no.such.file: No such file or directory
monitor stderr stdout file.out
:
$ ls my.file no.such.file 2>file.err
my.file
40

monitor stdout stderr file.err


:
$ ls my.file no.such.file 1>file.out 2>file.err
monitor stdout stderr ...
~~~ > ^_^

file locking
:
$ ls my.file no.such.file 1>file.both 2>file.both
file system FD
stdout(1) stderr(2) file.both
""
"" stdout stderr file.out

* 1, 2, 3 stdout
* 3, 4, 5 stderr
stderr 3
stderr stdout stdout stderr

FD ""...

stderr stdout stdout sterr

bingo
* 2>&1 stderr stdout
* 1>&2 >&2 stdout stderr

:
$ ls my.file no.such.file 1>file.both 2>&1

$ ls my.file no.such.file 2>file.both >&2

~~~ ^_^
locking
...
41

-------------11.4
okay I/O Redirction ...
(...) ~~~ ^_^
""
...
""
---
""~~~
Linux /dev/null
""
null ....
~~~ ^_^
null I/O Redirection
* FD1 FD2 /dev/null stdout stderr
* FD0 /dev/null nothing
stdout stderr
stderr ()
:
$ ls my.file no.such.file 2>/dev/null
my.file

stderr stdout null


:
$ ls my.file no.such.file >/dev/null
ls: no.such.file: No such file or directory

... ^_^
>/dev/null 2>&1
:
$ ls my.file no.such.file &>/dev/null
( &> >& ~~! )
okay
:
42

$ echo "1" > file.out


$ cat file.out
1
$ echo "2" > file.out
$ cat file.out
2
stdout stderr

~~~ > >>


:
$ echo "3" >> file.out
$ cat file.out
2
3

easy ... ^_^


> ""

---- yes ....

:
$ set -o noclobber
$ echo "4" > file.out
-bash: file: cannot overwrite existing file

""
set -o set +o
:
$ set +o noclobber
$ echo "5" > file.out
$ cat file.out
5

... ""

43

~~~ ~~~ ^_^


:
$ set -o noclobber
$ echo "6" >| file.out
$ cat file.out
6
> " | "( > | )....
.... ()~~~ ^_^

:
$ echo "some text here" > file
$ cat < file
some text here
$ cat < file > file.bak
$ cat < file.bak
some text here
$ cat < file > file
$ cat < file

---- cat file


why? why? why?
~~~!

-------------11.5
~~~ ~~~ ^_^
$ cat < file > file
priority
* IO Redirection stdout stderr stdin

> file file < file


...
~~~ ~~~~ ^_^
...
:
$ cat <> file
44

$ cat < file >> file

...
I/O Redirection sorry~~~
~~ ^_^
(~!#@!$%)
---- pipe line
pipe line
command line " | " pipe line
pipe line
... pipe
""...

input output

pipe line I/O I/O


* stdout stdin
... command line pipe line
command I/O ( ^_^ )
... ... ... ... stderr

*
pipe line stderr stdin

2> file
pipe line

* stderr stdin
()
^_^

* stderr stdout
... (...)

* cm1 | cm2 | cm3 ... pipe line cm2

cm1 | cm2 > file | cm3


45

cm3 stdin (
)

:
cm1 | cm2 > file ; cm3 < file
file I/O
command file I/O
shell file I/O

tee
* tee I/O stdout

:
cm1 | cm2 | tee file | cm3
tee -a

pipe line shell text filtering

cat, more, head, tail, wc, expand, tr, grep, sed, awk, ...
pipe line command line
""... ^_^
....
I/O Redirection
shell bye... ^_^

46

12) if case
...
~~~
10 return value

...
return value && ||
command group shell script

:
comd1 && {
comd2
comd3
} || {
comd4
comd5
}

comd1 return value true


comd3 comd4
comd4 comd5

shell script

&& || ""

if .... then ... else ... keyword

bash shell
:
if comd1
then
comd2
comd3
else
comd4
47

comd5
fi
shell script if
if command line true return value ( test
return value)
then else fi
keyword

if else then
( then command " " null command )
then else shell script

" " elif


keyword
:
if comd1; then
comd2
elif comd3; then
comd4
else
comd5
fi

comd1 true comd2


comd3 comd4
comd1 comd3 comd5

if shell script
...
case
if

string
:
QQ () {
echo -n "Do you want to continue? (Yes/No): "
read YN
48

if [ "$YN" = Y -o "$YN" = y -o "$YN" = "Yes" -o "$YN" = "yes" -o "$YN" =


"YES" ]
then
QQ
else
exit 0
fi
}
QQ
YN

:
...
if echo "$YN" | grep -q '^[Yy]\([Ee][Ss]\)*$'
...
Regular Expression ( RE)
...
case
:
QQ () {
echo -n "Do you want to continue? (Yes/No): "
read YN
case "$YN" in
[Yy]|[Yy][Ee][Ss])
QQ
;;
*)
exit 0
;;
esac
}
QQ
case ( string)

script
Linux /etc/init.d/* script
case

:
case "$1" in
start)
49

start
;;
stop)
stop
;;
status)
rhstatus
;;
restart|reload)
restart
;;
condrestart)
[ -f /var/lock/subsys/syslog ] && restart || :
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart}"
exit 1
esac
( positional parameter 9 )
okay.... ^_^

50

13) for what? while until


shell ... ~~~~
shell script ""(loop)
loop script
bash shell loop
* for
* while
* until
for loop "" do done

:
for var in one two three four five
do
echo ----------echo '$var is '$var
echo
done

1) for var one two three four five


2) 5 do done 5
3) echo
hard quote $var one two three four
five
4)

for loop

for loop in keyword


$@ ( $* )
:
for var; do
....
done
( positional parameter 9 ...)
for loop ""(list)
51

positional parameter
... ("")
""()for
:
for ((i=1;i<=10;i++))
do
echo "num is $i"
done

for loop while loop


:
num=1
while [ "$num" -le 10 ]; do
echo "num is $num"
num=$(($num + 1))
done

while loop for loop


while return
value
* ture do done while return
value
* false do done

1) while num=1
2) (test) $num 10
3) true echo num
4) num 1+1=2 10
true
5) num 10+1=11 ...

* while true
:
while :; do
echo looping...
done

52

" : " bash null command true return


value

( daemon)
signal ( ctrl-c )
( process signal )
while loop until loop
* while until return value false
until
:
num=1
until [ ! "$num" -le 10 ]; do
echo "num is $num"
num=$(($num + 1))
done

:
num=1
until [ "$num" -gt 10 ]; do
echo "num is $num"
num=$(($num + 1))
done

okay bash loop


loop
* break
* continue
do ... done
loop
... ^_^
break ""
break n "" n
break 1
break return exit
* break loop
* return function
* exit script/shell
continue break
continue done
53

...
break continue n (
)
continue 1
shell script loop script

....

----------
shell
""""...
shell ""

""""

--- ^_^

p.s.

1)
2)
Good luck and happy studying!

54

You might also like