Вы находитесь на странице: 1из 61

1

Shell Scripting
Yao-Yuan Chuang
2
Outline

What is shell?

Basic

Syntax

Lists

Functions

Command Execution

Here Documents

Deug

!egular Ex"ression

Find
3
Why Shell?

#he commercial $%&' used (orn Shell

For Linux) the Bash is the de*ault

Why Shell?

For routing +os) such as system administration) ,ithout


,riting "rograms

Ho,e-er) the shell scri"t is not e**icient) there*ore) can e


used *or "rototy"ing the ideas

For exam"le)
% ls al | more (better format of listing
directory)
% man bash | col b | lpr (print man page of man)
4
What is Shell?

Shell is the inter*ace et,een end user


and the Linux system) similar to the
commands in Windo,s

Bash is installed as in .in.sh

Chec/ the -ersion


% /bin/sh --version
(ernel
0ther
"rograms
' ,indo,
ash
csh
5
Pipe and Redirection

!edirection 12 or 34
% ls l > lsoutput.txt (save output to lsoutput.txt)
% ps >> lsoutput.txt (append to lsoutput.txt)
% more < killout.txt (use killout.txt as parameter to
more)
% kill -l 1234 > killouterr.txt 2 >1 (redirect to the
same file)
% kill -l 1234 >/!ev/null 2 >1 (ignore std output)

5i"e 164

5rocess are executed concurrently


% ps | sort | more
% ps xo comm | sort | uni" | #rep v sh | more
% cat m$!ata.txt | sort | uni" | > m$!ata.txt
(generates an empty file !)
6
Shell as a Language

We can ,rite a scri"t containing many shell commands

&nteracti-e 5rogram7

gre" *iles ,ith 50S&' string and "rint it


% %or %ile in &
> !o
> i% #rep l '()*+ ,%ile
> then
> more ,%ile

%i

!one
Posix
There is a file with PO!" in it

89: is ,ildcard
% more -#rep l '()*+ &-
% more ,.#rep l '()*+ &/
% more l '()*+ & | more
7
Writing a Script

$se text editor to generate the ;*irst< *ile


#!$bin$sh
# first
# this file looks for the files containing PO!"
# and print it
for file in %
do
if grep &' PO!" (file
then
echo (file
fi
done
exit )
% /bin/sh %irst
% chmo! 0x %irst
%./%irst (make sure . is include in P*T+ parameter)
exit code) = means success*ul
8
Syntax

>ariales

Conditions

Control

Lists

Functions

Shell Commands

!esult

Document
9
Variables

>ariales needed to e declared) note it is case-sensiti-e


1e?g? *oo) F00) Foo4

@dd 8A: *or storing -alues


% salutation12ello
% echo ,salutation
+ello
% salutation1304
% echo ,salutation
,-.
% salutation15$es !ear6
% echo ,salutation
yes dear
% rea! salutation
2ola7
% echo ,salutation
+ola!
10
Quoting

Edit a ;-artest?sh< *ile


