Академический Документы
Профессиональный Документы
Культура Документы
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?
Why Shell?
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?
!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
&nteracti-e 5rogram7
%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
>ariales
Conditions
Control
Lists
Functions
Shell Commands
!esult
Document
9
Variables
A5@#H "ath
A5S1 1normally C4
A5SD 1normally 34
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; > 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
@%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"
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
OO ac/slash
Oa alert
O ac/s"ace
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
= means success
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
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
Syntax
]x)y^Batch the last \loc/\ at least x and not more than y times
51
Regular Expressions
Exam"les7
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
1htt"7..linux?tnc?edu?t,.techdoc.4