#!$bin$sh
myvar/0+i there1
echo (myvar
echo 0(myvar1
echo 2(myvar2
echo 3(myvar
echo 4nter some text
read myvar
echo 5(myvar6 now e'uals (myvar
exit )
Output
+i there
+i there
(myvar
(myvar
4nter some text
2ello 8orl!
(myvar now e'uals +ello world
11
Environment Variables

AH0BE home directory

A5@#H "ath

A5S1 1normally C4

A5SD 1normally 34

AA "rocess id o* the scri"t

AE numer o* in"ut "arameters

A= name o* the scri"t *ile

A&FS se"aration character 1,hite s"ace4

$se 8en-: to chec/ the -alue


12
Parameter
% *9) 1 - -
% set %oo bar bam
% echo 5,:6
foo bar bam
% echo 5,&6
foo bar bam
% unset *9)
% echo 5,&6
foo bar bam
doesn:t matter &FS
13
Parameter
7./tr$;var %oo bar ba<
+ello
The program .$try8var is now running
The second parameter was bar
The first parameter was foo
The parameter list was foo bar ba9
The user6s home directory is $home$ychuang
Please enter a new greeting
2ola
+ola
The script is now complete
Edit *ile 8tryF-ar:
#!$bin$sh
salutation/0+ello1
echo (salutation
echo 0The program () is now running1
echo 0The parameter list was (%1
echo 0The second parameter was (:1
echo 0The first parameter was (;1
echo 0The user6s home directory is (+O<41
echo 0Please enter a new greeting1
read salutation
echo (salutation
echo 0The script is now complete1
exit )
14
ondition

test or 8 G 8
if test &f fred.c
then
...
fi
!f = >f
fred.c ?
then
...
fi
if = >f fred.c ?@then
...
fi
expression; &e' expression:
expression; &ne expression:
expression; &gt expression:
expression; &ge expression:
expression; >lt expression:
expression; &le expression:
!expression
>d file if directory
>e file if exist
>f file if file
>g file if set>group>id
>r file if readable
>s file if si9e A)
>u file if set>user>id
>w file if writable
>x file if executable
tring; / string:
tring; !/ string :
>n string (if not empty string)
>9 string (if empty string)
need s"ace H
15
ontrol Structure
Syntax
i% condition
then
statement
else
statement
%i
#!$bin$sh
echo 0!s it morningB Please answer yes or no1
read timeofday
if = (timeofday / 0yes1 ?@ then
echo 0Cood morning1
else
echo 0Cood afternoon1
fi
exit )
!s it morningB Please answer yes or no
$es
Cood morning
16
ondition Structure
#!$bin$sh
echo 0!s it morningB Please answer yes or no1
read timeofday
if = (timeofday / 0yes1 ?@ then
echo 0Cood morning1
elif = (timeofday / 0no1 ?@ then
echo 0Cood afternoon1
else
echo 0orryD (timeofday not recongni9ed. 4nter yes or no1
exit ;
fi
exit )
17
ondition Structure
#!$bin$sh
echo 0!s it morningB Please answer yes or no1
read timeofday
if = 0(timeofday1 / 0yes1 ?@ then
echo 0Cood morning1
elif = (timeofday / 0no1 ?@ then
echo 0Cood afternoon1
else
echo 0orryD (timeofday not recongni9ed. 4nter yes or no1
exit ;
fi
exit )
&* in"ut ;enter< still returns Iood morning
18
Loop Structure
Syntax
%or variable
!o
statement
!one
#!$bin$sh
for foo in bar fud EF
do
echo (foo
done
exit )
bar
fud
EF
Ho, to out"ut as ar *ud JK?
#ry change *or *oo in ;ar *ud JK<
#his is to ha-e s"ace in -ariale
19
Loop Structure

$se ,ildcard 89:


#!$bin$sh
for file in ((ls f%.sh)@ do
lpr (file
done
exit )
5rint all *9?sh *iles
20
Loop Structure
Syntax
8hile condition
!o
statement
!one
#!$bin$sh
for foo in ; : F E . G , H I ;)
do
echo 0here we go again1
done
exit )
#!$bin$sh
foo / ;
while = 0(foo1 &le ;) ?
do
echo 0here we go again1
foo / (foo(((foo-;))
done
exit )
Syntax
until condition
!o
statement
!one
%ote7 condition is
!e-erse to ,hile
Ho, to re-,rite
"re-ious sam"le?
21
ase Statement
Syntax
case variable in3
pattern = | pattern > ?/ statement@@
pattern = | pattern > ?/ statement@@
J
esac
#!$bin$sh
echo 0!s it morningB Please answer yes or no1
read timeofday
case 0(timeofday1 in
yes) echo 0Cood <orning1@@
y) echo 0Cood <orning1@@
no) echo 0Cood *fternoon1@@
n) echo 0Cood *fternoon1@@
% ) echo 0orryD answer not recongni9ed1@@
esac
exit )
22
ase Statement

@ much ;cleaner< -ersion


#!$bin$sh
echo 0!s it morningB Please answer yes or no1
read timeofday
case 0(timeofday1 in
yes K y K Les K L4 ) echo 0Cood <orning1@@
n% K M% ) echo 0Cood *fternoon1@@
% ) echo 0orryD answer not recongni9ed1@@
esac
exit )
But this has a "rolem) i* ,e enter 8ne-er: ,hich oeys n9
case and "rints ;Iood @*ternoon<
23
ase Statement
#!$bin$sh
echo 0!s it morningB Please answer yes or no1
read timeofday
case 0(timeofday1 in
yes K y K Les K L4 )
echo 0Cood <orning1
echo 0Np bright and early this morning1
@@
=nM?%)
echo 0Cood *fternoon1@@
%)
echo 0orryD answer not recongni9ed1
echo 0Please answer yes of no1
exit ;
@@
esac
exit )
24
List

@%D 1LL4
statement1 && statementD && statementK M
#!$bin$sh
touch file8one
rm &f file8two
if = >f file8one ? OO echo 0+ello1 OO =>f file8two? OO echo 0 there1
then
echo 0in if1
else
echo 0in else1
fi
exit )
Output
+ello
in else
Chec/ i* *ile exist i* not then create one
!emo-e a *ile
25
List

0! 1664
statement1 || statementD || statementK M
#!$bin$sh
rm &f file8one
if = >f file8one ? KK echo 0+ello1 KK echo 0 there1
then
echo 0in if1
else
echo 0in else1
fi
exit )
Output
+ello
in else
26
Statement !loc"

$se multi"le statements in the same "lace


get8comfirm OO P
grep &v 0(cdcatnum1 (stracks8file A (temp8file
cat (temp8file A (tracks8file
echo
add8record8tracks
Q
27
#unction

You can de*ine *unctions *or ;structured< scri"ts


function8name() P
statements
Q
#!$bin$sh
foo() P
echo 0Runction foo is executing1
Q
echo 0script starting1
foo
echo 0script ended1
exit )
Output
script starting
Runction foo is executing
cript ended
You need to de*ine a *unction e*ore using it
#he "arameters A9)AN)AE)A1)AD are re"laced y local -alue
i* *unction is called and return to "re-ious a*ter *unction is *inished
28
#unction
#!$bin$sh
sample8text/0global variable1
foo() P
local sample8text/0local variable1
echo 0Runction foo is executing1
echo (sample8text
Q
echo 0script starting1
echo (sample8text
foo
echo 0script ended1
echo (sample8text
exit )
de*ine local
-ariale
Output?
Check the
scope of
the
variables
29
#unction

$se return to "ass a result


#!$bin$sh
yes8or8no() P
echo 0!s your name (% B1
while true
do
echo &n 04nter yes or noS1
read x
case 0(x1 in
y K yes ) return )@@
n K no ) return ;@@
% ) echo 0*nswer yes or no1
esac
done
Q
echo 0Original parameters are (%1
if yes8or8no 0(;1
then
echo 0+i (;D nice name1
else
echo 0Mever mind1
fi
exit )
Output
./m$;name Aohn Bhuan#
Original parameters are Tohn Uhuang
!s your name TohnB
4nter yes or noS $es
+i TohnD nice name.
30
ommand

External7
use interacti-ely

&nternal7

only in scri"t

break
s/i" loo"
#!$bin$sh
rm &rf fred%
echo A fred;
echo A fred:
mkdir fredF
echo A fredE
for file in fred%
do
if = >d 0(file1 ? @ then
break@
fi
done
echo first directory starting fred was (file
rm &rf fred%
exit )
31
ommand

7 treats it as true
#!$bin$sh
rm &f fred
if = >f fred ?@ then
S
else
echo file fred did not exist
fi
exit )
32
ommand

continue continues next iteration


#!$bin$sh
rm &rf fred%
echo A fred;
echo A fred:
mkdir fredF
echo A fredE
for file in fred%
do
if = >d 0(file1 ?@ then
echo 0skipping directory (file1
continue
fi
echo file is (file
done
rm &rf fred%
exit )
33
ommand

? ?.shellFscri"t execute shellFscri"t


classic;set
#!$bin$sh
verion/classic
P*T+/$usr$local$old8binS$usr$binS$binS.
P;/0classicA 1
latest;set
#!$bin$sh
verion/latest
P*T+/$usr$local$new8binS$usr$binS$binS.
P;/0latest versionA 1
% . ./classic_set
classic3 echo $version
classic
Classic3 . latest_set
latest
latest -ersion3
34
ommand

echo "rint string

-n do not out"ut the trailing ne,line

-e enale inter"retation o* ac/slash esca"es

O=%%% the character ,hose @CS&& code is %%%

OO ac/slash

Oa alert

O ac/s"ace

Oc su""ress trailing ne,line

O* *orm *eed

On ne,line

Or carriage return

Ot horiPontal ta

O- -ertical ta
#ry these
7 echo &n 0string to 3n output1
7 echo &e 0string to 3n output1
35
ommand

e-al e-aluate the -alue o* a "arameter


similar to an extra 8A:
7 foo/;)
7 x/foo
7 y/5(6(x
7 echo (y
0ut"ut is A*oo
7 foo/;)
7 x/foo
7 eval y/5(6(x
7 echo (y
0ut"ut is 1=
36
ommand

exit n ending the scri"t

= means success

1 to DQQ means s"eci*ic error code

1DR means not executale *ile

1DS means no such command

1DT or 31DT signal


#!$bin$sh
if = >f .profile ?@ then
exit )
fi
exit ;
Or 7 = >f .profile ? OO exit ) KK exit ;
37
ommand

ex"ort gi-es a -alue to a "arameter


#his is 8ex"ortD:
#!$bin$sh
echo 0(foo1
echo 0(bar1
#his is 8ex"ort1:
#!$bin$sh
foo/0The first meta>syntactic variable1
export bar/0The second meta>syntactic variable1
export:
0ut"ut is
7export1
The second>syntactic variable
7
38
ommand

ex"r e-aluate ex"ressions


7x/2expr (x - ;2 (@ssign result -alue ex"r AxU1 to x4
@lso can e ,ritten as
7x/((expr (x - ;)
Ex"r1 6 ex"rD 1or4 ex"r1 HV ex"rD
Ex"r1 L ex"rD 1and4 ex"r1 U ex"rD
Ex"r1 V ex"rD ex"r1 W ex"rD
Ex"r1 3 ex"rD ex"r1 9 ex"rD
Ex"r1 3V ex"rD ex"r1 . ex"rD
Ex"r1 2 ex"rD ex"r1 C ex"rD 1module4
Ex"r1 2V ex"rD
39
ommand

"rint* *ormat and "rint data

Esca"e seXuence

OO ac/slash

Oa ee" sound

O ac/s"ace

O* *orm *eed

On ne,line

Or carriage return

Ot ta

O- -ertical ta

Con-ersion s"eci*ier

Cd decimal

Cc character

Cs string

CC "rint C
% print% 5%sCn6 hello
+ello
% print% 5%s %!Ct%s6 52i
Dhere6 14 people
+i There ;. people
40
ommand

return return a -alue

set set "arameter -ariale


#!$bin$sh
echo the date is ((date)
set ((date)
echo The month is (:
exit )
41
ommand

Shi*t shi*t "arameter once) AD to A1) AK to



AD) and so on
#!$bin$sh
while = 0(;1 !/ 01 ?@ do
echo 0(;1
shift
done
exit )
42
ommand

tra" action a*ter recei-ing signal


trap command signal

signal ex"lain
H$5 114 hung u"
&%# 1D4 interru"t 1Crtl U C4
Y$&# 1K4 Yuit 1Crtl U O4
@B!# 1R4 @ort
@L!B 11J4 @larm
#E!B 11Q4 #erminate
43
ommand
#!$bin$sh
trap 5rm &f $tmp$my8tmp8file8((6 !MT
echo creating file $tmp$my8tmp8file8((
date A $tmp$my8tmp8file8((
echo 0press interrupt (UTVW>U) to interrupt J1
while = >f $tmp$my8tmp8file8(( ?@ do
echo Rile exists
sleep ;
done
echo The file no longer exists
trap !MT
echo creating file $tmp$my8tmp8file8((
date A $tmp$my8tmp8file8((
echo 0press interrupt (UTVW>U) to interrupt J1
while = >f $tmp$my8tmp8file8(( ?@ do
echo Rile exists
sleep ;
done
echo we never get there
exit )
44
ommand
creating file $tmp$my8file8;E;
press interrupt (UTVW>U) to interrupt J
Rile exists
Rile exists
Rile exists
Rile exists
The file no longer exists
Ureating file $tmp$my8file8;E;
Press interrupt (UTVW>U) to interrupt J
Rile exists
Rile exists
Rile exists
Rile exists
45
ommand
$nset remo-e "arameter or *unction
#!$bin$sh
foo/0+ello Xorld1
echo (foo
unset (foo
echo (foo
46
Pattern $atching

*ind search *or *iles in a directory hierarchy


%in! =path? =options? =tests? =actions?
o"tions
-de"th *ind content in the directory
-*ollo, *ollo, symolic lin/s
-maxde"ths % *ond % le-els directories
-mount do not *ind other directories
tests
-atime % accessed % days ago
-mtime % modi*ied % days ago
-ne, other*ile name o* a *ile
-ty"e ' *ile ty"e '
-user username elong to username
47
Pattern $atching
o"erator
H -not test re-erse
-a -and test and
-o -or test or
action
-exec command execute command
-o/ command con*irm and exectute command
-"rint "rint
-ls ls Wdils
Find *iles ne,er than ,hileD then "rint
% %in! . ne8er 8hile2 -print
48
Pattern $atching
Find *iles ne,er than ,hileD then "rint only *iles
% %in! . ne8er 8hile2 t$pe % print
Find *iles either ne,er than ,hileD) start ,ith 8F:
% %in! . C. -name 5;&6 or ne8er 8hile2 C/ t$pe %
print
Find *iles ne,er than ,hileD then list *iles
% %in! . ne8er 8hile2 t$pe % exec ls l EF C@
49
Pattern $atching

gre" "rint lines matching a "attern


1Ieneral !egular Ex"ression 5arser4
#rep =options? P*TT4VM =R!W4?
o"tion
-c "rint numer o* out"ut context
-E &nter"ret 5@##E!% as an extended regular ex"ression
-h Su"ress the "re*ixing o* *ilenames
-i ignore case
-l sur"ress normal out"ut
-- in-ert the sense o* matching
% #rep in 8or!s.txt
% #rep c in 8or!s.txt 8or!s2.txt
% #rep c v in 8or!s.txt 8or!s2.txt
50
Regular Expressions

a reular expression 1are-iated as reexp or reex) ,ith


"lural *orms reexps) reexes) or reexen4 is a string that
descries or matches a set o* strings) according to certain syntax
rules?

Syntax

Z Batches the start o* the line

A Batches the end o* the line

? Batches any single character

G[ Batches a single character that is contained ,ithin the


rac/ets

GZ[ Batches a single character that is not contained ,ithin the


rac/ets

14 De*ines a \mar/ed suex"ression<

]x)y^Batch the last \loc/\ at least x and not more than y times
51
Regular Expressions

Exam"les7

\?at\ matches any three-character string li/e


hat) cat or bat

\Ghc[at\ matches hat and cat

\GZ[at\ matches all the matched strings *rom


the regex \?at\ exce"t bat

\ZGhc[at\ matches hat and cat ut only at the


eginning o* a line

\Ghc[atA\ matches hat and cat ut only at the


end o* a line
52
Regular Expressions

!OS"# class si$ilar to $eanin

G7u""er7[ G@-_[ u""ercase letters

G7lo,er7[ Ga-P[ lo,ercase letters

G7al"ha7[ G@-_a-P[ u""er- and lo,ercase letters

G7alnum7[ G@-_a-P=-`[ digits) u""er- and lo,ercase letters

G7digit7[ G=-`[ digits

G7xdigit7[ G=-`@-Fa-*[ hexadecimal digits

G7"unct7[ G?)H?7???[ "unctuation

G7lan/7[ G Ot[ s"ace and #@B characters only

G7s"ace7[ G OtOnOrO*O-[lan/ 1,hites"ace4 characters

G7cntrl7[ control characters

G7gra"h7[ GZ OtOnOrO*O-[ "rinted characters

G7"rint7[ GZOtOnOrO*O-[ "rinted characters and s"ace

Exam"le7 GG7u""er7[a[ should only match the u""ercase letters and


lo,ercase aaa and aa?
53
Regular Expressions

!OS"# $o%ern &exten%e%' reular


expressions

#he more modern \extended\ regular ex"ressions


can o*ten e used ,ith modern $nix utilities y
including the command line *lag \-E\?

U Batch one or more times

? Batch at most once

9 Batch Pero or more

]n^ Batch n times

]n)^ Batch n or more times

]n)m^ Batch n to m times


54
Regular Expressions

Search *or lines ending ,ith ;e<


7 grep e( words:.txt

Search *or ;a<


7 grep a==SblankS?? word:.txt

Search *or ,ords starting ,ith ;#h?<


7 grep Th.==SblankS?? words:.txt

Search *or lines ,ith 1= lo,er case characters


7 grep &4 =a>9?3P;)3Q words:.txt
55
ommand

((command) to execute command in a script

Old format used 021 but it can be confused with


061
#!$bin$sh
echo The current directory is (PXY
echo the current users are ((who)
56
%rithmetic Expansion

$se A11M44 instead o* ex"r to e-aluate arithmetic eXuation


#!$bin$sh
x/)
while = 0(x1 &ne ;)?@ do
echo (x
x/((((x-;))
done
exit )
57
Parameter Expansion

5arameter @ssignment
foo/fred
echo (foo
EH.in.sh
*or i in 1 D
do
myFsecretF"rocess A]i^Ftm"
done
EH.in.sh
*or i in 1 D
do
myFsecretF"rocess AiFtm"
done
Ii-es result
;muFsecretF"rocess7
too *e, arguments<
A]"aram7-de*ault^ set de*ault i* null
A]E"aram^ length o* "aram
A]"aramC,ord^ remo-e smallest su**ix "attern
A]"aramCC,ord^ remo-e largest su**ix "attern
A]"aramE,ord^ remo-e smallest "re*ix "attern
A]"aramEE,ord^ remo-e largest "re*ix "attern
58
Parameter Expansion
#!$bin$sh
unset foo
echo (PfooS>barQ
foo/fud
echo (PfooS>barQ
foo/$usr$bin$";;$startx
echo (Pfoo#%$Q
echo (Pfoo##%$Q
bar/$usr$local$etc$local$networks
echo (Pbar7local%Q
echo (Pbar77local%Q
4xit )
Output
ar
*ud
usr.in.'11.startx
startx
.usr.local.etc
.usr
59
&ere 'ocuments

@ here document is a s"ecial-"ur"ose code loc/) starts


,ith 22
#!$bin.sh
cat ZZ!RNM[L!
hello
this is a here
document
!RNMU[L!
exit )
EH.in?sh
ed aFtextF*ile 22HE!E
K
d
?)OAs.is.,as.
,
X
HE!E
exit =
Output
#hat is line 1
#hat is line D
#hat ,as line J
a_text_file
#hat is line 1
#hat is line D
#hat is line K
#hat is line J
60
'ebug

sh Wn2scri"t3 set -o noexec chec/ syntax


set Wn

sh W-2scri"t3 set -o -erose echo command e*ore


set W-

sh Wx2scri"t3 set Wo trace echo command a*ter


set Wx
set Wo nounset gi-es error i* unde*ined
set Wx
set &o xtrace
set -o xtrace
trap 5echo 4xitingS critical variable /(critical8variable6
4"!T
61
Re(erences

Bash Beginners Iuide 1htt"7..tld"?org.LD5.Bash-


Beginners-Iuide.4

1htt"7..linux?tnc?edu?t,.techdoc.4

Вам также может понравиться