Learn | LINUX AND SHELL SCRIPTING Online Training USA | LINUX AND SHELL SCRIPTING Trainings
Linux 3 t Scripting - CSC Company Site
Transcript of Linux 3 t Scripting - CSC Company Site
!!*** subroutine mpi_utils_step_parallel_edgeimplicit noneinteger ele, ierr
do ele = 1, nfacescall mpi_isend(commvec(ele)%field1(commvec(ele)%out_i1, &
& commvec(ele)%out_j1, && commvec(ele)%out_k1), && 1, commvec(ele)%mpi_type_out1, && commvec(ele)%to_id, commvec(ele)%to_id, && MPI_COMM_WORLD, send_reqs(ele), ierr)
if(ierr /= MPI_SUCCESS) thencall pio_abort(ierr)
end ifcall mpi_isend(commvec(ele)%field2(commvec(ele)%out_i2, &
& commvec(ele)%out_j2, && commvec(ele)%out_k2), && 1, commvec(ele)%mpi_type_out2, && commvec(ele)%to_id, commvec(ele)%to_id+tag_offset, && MPI_COMM_WORLD, send_reqs(nfaces+ele), ierr)
if(ierr /= MPI_SUCCESS) thencall pio_abort(ierr)
end ifend do
#ifdef NONBLOCKdo ele = 1, nfaces
call mpi_irecv(commvec(ele)%field1(commvec(ele)%in_i1, && commvec(ele)%in_j1, && commvec(ele)%in_k1), && 1, commvec(ele)%mpi_type_in1, &
Linux 3 – Scripting
August 22nd, 2016
Lecturers (in alphabetical order):
Jaakko Leinonen
Tomasz Malkiewicz
Thomas Zwinger
All material (C) 2016 by the authors.This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 UnportedLicense, http://creativecommons.org/licenses/by-nc-sa/3.0/
Ab
ou
t Th
is C
ou
rse
1
Pro
gram
, Au
gust
22
nd
09
:00
–0
9:3
0 M
orn
ing
coff
ee
& r
egi
stra
tio
n0
9:3
0 –
10
:00
In
tro
du
ctio
n t
o t
he
co
urs
ean
d r
eca
p o
f p
revi
ou
s co
urs
es
10
:00
–1
1:3
0B
ash
scr
ipti
ng
11
:30
–1
2:3
0 S
ed
& r
egu
lar
exp
ress
ion
s1
2:3
0 –
13
:30
Lun
ch1
3:3
0 –
14
:30
Aw
k1
4:3
0 –
15
:00
Co
ffe
e1
5:0
0 –
16
:00
Exe
rcis
es
16
:00
–1
6:3
0 W
rap
-up
2
Ho
w W
e T
eac
h
All
top
ics
are
pre
sen
ted
wit
h in
tera
ctiv
e d
emo
nst
rati
on
s.
–P
leas
e, in
dic
ate
imm
edia
tely
, if
pac
e is
to
o f
ast.
We
wan
t to
hav
e ev
eryo
ne
wit
h u
s al
l th
e ti
me.
Ad
dit
ion
ally
, exe
rcis
es t
o e
ach
of
the
sect
ion
s w
ill b
e p
rovi
ded
.
The
Exer
cise
s an
d W
rap
-up
sec
tio
ns
are
mea
nt
for
per
son
al a
nd
gr
ou
p in
tera
ctio
n a
nd
are
(w
ith
a t
ime-
limit
to
17
:00
or
17
:30
) ke
pt
in a
n o
pen
en
d s
tyle
.
3
Pra
ctic
alit
ies
Kee
p t
he
nam
e ta
g vi
sib
le.
Lun
ch is
ser
ved
in t
he
sam
e b
uild
ing.
–R
oo
m w
ill b
e lo
cked
du
rin
g lu
nch
bu
t lo
bb
y is
op
en, u
se t
he
lock
ers.
Toile
ts a
re in
th
e lo
bb
y.N
etw
ork
:–
Wi-
Fi: e
du
roam
, HA
KA
au
then
tica
tio
n–
Eth
ern
et c
able
s o
n t
he
tab
les
–C
SC-G
ues
t ac
cou
nts
up
on
req
ues
t
Tran
spo
rtat
ion
:–
Bu
s st
op
s to
Kam
pp
i/C
ente
r ar
e lo
cate
d a
t th
e o
ther
sid
e o
f th
e st
reet
(1
02
, 10
3).
–B
us
sto
ps
to C
ente
r/Pa
sila
are
on
th
is s
ide,
to
war
ds
the
bri
dge
(1
94
, 19
5, 5
03
-6).
–To
arr
ive
at C
SC t
he
sto
ps
are
at t
he
sam
e p
osi
tio
ns,
just
on
op
po
site
sid
es o
n t
he
stre
et.
–If
yo
u c
ame
by
car:
par
kin
g is
bei
ng
mo
nit
ore
d–
ask
for
a te
mp
ora
ry p
arki
ng
per
mit
fro
m t
he
rece
pti
on
(te
ll w
hic
h w
ork
sho
p y
ou
’re
par
tici
pat
ing)
.
Vis
itin
g o
uts
ide:
th
e d
oo
rs b
y th
e re
cep
tio
n d
esks
are
op
en.
Use
rnam
e an
d p
assw
ord
for
wo
rkst
atio
ns:
giv
en o
n-s
ite.
4
Aro
un
d C
SC
B1
B2
CS
C
(K4 S
ala
db
ar)
(TH
INK
resta
ura
nt)
Resta
ura
nt
Yo
uare
he
re
(Tra
inin
g
roo
m)
B1 (
102,1
03)
Kam
pp
i
B2 (
194/5
,503/4
/…
Pa
sila,…
Resta
ura
nt
5
To B
rief
ly G
o W
he
re W
e H
ave
Be
en
6
The
Sh
ell
“A s
hel
lin
co
mp
uti
ng
pro
vid
es a
use
r in
terf
ace
for
acce
ss t
o a
n
op
erat
ing
syst
em’s
ker
nel
ser
vice
s.”
(Wik
iped
ia)
–A
sh
elli
s a
pro
gram
wh
ich
pro
vid
es t
he
trad
itio
nal
, tex
t-o
nly
use
r in
terf
ace
for
Lin
ux
(an
d o
ther
Un
ix li
ke s
yste
ms)
.
–Sh
ell’s
pri
mar
y fu
nct
ion
is t
o r
ead
com
ma
nd
sth
at a
re t
yped
into
a
con
sole
or
term
inal
win
do
w a
nd
th
enex
ecu
teth
em.
A g
reat
var
iati
on
of
shel
ls a
re a
vaila
ble
bu
t B
ou
rne
shel
l, b
ash
, is
no
wad
ays
the
de-
fact
o d
efau
lt s
hel
l fo
r m
ost
sys
tem
s.
7
The
Sh
ell
Envi
ron
me
nt
The
shel
l kee
ps
trac
k o
f it
s se
ttin
gs a
nd
op
erat
ion
th
rou
gh s
hel
l en
viro
nm
ent
wh
ich
is a
set
of
stri
ng
vari
able
s.
The
vari
able
s ca
n b
e o
ne
of
two
typ
es, e
nvir
on
men
tal v
ari
ab
les
or
shel
l va
ria
ble
s.
–En
viro
nm
enta
l var
iab
les
are
vari
able
s th
at a
re d
efin
ed fo
r th
e cu
rren
t sh
ell a
nd
are
inh
erit
ed b
y an
y ch
ild s
hel
ls o
r p
roce
sses
.
–Sh
ell v
aria
ble
s ar
e co
nta
ined
exc
lusi
vely
wit
hin
th
e sh
ell i
n w
hic
h t
hey
w
ere
set
or
def
ined
.
Typ
ical
ly t
hes
e va
riab
les
are
def
ined
wit
h c
apit
al le
tter
s.
8
Ow
n V
aria
ble
s
To c
reat
e a
vari
able
yo
u s
imp
ly a
ssig
n a
val
ue
to it
:$ m
y_
va
ria
ble
="s
om
e v
alu
e"
To r
efer
to
(ak
a, u
se)
a va
riab
le, p
rece
de
it w
ith
a d
olla
r si
gn:
$ e
ch
o $
my_vari
ab
le
Use
exp
ort
com
man
d t
o d
efin
e w
het
her
a v
aria
ble
is a
sh
ell v
aria
ble
o
r an
en
viro
nm
ent
vari
able
.
The
unse
tco
mm
and
rel
ease
s (k
ills)
th
e va
riab
le.
9
Po
siti
on
al P
aram
ete
rs
A p
osi
tio
na
l pa
ram
eter
is a
var
iab
le t
hat
sto
res
the
argu
men
ts g
iven
to
a p
roce
ss s
tart
ed b
y th
e sh
ell.
$ s
om
ec
om
ma
nd
-a -
b f
ile
na
me
A p
aram
eter
is s
et if
it h
as b
een
ass
ign
ed a
val
ue
.
First
pa
ram
ete
r: $
0$1
$2
$3
10
Spe
cial
Par
amet
ers
$0
exp
and
s to
th
e n
ame
of
the
shel
l or
shel
l scr
ipt.
$#
exp
and
s to
th
e n
um
ber
of
po
siti
on
al p
aram
eter
s in
dec
imal
.
$@
exp
and
s to
th
e p
osi
tio
nal
par
amet
ers,
sta
rtin
g fr
om
on
e.
$$
exp
and
s to
th
e p
roce
ss ID
of
the
shel
l.
$?
exp
and
s to
th
e ex
it s
tatu
s o
f th
e m
ost
rec
entl
y ex
ecu
ted
fo
regr
ou
nd
pip
elin
e.
$!
exp
and
s to
th
e p
roce
ss ID
of
the
job
mo
st r
ecen
tly
pla
ced
into
th
e b
ackg
rou
nd
.
11
Job
Man
age
me
nt
By
def
ault
, pro
cess
es a
re s
tart
ed in
th
e fo
regr
ou
nd
bu
t m
ay a
lso
be
star
ted
as
bac
kgro
un
d p
roce
sses
.
Pre
ss C
trl+
Cto
kill
th
e fo
regr
ou
nd
pro
cess
.
–Te
rmin
als
are
usu
ally
co
nfi
gure
d t
o s
end
th
e SI
GIN
T si
gn
alt
o c
urr
ent
fore
gro
un
d p
roce
ss w
hen
th
e C
trl+
Cke
y co
mb
inat
ion
is p
ress
ed.
Pre
ss C
trl+
Zto
sen
d c
urr
ent
fore
gro
un
d p
roce
ss t
o b
ackg
rou
nd
.
Use
job
sco
mm
and
to
list
all
star
ted
pro
cess
es a
nd
fgan
d b
g
com
man
ds
to s
wit
ch b
etw
een
th
em.
Use
kill
-9
com
man
d t
o f
orc
ibly
kill
a p
roce
ss.
12
Re
dir
ect
ion
s
Bef
ore
a c
om
man
d is
exe
cute
d, i
ts in
pu
t an
d o
utp
ut
may
be
red
irec
ted
usi
ng
red
irec
tio
n o
per
ato
rs |,
<, >
and
/or
>>
.
A p
ipel
ine
is a
sim
ple
red
irec
tio
n w
her
e o
utp
ut
(std
ou
t) o
f ea
ch
com
man
d in
th
e p
ipel
ine
is c
on
nec
ted
via
a p
ipe
to t
he
inp
ut
(std
in)
of
the
nex
t co
mm
and
.$ l
s| s
ort
| le
ss
13
Co
mm
and
Su
bst
itu
tio
n
Co
mm
and
su
bst
itu
tio
n a
llow
s th
e o
utp
ut
of
a co
mm
and
to
rep
lace
th
e co
mm
and
itse
lf.$ a
=$
(ls);
ec
ho
$a
Old
fo
rmat
is `c
om
mand`
and
its
use
is d
isco
ura
ged
.
14
Bas
h s
crip
tin
g
15
Intr
od
uct
ion
Shel
l scr
ipti
ng
is t
he
art
of
wri
tin
g sc
rip
ts t
hat
can
au
tom
ate
rep
etit
ive
task
s
BA
SH is
pre
sen
ted
an
d it
is d
iscu
ssed
ho
w it
can
be
use
d a
s ge
ner
al
pro
gram
min
g la
ngu
age
16
Dif
fere
nt
she
lls
sh–
Bo
urn
e sh
ell
csh
/tcs
h–
C s
hel
l
ksh
–K
orn
shel
l
zsh
–Z
shel
l
… bash
–B
ou
rne
agai
n s
hel
l
–sh
succ
esso
r th
at in
clu
des
csh
and
ksh
feat
ure
s
17
Wh
en
NO
T to
use
a s
he
ll sc
rip
t?
Res
ou
rce
or
com
pu
te in
ten
sive
Larg
e-sc
ale
app
licat
ion
s
Co
mp
lex
dat
astr
uct
ure
s
Lot’
s o
f I/
O, n
eed
lib
rari
es, f
loat
ing
po
int
arit
hm
etic
, …
use
Pyth
on
, C
/C+
+ o
r F
ort
ran
, o
r e
ve
n
Ja
va
or
Pe
rl in
ste
ad
18
Wh
en
to
use
a s
he
ll sc
rip
t?
Glu
e to
geth
er U
nix
co
mm
and
s to
au
tom
ate
rep
etit
ive
task
s
–aw
k, b
c, m
ore
, les
s, p
aste
, …
Sim
ple
ap
plic
atio
ns
that
do
no
t n
eed
lots
of
reso
urc
es
Fan
cy s
crip
ts f
or
bat
ch jo
bs
19
Ho
w t
o w
rite
a s
crip
t?
Pu
t co
mm
and
s th
at y
ou
wo
uld
exe
cute
on
th
e co
mm
and
-lin
e in
a f
ile
$cat hello.sh
echo "Hello world!"
echo"... thisis my firstscript."
$bash hello.sh
Hello world!
... thisis my firstscript.
20
Pip
e m
agic
Co
mm
and
sca
nb
ech
ain
edto
geth
eru
sin
ga
pip
e|
The
ou
tpu
t o
f th
e1
st c
om
man
d(l
eft)
is u
sed
as t
he
inp
ut
of
the
2n
d
com
man
d(r
igh
t)
Arb
itra
ryn
um
ber
of
pip
esca
nb
eu
sed
$cat hello.sh | grep my
echo"... thisis my firstscript."
$./configure --help | less
21
Pip
e m
agic
wit
h f
iles
Ou
tpu
t ca
nb
ere
dir
ecte
dto
a f
ilew
ith
>o
verw
rite
>>
app
end
Inp
ut
can
be
read
fro
ma
file
wit
h
<re
ad$echo "Hello world!" > hello
$echo "... what's up?" >> hello
$cat < hello
Hello world!
... what's
up?
22
Pip
es
& s
tre
ams
Thre
e sp
ecia
lstr
eam
sal
way
so
pen
ed:
stdin
0<
keyb
oar
d
stdout
1>
scre
en
stderr
2>
erro
rm
essa
ges
(on
scr
een
)
stdout
and
stderr
can
be
join
edw
ith&>
or&>>
M>&N
red
irec
tsfi
led
escr
ipto
rM
to N
–e.
g. 2>&1
join
sst
der
ran
d s
tdo
ut
23
# separatestdout& stderr
$./myprogram> output 2> errors
# joinedstdout& stderr
$./myprogram&> output+errors
# one-timeredirect
$./myprogram> filename 2>&1
# permanentredirect
$exec 2>&1
$./myprogram> filename
# swap
stdoutand stderr
$./myprogram3>&2 2>&1 1>&3
Pip
es
& s
tre
ams
24
Exe
cuta
ble
scr
ipt
Ad
d a
sh
eban
g to
th
e sc
rip
t to
def
ine
the
shel
l th
at e
xecu
tes
it#!shell-binary
hello.sh:
#!/bin/bash
echo
"Hello
world!"
$chmod
u+x
hello.sh
$./hello.sh
Hello world!
25
Co
mm
en
ts
An
yth
ing
afte
r #
is t
reat
ed a
s a
com
men
t
Can
be
add
ed a
s se
par
ate
line,
or
at t
he
end
of
a lin
e
hello.sh:
#!/bin/bash
# let'sgreet
the
world...
echo"Helloworld!" # cliche
$bash hello.sh
Hello world!
26
Var
iab
les
Like
oth
er p
rogr
amm
ing
lan
guag
es b
ash
has
var
iab
les
Var
iab
les
can
be
set
wit
h name=value
To u
se t
he
vari
able
pre
ced
e th
e n
ame
wit
h $
, e.g
.:
$name
–Yo
u m
ay n
eed
to
en
clo
se t
he
vari
able
nam
e in
cu
rly
bra
cket
s:
${name}
27
Var
iab
le e
xpan
sio
n
$name=Max
$echo "Hi $name!"
Hi Max!
$name="Sir Max"; d=Sun
$echo "Dear $name,"
$echo "today is ${d}day."
Dear Sir Max,
todayis Sunday.
28
Var
iab
le t
ypes
Ther
e ar
e tw
o t
ypes
of
vari
able
s
–St
rin
gs
–In
tege
rs (
no
nat
ive
flo
atin
g p
oin
t ty
pe!
)
Ad
dit
ion
ally
var
iab
les
can
be
–R
ead
-on
ly
–A
n a
rray
By
def
ault
var
iab
les
can
ho
ld a
ny
dat
a w
e as
sign
to
it
–Th
is c
an b
e lim
ited
wit
h "
dec
lare
"
29
de
clar
e
Dec
lare
an
inte
ger
vari
able
declare –I name=value
Dec
lare
a r
ead
-on
ly v
aria
ble
declare –r name=value
Pri
nt
the
vari
able
att
rib
ute
s
declare –p name
30
Arr
ays
bas
h s
up
po
rts
1D
arr
ays
Def
inin
g an
arr
ay
–El
emen
t-b
y-el
emen
t:myarray[123]=456
–A
s a
list:myarray=( 456 4 apple 1 )
Elem
ents
do
no
t n
eed
to
be
con
tigu
ou
s
Val
ue
acce
ssed
as ${myarray[123]}
–D
oes
NO
T w
ork
wit
ho
ut
the
curl
yb
rack
ets!
31
Arr
ays
Spec
ial a
rray
syn
tax
${myarray[*]}
all i
tem
s
${!myarray[*]}
all i
nd
exes
${#myarray[*]}
nu
mb
er o
f it
ems
${#myarray[0]}
len
gth
of
item
0
${#myarray[1]}
len
gth
of
item
1
32
Co
mm
and
lin
e a
rgu
me
nts
Co
mm
and
lin
e ar
gum
ents
are
sto
red
as
spec
ial v
aria
ble
s
$#
nu
mb
er o
f ar
gum
ents
$@
all a
rgu
men
ts
$0
nam
e o
f sc
rip
t
$1
firs
t ar
gum
ent
$2
seco
nd
arg
um
ent
…
33
Co
mm
and
line
arg
um
en
ts
#!/bin/bash
echo"args: $@"
echo"name: $0"
echo"1st argument: $1"
echo"3rd argument: $3"
$bash cmd-args.sh one two three
args: one two three
name: cmd-args.sh
1st argument: one
3rd argument: three
cmd-args.sh:
34
Qu
ota
tio
ns
Sin
gle
qu
ote
s ('
) p
rese
rve
liter
al v
alu
es
Do
ub
leq
uo
tes
(")
pre
serv
e lit
eral
val
ues
, exc
ept
for:
$`\
$a='hello!'
$echo '$a'
$a
$echo "$a"
hello!
$echo "$a \" "
hello! "
35
Co
mm
and
su
bst
itu
tio
n
The
ou
tpu
t o
f a
com
man
d c
an b
e u
sed
in a
scr
ipt
wit
h
$(command)
(or `command`
)
$(command)
is s
ub
stit
ute
dw
ith
the
ou
tpu
t o
f command
just
like
a va
riab
lew
ou
ldb
e
$echo "Today is $(date)."
Todayis Fri Aug26 10:58:03 EEST 2011.
$b=20.1
$a=$(echo "$b*10.0" | bc–l)
$echo $a
201.00
36
Ari
thm
etic
s
Sim
ple
arit
hm
etic
sp
erfo
rmed
usi
ng:
(( expression
))
let"expression"
Var
iab
lean
d c
om
man
dex
pan
sio
nd
on
eo
n expression
$n
ot
nee
ded
for
vari
able
sin
expression
$a=10
$(( c = a -4 ))
$echo $c
6
$let "a += 3"
$echo $a
13
$echo $a
$(( a++ ))
14
37
Ari
thm
etic
exp
ansi
on
$(( expression))
rep
lace
db
yva
lue
of expression
(( expression))
use
dfo
r if
and
for
con
stru
cts
$a=10; b=20
$echo $(( a + b ))
30
$echo "$a x $b = $(( a * b ))"
10 x 20 = 200
$if (( $DEBUG || $MODE == "d" ))
then
echo "debug mode ON"
fi
38
Ari
thm
etic
op
era
tors
Op
era
tor
Me
an
ing
+ -
ad
ditio
n, su
btr
actio
n
* / %
mu
ltip
lica
tion
, d
ivis
ion
, re
ma
inde
r
**
exp
on
en
tia
tio
n
VAR++ VAR--
va
ria
ble
po
st-
incre
men
t a
nd
po
st-
decre
men
t
++VAR --VAR
va
ria
ble
pre
-in
cre
men
t a
nd
pre
-de
cre
me
nt
<= >= < >
co
mp
arison
op
era
tors
== !=
eq
ua
lity a
nd
in
eq
ua
lity
&&
log
ica
l AN
D
||
log
ica
l O
R
! ~
log
ica
l a
nd
bitw
ise
ne
ga
tio
n
&b
itw
ise
AN
D
^b
itw
ise
exclu
siv
e O
R
|b
itw
ise
OR
<< >>
left
an
d r
igh
t b
itw
ise
sh
ifts
expr ? expr : expr
co
nditio
nal e
va
luatio
n
= *= /= %=
assig
nm
ent o
pe
ratio
ns
+= -= <<= >>=
&= ^= |=
39
Co
nd
itio
nal
if s
tate
me
nts
Bas
ic f
orm
:
if test-commands
then
do-stuff
eliftest-commands
then
do-stuff
else
do-stuff
fi
$if [[ 1 –eq1 ]]
then
echo A
else echo B
fi
A
$[[ 1 –eq1 ]] && echo A || echo B
A
40
Test
co
mm
and
s
Fou
r w
ays
to d
o t
his
(ye
s re
ally
…)
–test
com
man
d o
r [ ]
sin
gle
bra
cket
s
–[[ ]]
do
ub
le b
rack
ets
–(( ))
arit
hm
etic
exp
ansi
on
Do
ub
le b
rack
ets
are
safe
r th
an test
or
sin
gle
bra
cket
s
Op
erat
ors
fo
r st
rin
gs, i
nte
gers
, file
s an
d B
oo
lean
alg
ebra
41
[[ ]
] –
file
te
st o
pe
rato
rs
[[ -e name ]]
file
exi
st
[[ -f name ]]
is a
reg
ula
r fi
le
[[ -d name ]]
is a
dir
ecto
ry
[[ -s name ]]
is s
ize
of
file
no
t ze
ro
[[ -r name ]]
file
has
rea
d p
erm
issi
on
[[ -w name ]]
file
has
wri
te p
erm
issi
on
[[ -x name ]]
file
has
exe
cute
per
mis
sio
n
…
42
[[ ]
] –
stri
ng
op
era
tors
[[ "s1" == "s2" ]]
stri
ng
equ
alit
y
[[ "s1" != "s2" ]]
stri
ng
ineq
ual
ity
[[ "s1" < "s2" ]]
stri
ng
lexi
ogr
aph
ic b
efo
re
[[ "s1" > "s2" ]]
stri
ng
lexi
ogr
aph
ic a
fter
[[ "s1" =~ "s2" ]]
regu
lar
exp
ress
ion
mat
ch
[[ -z "s1" ]]
stri
ng
has
zer
o le
ngt
h
[[ -n "s1" ]]
stri
ng
has
no
n-z
ero
len
gth
43
[[ ]
] –
inte
ger
op
era
tors
[[ 1 –eq 2 ]]
equ
alit
y
[[ 1 –ne 2 ]]
no
n-e
qu
alit
y
[[ 1 –lt 2 ]]
less
th
an
[[ 1 –gt 2 ]]
mo
re t
han
[[ 1 -le 2 ]]
less
th
an o
r eq
ual
[[ 1 -ge 2 ]]
grea
ter
than
or
equ
al
44
[[ ]
] –
Bo
ole
an a
lge
bra
[[ A || B ]]
(lo
gica
l) A
or
B
[[ A && B ]]
(lo
gica
l) A
an
d B
[[ ! A ]]
no
t A
45
Co
nd
itio
nal
exam
ple
#!/bin/bash
if[[ ($1 != $2) && \
( $1 || $2 ) ]]
then echoTrue
else echoFalse
fi
backsla
sh
ca
nbe
used
to
continue
the
com
mand
on t
he
next
line
$bash xor.sh 1 0
True
$bash xor.sh 1 1
False
xor.sh:
46
Cas
e
Bra
nch
cod
eb
ym
atch
ing
the
valu
eo
f a
vari
able
agai
nst
exp
ress
ion
(s)
case "$variable" in
expr1)
commands...
;;
expr2)
commands...
;;
*)
defaultcommands...
;;
esac
alw
ays t
rue!
each
blo
ck
term
ina
ted
with
;;
47
Cas
e e
xam
ple
#!/bin/bash
DEBUG=0
case "$1" in
-d|--debug)
DEBUG=1
;;
[0-9]*)
echo
"number"
;;
[[:upper:]])
echo
"uppercase
letter"
;;
*)
echo
"something
else"
;;
esac
(( $DEBUG )) && echo
"debug
mode
ON"$bash case.sh Z
uppercaseletter
$bash case.sh 77
number
$bash case.sh f!
somethingelse
$bash case.sh --debug
debugmodeON
case.sh:
48
Loo
ps
for
loo
ps
ove
r w
ord
s in
a s
trin
g
for namein Alice Bob Charlie
… o
r o
ver
nu
mer
ical
ind
eces
for ((i=0; i<10; i++)) HUOM: {0..10}
continue
jum
ps
to n
ext
iter
atio
n
break
step
so
ut
of
the
loo
p
while
, until
49
Loo
ps –
ove
r w
ord
s
#!/bin/bash
for namein Alice Bob "Charlie D"
do
echo
"Who? $name"
done
$bash loop.sh
Who? Alice
Who? Bob
Who? Charlie D
loop.sh:
50
Loo
ps –
ove
r fi
les
loop-files.sh:
#!/bin/bash
for f in *.sh
do
l=$(wc $f | awk'{ print$1 }')
echo"$f has$l lines"
done
$bash loop-files.sh
loop-args.sh has 7 lines
loop-files.sh has 8 lines
…
51
Loo
ps –
ove
r ar
gum
en
ts
loop-args.sh:
#!/bin/bash
for argin "$@"
do
echo$arg
done
qu
ote
sneeded
to h
and
le
wo
rdsp
littin
gco
rrectly!
$bash loop-args.sh one two "3 4"
one
two
3 4
52
Loo
ps –
ove
r in
tege
rs
loop-int.sh:
#!/bin/bash
for ((i=0; i<4; i++))
do
echo
part-$i
done
$bash loop-int.sh
part-0
part-1
part-2
part-3
53
Loo
ps –
wh
ile
loop-while.sh:
#!/bin/bash
i=0
while[[ $i –lt4 ]]
do
echopart-$i
let"i += 1"
done
$bash loop-while.sh
part-0
part-1
part-2
part-3
54
Fun
ctio
ns
(Lim
ited
) su
pp
ort
for
fun
ctio
ns
Dec
lara
tio
nm
ust
be
bef
ore
call!
funcs-hello.sh:
#!/bin/bash
hello
() {
echo"Funkyhello!"
} echo
"calling
functionhello() ..."
hello
decla
ration
ca
ll
$bash funcs-hello.sh
calling function hello() ...
Funkyhello!
55
Var
iab
les –
sco
pe
bas
h h
as t
hre
e sc
op
es f
or
vari
able
s:lo
cal,
glo
bal
an
d e
nvi
ron
men
t va
riab
les
Loca
l var
iab
les
are
loca
l to
cu
rren
t co
deb
lock
–se
t w
ith
: local var=value
–u
se lo
cal v
aria
ble
s in
fu
nct
ion
s!
Glo
bal
var
iab
les
are
glo
bal
in c
urr
ent
shel
l
–se
t w
ith
: var=value
56
Var
iab
les –
sco
pe
Envi
ron
men
t va
riab
les
are
inh
erit
ed b
y al
l lau
nch
ed s
ub
-sh
ells
–Se
t w
ith
: export var=value
–C
han
ges
in v
alu
e ar
e n
ot
pas
sed
bac
k!
–B
y co
nve
nti
on
, ALL
_CA
PS
use
dfo
r n
ames
57
Var
iab
les –
sco
pe
variables.sh:
#!/bin/bash
scopetest() {
local
lvar=20
echo
"---in
function---"
echo
"lvar: $lvar"
echo
" var: $var"
echo
"EVAR: $EVAR"
} var=30
export
EVAR=40
scopetest
echo
"---in
main scope---"
echo
"lvar: $lvar"
echo
" var: $var"
echo
"EVAR: $EVAR"
bash
print-variables.sh
print-variables.sh:
#!/bin/bash
echo "---in next shell---"
echo "lvar: $lvar"
echo " var: $var"
echo "EVAR: $EVAR"
58
Var
iab
les –
sco
pe
$bash variables.sh
---in function---
lvar: 20
var: 30
EVAR: 40
---in mainscope---
lvar:
var: 30
EVAR: 40
---in nextshell---
lvar:
var:
EVAR: 40
und
efin
ed
!
59
Fun
ctio
ns –
inp
ut
Inp
ut
par
amet
ers
wo
rk
like
com
man
d-l
ine
argu
men
ts
funcs-input.sh:
#!/bin/bash
addup() {
localtotal=0
while[[ $# -gt0 ]]
do
case "$1" in
[0-9]*)
let"total+= $1"
;;
*)
echo"invalidargument: $1" >&2
;;
esac
shift
done
echo"Grand total: $total"
} addup4 6 12
addup3 8 foo
$bash funcs-input.sh
Grand total: 22
invalidargument: foo
Grand total: 11
$bash funcs-input.sh 2>
/dev/null
Grand total: 22
Grand total: 11
60
Fun
ctio
ns –
ou
tpu
t
No
dir
ect
way
to
get
ou
tpu
t
Ret
urn
val
ue
of
fun
ctio
n is
–0
su
cces
s
–n
on
zero
fai
lure
Op
tio
ns
are
–G
lob
al v
aria
ble
(n
ot
reco
mm
end
ed b
ut
fast
)
–C
om
man
d s
ub
stit
uti
on
, i.e
. ech
o o
utp
ut
(slo
w?)
–O
utp
ut
vari
able
nam
e p
asse
d a
s in
pu
t
61
Fun
ctio
ns
-o
utp
ut
# set global
out_a(){
val_a=$1
} # command subst
out_b(){
echo $1
} # pass output varname
out_c(){
eval$1='$2'
}
$out_a123
$echo $val_a
123
$val_b=$(out_b123)
$echo $val_b
123
$out_cval_c123
$echo $val_c
123
eval
will
evalu
ate
the lin
e a
fter
sh
ell
exp
an
sio
ns
62
He
re d
ocu
me
nts
Pip
ean
arb
itra
ryle
ngt
hte
xtb
lock
span
nin
go
ver
mu
ltip
lelin
esto
a c
om
man
d
Sam
e as
command < file
wh
ere file
incl
ud
es t
he
inp
ut
abo
ve
END
is a
n a
rbit
rary
tag
th
at m
arks
th
e en
d o
f in
pu
t
$command << END
... multiple
lines
of text
...
END
63
He
re d
ocu
me
nts
heredoc.sh:
cat << EndBlock
A here document can be used to print out
instructions using cat. It can also be used to
steer an interactive program as shown below...
EndBlock
gnuplot<< EOF
f(x)=1/x
plot f(x)
pause 2
EOF
64
He
re d
ocu
me
nts
$bash heredoc.sh
A here document can be used to print out
instructions using cat. It can also be used to
steer an interactive program as shown below...
65
Furt
her
info
rmat
ion
On
line
reso
urc
es
–B
ash
gu
ide
for
Beg
inn
ers
htt
p:/
/tld
p.o
rg/L
DP/
Bas
h-B
egi
nn
ers
-Gu
ide/
htm
l/
–A
dva
nce
d B
ash
-Scr
ipti
ng
Gu
ide
htt
p:/
/tld
p.o
rg/L
DP/
abs/
htm
l/
–B
ASH
FA
Qh
ttp
://m
ywik
i.wo
ole
dge
.org
/Bas
hFA
Q
–G
NU
Bas
h R
efer
ence
Man
ual
h
ttp
://w
ww
.gn
u.o
rg/s
oft
war
e/b
ash
/man
ual
/bas
hre
f.htm
l
66
Sed
an
d R
egu
lar
Exp
ress
ion
s
67
Mat
chin
g Te
xt
A n
um
ber
of
Un
ix t
ext-
pro
cess
ing
uti
litie
s le
t yo
u s
earc
h f
or,
an
d in
so
me
case
s ch
ange
, tex
t st
rin
gs.
–Th
ese
uti
litie
s in
clu
de
the
edit
ing
pro
gram
s e
d, e
x, v
ian
d s
ed, t
he
aw
k
pro
gram
min
g la
ngu
age,
an
d t
he
com
man
ds
gre
pan
d e
gre
p.
Reg
ula
r ex
pre
ssio
ns,
or
reg
exes
for
sho
rt, a
re a
way
to
mat
ch t
ext
wit
h p
atte
rns.
Reg
ula
r ex
pre
ssio
ns
are
a p
atte
rn m
atch
ing
stan
dar
d f
or
stri
ng
par
sin
g an
d r
epla
cem
ent.
68
The
Mo
st S
imp
le R
ege
x
In it
's s
imp
lest
form
, a r
egu
lar
exp
ress
ion
is a
str
ing
of
sym
bo
ls t
o
mat
ch "
as is
".
A s
imp
le e
xam
ple
:$ g
rep
'2
34
'
Re
ge
xM
atc
he
s
ab
ca
bca
bca
bc
23
41
23
45
69
Qu
anti
fie
rs
To m
atch
sev
eral
ch
arac
ters
yo
u n
eed
to
use
a q
uan
tifi
er:
–*
mat
ches
any
nu
mb
ero
f w
hat
's b
efo
re it
, fro
m z
ero
to
infi
nit
y.
–?
mat
ches
zer
o o
r o
ne
of
wh
at's
bef
ore
it.
–+
mat
ches
on
e o
r m
ore
of
wh
at's
bef
ore
it.
A s
imp
le e
xam
ple
:$ g
rep
'2
3*4
'
Re
ge
xM
atc
hes
23
*41
24
5, 1
23
45, 1
23
34
5
23
?4
12
45, 1
23
45
23
+4
12
34
5, 1
23
34
5
70
Bas
ic R
ege
xes
vs. E
xte
nd
ed
Re
gexe
s
The
Ba
sic
Reg
ula
r Ex
pre
ssio
ns
or
BR
E fl
avo
rst
and
ard
izes
a f
lavo
r si
mila
r to
th
e o
ne
use
d b
y th
e tr
adit
ion
al U
NIX
gre
p c
om
man
d.
–O
nly
su
pp
ort
ed m
etac
har
acte
rs a
re .
(do
t), ^
(car
et),
$(d
olla
r), a
nd
*(s
tar)
. To
mat
ch t
hes
e ch
arac
ters
lite
rally
, esc
ape
them
wit
h a
\.
–So
me
imp
lem
enta
tio
ns
sup
po
rt \?
and
\+, b
ut
they
are
no
t p
art
of
the
PO
SIX
sta
nd
ard
.
Mo
st m
od
ern
reg
ex f
lavo
rs a
re e
xten
sio
ns
of
the
ERE
fla
vor.
By
tod
ay's
sta
nd
ard
, th
e P
OSI
X E
RE
flav
or
is r
ath
er b
are
bo
nes
.
We
will
be
usi
ng
exte
nd
ed r
egex
es, s
o:
$ a
lia
s g
rep
='g
rep
--c
olo
r=a
uto
-E
'
71
Re
gexe
s A
re H
ogg
ish
By
def
ault
, reg
exes
are
gre
edy.
Th
ey m
atch
as
man
y ch
arac
ters
as
po
ssib
le.
You
can
def
ine
ho
w m
any
inst
ance
s o
f a
mat
ch y
ou
wan
t b
y u
sin
g ra
nge
s:
–{m
}m
atch
es o
nly
mn
um
ber
of
wh
at's
bef
ore
it.
–{m
,n}
mat
ches
mto
nn
um
ber
of
wh
at's
bef
ore
it.
–{m
,}m
atch
es m
or
mo
re n
um
ber
of
wh
at's
bef
ore
it.
Re
ge
xM
atc
he
s
21
22
22
3
72
Spe
cial
Ch
arac
ters
A lo
t o
f sp
ecia
l ch
arac
ters
are
ava
ilab
le fo
r re
gex
bu
ildin
g. H
ere
are
som
e o
f th
e m
ore
usu
al o
nes
:
–.th
e d
ot
mat
ches
an
y si
ngl
e ch
arac
ter.
–\w
mat
ches
an
alp
han
um
eric
ch
arac
ter,
\Wa
no
n-a
lph
anu
mer
ic.
–\to
esc
ape
spec
ial c
har
acte
rs, e
.g. \
.m
atch
es a
do
t, a
nd
\\m
atch
es a
b
acks
lash
.
–^
mat
ches
th
e b
egin
nin
g o
f th
e in
pu
t st
rin
g.
–$
mat
ches
th
e en
d o
f th
e in
pu
t st
rin
g.
73
Spe
cial
Ch
arac
ter
Exam
ple
s
Re
ge
xM
atc
he
sD
oe
s n
ot
ma
tch
1.3
12
34
, 1
z3
, 0
13
31
3
1.*
31
3, 1
23
, 1
zd
fkj3
\w+
@\w
+a
@a
, e
ma
il@o
y.a
b,.-!
"#€
%&
/
^1.*
3$
13
,1
23
, 1
zd
fkj3
x1
3,
12
3x,
x1
zd
fkj3
x
74
Ch
arac
ter
Cla
sse
s
You
can
gro
up
ch
arac
ters
by
pu
ttin
g th
em b
etw
een
sq
uar
e b
rack
ets.
Th
is w
ay, a
ny
char
acte
r in
th
e cl
ass
will
mat
ch a
ny o
ne
char
acte
r in
th
e in
pu
t.
–[a
bc]m
atch
es a
ny
of
a, b
, an
d c
.
–[a
-z]m
atch
es a
ny
char
acte
r b
etw
een
a a
nd
z.
–[^
ab
c]
mat
ches
an
yth
ing
oth
er t
han
a, b
, or
c.
N
ote
th
at h
ere
the
care
t ^
at t
he
beg
inn
ing
ind
icat
es "
no
t" in
stea
d o
f b
egin
nin
g o
f lin
e.
–[+
*?.]
mat
ches
an
y o
f +,
*, ?
or
the
do
t.
M
ost
sp
ecia
l ch
arac
ters
hav
e n
o m
ean
ing
insi
de
the
squ
are
bra
cket
s.
75
Ch
arac
ter
Cla
ss E
xam
ple
s
Re
ge
xM
atc
he
sD
oe
s n
ot
ma
tch
[^a
b]
c, d
, z, a
bc
a,
b, a
b
^[1
-9][0
-9]*
$1
, 4
5, 1
01
0,
-1, a
1, 2
.0
[0-9
]*[,.]?
[0-9
]+1
, .1
, 0
.1,
1,0
00
,0
,0,0
,0
76
Gro
up
ing
and
Alt
ern
ativ
es
It m
igh
t b
e n
eces
sary
to
gro
up
th
ings
to
geth
er, w
hic
h is
do
ne
wit
h
par
enth
eses
( a
nd
).
–G
rou
pin
g it
self
usu
ally
do
es n
ot
do
mu
ch, b
ut
com
bin
ed w
ith
oth
er
feat
ure
s tu
rns
ou
t to
be
very
use
ful.
The
OR
op
erat
or
| m
ay b
e u
sed
fo
r al
tern
ativ
es.
Re
ge
xM
atc
he
sD
oe
s n
ot
ma
tch
(ab
)+a
b, a
ba
b,
aa
bb
aa
bb
Re
ge
xM
atc
he
sD
oe
s n
ot
ma
tch
(aa
|bb
)+a
a, b
ba
a,
aa
bb
ab
ab
77
Sub
exp
ress
ion
s
Wit
h p
aren
thes
es, y
ou
can
als
o d
efin
e su
bex
pre
ssio
ns
to s
tore
th
e m
atch
aft
er it
has
hap
pen
ed a
nd
th
en r
efer
to
it la
ter
on
.
You
can
sto
re u
p t
o n
ine
mat
ches
an
d r
efer
bac
k to
th
e m
atch
es
usi
ng
\1, \
2,…
\9
no
tati
on
.
Re
ge
xM
atc
he
sD
oe
s n
ot
ma
tch
(ab
)\1
ab
ab
cd
cd
ab
,a
bca
bc
(ab
)c.*
\1a
bca
bc, a
bcd
efa
bcd
ef
ab
c, a
ba
bc
78
Som
e P
ract
ical
Exa
mp
les
Ch
eck
for
a va
lid f
orm
at f
or
emai
l ad
dre
ss:
$ g
rep
'^
[A-Z
a-z
0-9
_\.
-]+
@[A
-Za-z
0-9
_\.
-]+
[A-Z
a-z
][A
-Za-z
]$'
–^[
A-Z
a-z
0-9
_\.
-]+
mat
ches
a p
osi
tive
nu
mb
er o
f ac
cep
tab
le c
har
acte
rs a
t th
e st
art
of
the
stri
ng.
–@
mat
ches
th
e @
sig
n.
–[A
-Za
-z0
-9_
\.-]
+m
atch
es a
ny
do
mai
n n
ame,
incl
ud
ing
a d
ot.
–[A
-Za
-z0
-9_
][A
-Za
-z0
-9_
]$m
atch
es t
wo
acc
epta
ble
ch
arac
ters
.
Ch
eck
for
a va
lid f
orm
at f
or
Fin
nis
h s
oci
al s
ecu
rity
nu
mb
er:
$ g
rep
'^
[0-9
]{6
}[+
-A][
0-9
]{3
}[A
-Z0-9
]$'
79
The
Str
eam
Ed
ito
r –
sed
Sed
is a
no
n-i
nte
ract
ive
–o
r st
ream
ori
ente
d –ed
ito
r.
Sed
co
mes
sta
nd
ard
wit
h e
very
PO
SIX
-co
mp
lian
t U
nix
.
Mas
teri
ng
sed
can
be
red
uce
d t
o u
nd
erst
and
ing
and
man
ipu
lati
ng
the
fou
r sp
aces
of
sed
. Th
ese
fou
r sp
aces
are
:
–In
pu
t st
ream
–Pa
tter
n s
pac
e
–H
old
bu
ffer
–O
utp
ut
Stre
am
80
Ho
w s
ed
Wo
rks
Sed
rea
ds
dat
a fr
om
th
e in
pu
t st
ream
un
til i
t fi
nd
s th
e n
ewlin
e ch
ara
cter
\n.
Then
it p
lace
s th
e d
ata
read
so
far
, wit
ho
ut
the
new
line,
into
th
e p
atte
rn s
pac
e. M
ost
of
the
sed
co
mm
and
s o
per
ate
on
th
e d
ata
in
the
pat
tern
sp
ace.
–Th
e h
old
bu
ffer
is t
her
e fo
r yo
ur
con
ven
ien
ce. Y
ou
can
co
py
or
exch
ange
dat
a b
etw
een
th
e p
atte
rn s
pac
e an
d t
he
ho
ld b
uff
er.
On
ce s
ed h
as e
xecu
ted
all
the
com
man
ds,
it o
utp
uts
th
e p
atte
rn
spac
e an
d a
dd
s a
new
line
char
acte
r \n
at t
he
end
.
81
Syn
tax
of
Sed
Co
mm
and
s
Sed
co
mm
and
s h
ave
the
gen
eral
fo
rm:
[ad
dre
ss [
,ad
dre
ss]
[!]]
co
mm
an
d [
arg
um
en
ts]
–Th
e o
pti
on
al a
dd
ress
essp
ecif
y to
wh
ich
inp
ut
lines
th
e co
mm
and
will
b
e ap
plie
d t
o, t
he
def
ault
is e
very
lin
e.
–Th
e co
mm
an
ds
con
sist
s o
f a
sin
gle
lett
er o
r sy
mb
ol.
–Th
e a
rgu
men
tsar
e o
pti
on
al a
nd
on
ly a
few
co
mm
and
s ac
cep
t th
em.
82
A S
imp
le s
ed
Pro
gram
This
pro
gram
rep
lace
s te
xt "
foo
" w
ith
"b
ar"
on
eve
ry li
ne:
$ s
ed
's
/fo
o/b
ar/
'
Sed
op
ens
the
file
as
the
inp
ut
stre
am a
nd
sta
rts
read
ing
the
dat
a.
–A
fter
rea
din
g th
e fi
rst
line
it f
ind
s a
new
line
\nan
d it
th
en p
lace
s th
e te
xt r
ead
in t
o t
he
pat
tern
sp
ace.
Nex
t se
d a
pp
lies
the
s/foo/b
ar/
com
man
dfo
r th
e p
atte
rn s
pac
e.
–If
th
ere
is n
o "
foo
" an
ywh
ere,
sed
do
es n
oth
ing
to t
he
pat
tern
sp
ace
.
–If
th
ere
is "
foo
", it
will
be
rep
lace
d b
y "b
ar".
The
def
ault
act
ion
wh
en a
ll th
e co
mm
and
s h
ave
bee
n e
xecu
ted
is t
o
pri
nt
the
pat
tern
sp
ace,
fo
llow
ed b
y n
ewlin
e.
83
Op
tio
ns
-n a
nd
-e
, an
d C
om
man
d p
If y
ou
sp
ecif
y o
pti
on
-nth
en s
ed w
ill n
o lo
nge
r p
rin
t th
e p
atte
rn
spac
e w
hen
it r
each
es t
he
end
of
the
scri
pt:
$ s
ed
-n
's
/fo
o/b
ar/
'
–If
yo
u r
un
th
is p
rogr
am, t
her
e w
ill b
e n
o o
utp
ut.
You
no
w m
ust
use
sed
’sp
com
man
d t
o f
orc
e se
d t
o p
rin
t th
e lin
e:
$ s
ed
-n
's
/fo
o/b
ar/
; p
'
–Se
d c
om
man
ds
are
sep
arat
ed b
y th
e se
mic
olo
n ;
char
acte
r.
–Yo
u c
an a
lso
use
th
e o
pti
on
-eto
sep
arat
e th
e co
mm
and
s:$
se
d -
n -
e 's/f
oo
/bar/
' -e
'p
'
84
Dan
ger:
th
e O
pti
on
-i
Op
tio
n -i
forc
es s
ed t
o d
o in
-pla
ce e
dit
ing
of
the
file
:$ s
ed
-i's
/fo
o/b
ar/
' fi
le
–Th
is m
ean
s se
d r
ead
s th
e co
nte
nts
of
the
file
, exe
cute
s th
e co
mm
and
s,
and
pla
ces
the
new
co
nte
nts
bac
k in
th
e fi
le.
Be
very
car
efu
l wh
en u
sin
g -i
as it
’s d
estr
uct
ive
and
it’s
no
t re
vers
ible
!
–It
’s a
lway
s sa
fer
to r
un
sed
wit
ho
ut
-i, a
nd
th
en r
epla
ce t
he
file
yo
urs
elf.
85
Ad
dre
sses
The
sim
ple
st fo
rm o
f ad
dre
ss is
a s
ingl
e n
um
ber
th
at li
mit
s se
d
com
man
ds
to t
he
give
n li
ne
nu
mb
er:
$ s
ed
-n
'5p'
–O
nly
th
e 5
thlin
e o
f th
e in
pu
t st
ream
will
be
pri
nte
d.
The
add
ress
es c
an b
e in
vert
ed w
ith
th
e !
afte
r th
e ad
dre
ss:
$ s
ed
-n
'5!p
'
–P
rin
t al
l bu
t 5
thlin
e.
You
can
als
o li
mit
sed
co
mm
and
s to
a r
ange
of
lines
by
spec
ifyi
ng
two
nu
mb
ers,
sep
arat
ed b
y a
com
ma:
$ s
ed
-n
'5,1
0p
'
–P
rin
t o
nly
lin
es 5
–1
0, i
ncl
usi
ve.
86
Re
gula
r Ex
pre
ssio
ns
as A
dd
ress
es
Ther
e is
als
o a
reg
ula
r ex
pre
ssio
n a
dd
ress
mat
ch, /
rege
x/.
If y
ou
sp
ecif
y a
regu
lar
exp
ress
ion
bef
ore
a c
om
man
d, t
hen
th
e co
mm
and
will
on
ly g
et e
xecu
ted
on
lin
es t
hat
mat
ch t
he
rege
x:$ s
ed
-n
'/-
/p'
–P
rin
t o
nly
lin
es c
on
tain
ing
hyp
hen
(o
r m
inu
s) c
har
acte
r -.
You
can
als
o u
se e
xpre
ssio
ns
to m
atch
a r
ange
bet
wee
n t
wo
reg
exes
:$ s
ed
-n
'/M
ay/,
/Au
g/p
'
–Th
is m
atch
es a
ll lin
es b
etw
een
th
e fi
rst
line
that
mat
ches
"M
ay"
rege
x an
d t
he
firs
t lin
e th
at m
atch
es "
Au
g" r
egex
, in
clu
sive
.
87
The
Las
t Li
ne
an
d M
ixin
g A
dd
ress
Typ
es
The
spec
ial a
dd
ress
$m
atch
es t
he
last
lin
e o
f th
e in
pu
t st
ream
:$ s
ed
-n
'$
p'
–P
rin
ts o
nly
th
e la
st li
ne
of
the
inp
ut
stre
am.
Ad
dre
sses
can
als
o b
e co
mb
ined
:$ s
ed
-n
'5
,$p
'
–Fr
om
5th
line
to t
he
end
of
file
.
$ s
ed
-n
'/r
eg
ex/,
$p
'
–Fr
om
lin
e m
atch
ing
reg
exto
th
e en
d o
f fi
le.
$ s
ed
-n
'5,/
reg
ex/p
'
–Fr
om
5th
line
to t
he
line
mat
chin
g re
gex
.
88
Co
mm
and
Gro
up
ing
Wit
h c
om
ma
nd
gro
up
ing{…
}it
is p
oss
ible
to
ap
ply
on
e se
t o
f co
mm
and
s to
sp
ecif
ic li
nes
an
d a
no
ther
set
of
com
man
ds
for
all t
he
rest
of
the
inp
ut
lines
.
–It
say
s, e
xecu
te a
ll th
e co
mm
and
s in
{…}
on
th
e lin
e(s)
th
at m
atch
es
the
rest
rict
ion
.
E.g.
to
pri
nt
the
line
afte
r re
gex
, bu
t n
ot
the
line
con
tain
ing
the
reg
ex:
$ s
ed
-n
'/re
ge
x/{
n; p
}'
–Th
e n
com
man
dw
ill e
mp
ty t
he
curr
ent
pat
tern
sp
ace
and
rea
d in
th
e n
ext
line
of
inp
ut.
89
The
Ho
ld B
uff
er
Wit
h h
old
bu
ffer
yo
u c
an s
ave
the
curr
ent
line
to t
he
ho
ld b
uff
er,
and
th
en le
t se
d r
ead
in t
he
nex
t lin
e.
The
com
man
d f
or
cop
yin
g th
e cu
rren
t p
atte
rn s
pac
e to
th
e h
old
b
uff
er is
han
d t
he
com
man
d f
or
cop
yin
g th
e h
old
bu
ffer
bac
k to
p
atte
rn s
pac
e is
g.
–Th
e co
mm
and
fo
r ex
chan
gin
g th
e co
nte
nts
of
the
ho
ld b
uff
er a
nd
th
e p
atte
rn s
pac
e is
x.
E.g.
to
pri
nt
the
line
bef
ore
th
e lin
e th
at m
atch
es r
egex
:$ s
ed -
n '/r
egex/{
x;
p;
x};
h'
90
Text
Su
bst
itu
tio
n
The
sub
stit
ute
com
man
d s
///
is u
sed
to
fin
d a
nd
rep
lace
tex
t:$ s
ed
's
/fo
o/b
ar/
'
–C
han
ge t
he
firs
t o
ccu
rren
ce o
f "f
oo
" w
ith
"b
ar".
Sub
stit
ute
th
e 4
tho
ccu
rren
ce o
f "f
oo
" w
ith
"b
ar"
on
eac
h li
ne:
$ s
ed
's
/fo
o/b
ar/
4'
–W
ith
a n
um
eric
fla
g lik
e /1
, /2, e
tc. o
nly
th
at o
ccu
rren
ce is
su
bst
itu
ted
.
Sub
stit
ute
all
occ
urr
ence
s o
f "f
oo
" w
ith
"b
ar"
on
eac
h li
ne:
$ s
ed
's
/fo
o/b
ar/
g'
–W
ith
glo
bal
fla
g /g
set,
su
bst
itu
te c
om
man
d d
oes
as
man
y su
bst
itu
tio
ns
as p
oss
ible
, i.e
., a
ll.
91
Fin
d a
nd
Re
pla
ce v
s. S
ub
stit
ute
You
can
use
an
y re
gex
to m
atch
(i.e
. fin
d)
the
text
yo
u w
ant:
$ s
ed
's
/\(.
*\)f
oo
/\1
ba
r/'
–R
epla
ce o
nly
th
e la
st o
ccu
rren
ce o
f "f
oo
" w
ith
"b
ar".
–N
ote
th
at s
ed u
ses
bas
ic r
egu
lar
exp
ress
ion
s, h
ence
\(
and
\)!
Yo
u c
an u
se e
xten
ded
reg
exes
wit
h t
he
-ro
pti
on
:$
se
d -
r 's
/(.*
)fo
o/\1b
ar/
'
You
can
als
o r
efer
bac
k to
th
e m
atch
ed s
trin
g w
ith
th
e &
char
acte
r:$ s
ed
's
/^\(
.*\)
$/(
&)/
'
–A
dd
s p
aren
thes
is a
rou
nd
th
e lin
e.
92
Tran
slat
e C
har
acte
rs
The
tra
nsl
ate
com
man
d y
/src
/dst/
do
es t
ran
slit
erat
ion
, a 1
:1 m
app
ing
of
sym
bo
ls in
src
to s
ymb
ols
in d
st:
$ s
ed
'y/a
bc/x
yz/'
–C
han
ge e
very
inst
ance
of
ato
x, b
to y
, an
d c
to z
.
The
com
man
d a
lway
s ac
ts o
n t
he
wh
ole
pat
tern
sp
ace
so y
ou
can
't
use
reg
exes
to
mat
ch t
he
par
ts o
f th
e lin
e yo
u w
ou
ld w
ant
to.
–Yo
u c
an s
till
use
ad
dre
ssin
g to
lim
it t
he
com
man
d t
o s
pec
ific
lin
es:
$ s
ed
'/r
eg
ex
/,$
y/s
rc/d
st/
'
Tr
ansl
ate
on
ly f
rom
th
e fi
rst
line
mat
chin
g re
gex
to t
he
end
of
the
file
.
93
De
leti
ng
Text
The
dco
mm
and
–d
elet
es t
he
curr
ent
pat
tern
sp
ace;
–re
ads
in t
he
nex
t lin
e in
to t
he
pat
tern
sp
ace;
an
d
–ab
ort
s th
e sc
rip
t ex
ecu
tio
n a
nd
sta
rts
the
exec
uti
on
at
the
firs
t se
d
com
man
d.
$ s
ed
'/^
$/d
'
–D
elet
es a
ll b
lan
k lin
es f
rom
th
e in
pu
t st
ream
.
$ s
ed
'n
; n
; n
; d
'
–D
elet
es e
very
4th
line.
94
Lab
els
an
d B
ran
chin
g
The
com
man
d :n
am
ecr
eate
s a
nam
ed la
bel
na
me,
wh
ich
yo
u c
an
bra
nch
to
wit
h t
he
bco
mm
and
:$
sed
':a
$s/\
n/ /g
; N
; ba'
–Th
e :a
com
man
d c
reat
es a
nam
ed la
bel
a.
–Th
e $
rest
rict
s th
e n
ext
com
man
d o
nly
to
th
e la
st li
ne
of
inp
ut.
–Th
e s/\
n/ /g
sub
stit
ute
s al
l new
line
char
acte
r to
sp
ace
char
acte
rs.
–Th
e N
com
man
d f
irst
ap
pen
ds
a n
ewlin
e to
th
e cu
rren
t p
atte
rn s
pac
e an
d t
hen
ap
pen
ds
the
nex
t in
pu
t lin
e.
–Th
e b
aco
mm
and
bra
nch
es b
ack
to t
he
lab
el a
.
–Ef
fect
ivel
y, t
his
will
join
all
inp
ut
lines
into
a s
ingl
e lin
e.
95
Sed
Is G
oo
d f
or…
A T
uri
ng
ma
chin
eis
a h
ypo
thet
ical
mac
hin
e th
ou
ght
of
by
the
mat
hem
atic
ian
Ala
n T
uri
ng
in 1
93
6. D
esp
ite
its
sim
plic
ity,
th
e m
ach
ine
can
sim
ula
te any
com
pu
ter
algo
rith
m, n
o m
atte
r h
ow
co
mp
licat
ed it
is!
–Se
d h
as b
een
dem
on
stra
ted
1to
be
Turi
ng
com
ple
te s
o in
th
eory
, yo
u
can
use
sed
fo
r an
y co
mp
uta
tio
nal
pro
ble
m:
$ s
ed
-f
se
dtr
is.s
ed
$ s
ed
-n
fa
rka
no
id.s
ed
Sed
is m
ain
ly u
sed
fo
r it
s te
xt s
ub
stit
uti
on
cap
abili
ties
.
1
Bla
eess
, Ch
rist
op
he,
Imp
lem
enta
tio
n o
f a
Turi
ng
Mac
hin
e as
Sed
Scr
ipt,
htt
p:/
/ww
w.c
ato
nm
at.n
et/f
tp/s
ed/t
uri
ng.
txt
96
awk–
a ve
rsat
ile t
ext
pro
cess
ing
lan
guag
e
97
awk–
text
pro
cess
ing
Dev
elo
ped
at
Bel
l Lab
s in
19
77
by
Ah
o(n
ot
Esko
, bu
t A
lfre
d V
ain
ö!)
, Wei
nb
erge
r, K
ern
igh
an
A v
ersa
tile
scr
ipti
ng
lan
guag
e w
hic
h r
esem
ble
s C
(su
rpri
se!
-K
ern
igh
an&
Rit
chie
)
Pow
erfu
l wit
h s
pre
ad-s
hee
t /
tab
ula
ted
dat
a
Typ
ical
usa
ge p
erh
aps
in o
ne-
liner
s w
ith
m
atch
ing
/reo
rder
ing
/fo
rmat
tin
g/c
alcu
lati
ng
fiel
ds
fro
m t
he
exis
tin
g ta
ble
s o
f d
ata
awk
com
man
d s
crip
tin
g is
als
o a
vaila
ble
98
awk
line
co
mm
and
To p
rin
t a
cert
ain
co
lum
n (
$2
ref
ers
to 2
nd
colu
mn
in r
ow
–w
ill b
e ex
pla
ined
late
r), t
ype
the
follo
win
g to
th
e te
rmin
al
$aw
k'{
pri
nt
$2
}' /
etc
/ne
tco
nfi
g
–b
y d
efau
lt y
ou
ass
um
e th
at t
he
file
is s
epar
ated
by
bla
nk
spac
es
You
can
red
irec
t th
e o
utp
ut
(usi
ng
the
> sy
mb
ol)
to
sto
re t
he
resu
lt
into
a n
ew f
ile
$aw
k'{
pri
nt
$2
}' /e
tc/n
etc
on
fig
>n
etc
on
fig
2n
dc
olu
mn
.tx
t
You
can
als
o u
se it
wit
hin
a p
ipe
$c
at
/etc
/ne
tco
nfi
g|
aw
k'{
pri
nt
$2
}'
99
awk-
scri
pts
You
can
sav
e yo
ur
awk-
dir
ecti
ves
in a
tex
t fi
le (
a.k.
a. s
crip
t). W
hy?
–So
met
imes
on
e-l
iner
s ge
t to
o lo
ng
–Yo
u w
ant
to b
e ab
le t
o e
asily
rep
rod
uce
yo
ur
awk-
com
man
d
–M
ay b
e u
sefu
l if
you
nee
d t
o d
ecla
re u
ser
def
ined
fu
nct
ion
s th
rou
gh
com
man
d s
crip
ts
–N
ot
man
dat
ory
, bu
t u
sefu
l to
giv
e su
ffix
.aw
k
–Tr
igge
red
by
op
tio
n -
f
Can
be
use
d in
co
nn
ecti
on
wit
h r
edir
ecte
d o
utp
ut:
$a
wk
-f m
ys
cri
pt.
aw
kin
pu
tfil
e.t
xt
> o
utp
utf
ile
.txt
100
awk
com
man
ds
esse
nti
ally
mat
ch a
pat
tern
fro
m a
tex
t an
d a
pp
ly a
n
acti
on
to
it:
!/ p
att
ern
/ {
ac
tio
n }
(th
e ex
clam
atio
n m
ark
inve
rts
mat
ch)
For
exam
ple
, we
wan
t to
pri
nt
all r
elev
ant
lines
in /e
tc/n
etc
on
fig
, i.e
.,
excl
ud
e al
l co
mm
ente
d li
nes
th
at s
tart
wit
h #
$a
wk
'!/#
/' /e
tc/n
etc
on
fig
awk
pat
tern
101
An
oth
er e
xam
ple
giv
en b
y a
scri
pt
to d
isp
lay
all n
olo
gin
-acc
ou
nts
in
the
syst
em (
save
into
file
slide3.awk)
:
BE
GIN
{x =
0}
/no
log
in/ {x
= x
+ 1
; p
rin
t x,"
...",
$1}
EN
D {
pri
nt
"--
----
----
----
----
";p
rin
t "n
olo
gin
s=
",x
}
Use
-f o
pti
on
to
lau
nch
th
e sc
rip
t
$a
wk
-f s
lid
e3
.aw
k /e
tc/p
as
sw
d
awk
pat
tern
ctd
.
102
Pre
-an
d p
ost
-pro
cess
ing
ste
ps
BE
GIN
{ }
and
EN
D {
}
stat
emen
ts a
re o
pti
on
al in
aw
kan
d if
p
rese
nt,
th
ey e
xecu
te c
od
e b
efo
re a
nd
aft
er r
ead
ing
the
inp
ut
They
are
not
test
ed a
gain
st t
he
inp
ut
BE
GIN
is o
ften
use
d t
o in
itia
lize
vari
able
s b
efo
re t
he
firs
t in
pu
t lin
e h
as b
een
rea
d in
EN
Dis
usu
ally
use
d t
o p
rin
t so
me
sum
mar
y in
form
atio
n a
fter
inp
ut
has
bee
n f
inis
hed
103
Fie
ld s
ep
arat
or
Fiel
d s
epar
ato
r (F
S),
th
e sa
me
as -
Fo
pti
on
, can
be
use
d t
o in
dic
ate
char
acte
r(s)
use
d t
o s
epar
ate
con
secu
tive
fie
lds:
$a
wk
-F:
-f s
lid
e3
.aw
k /e
tc/p
as
sw
d
If y
ou
do
no
t w
ant
to u
se t
he
–F
op
tio
n, d
efin
e in
sid
e th
e sc
rip
t
BE
GIN
{ F
S=
"[:
,]" }
You
r F
Sis
eit
her
co
lon
or
com
ma,
try
fo
r in
stan
ce (
NF
is n
um
ber
of
colu
mn
s –
see
nex
t sl
ide)
:
$e
ch
o “
0 1
:2,3
4" |
aw
k-F
"[:
,]" '{
pri
nt
NF
" l
ast
co
lum
n:
"
$N
F}‘
or
wit
h b
lan
k o
r co
lon
$ e
ch
o “
0 1
:2,3
4" |
aw
k-F
"[:
]" '{
pri
nt
NF
" l
ast
co
lum
n:
"
$N
F}‘
104
Re
cord
se
par
ato
r –
RS
Sim
ilar
to F
S, t
he
reco
rd s
epar
ato
r (R
S)
can
be
use
d t
o t
urn
an
y ch
arac
ter(
s) in
to li
ne
bre
aks
(=n
ew r
ow
s)
Ther
e is
no
co
mm
and
lin
e o
pti
on
fo
r R
S t
hat
can
be
pas
sed
The
follo
win
g p
rin
ts o
ut
no
t 1
, bu
t 4
lin
es:
$e
ch
o "
AA
,BB
:CC
;DD
" |
aw
k'B
EG
IN{R
S=
"[,
:;]"
}{p
rin
t}'
105
Co
un
ters
of
colu
mn
s an
d r
ow
s
NF
is t
he
nu
mb
er o
f fi
eld
s o
n e
ach
lin
e (#
co
lum
ns
in r
ow
)
$ a
wk
'{fo
r (i
=1
; i<
=N
F;
i++
) p
rin
t $
i}' fi
le.t
xt
NR
is t
he
nu
mb
er o
f in
pu
t re
cord
s (l
ines
)
$ a
wk
'EN
D {
pri
nt
NR
}' /
etc
/pa
ss
wd
Mu
ch s
imp
ler
still
: $
wc
-l
/etc
/pa
ss
wd
awk
fiel
ds
are
acce
ssed
th
rou
gh v
aria
ble
s $
1 ,
$2
, …
, $
(NF
-1),
$(N
F)
–$
0re
fers
to
th
e w
ho
le in
pu
t ro
w
106
Co
un
ters
of
colu
mn
s an
d r
ow
s ct
d.
Pri
nt
wh
ole
lin
e o
nly
if n
um
ber
of
fiel
ds
(=co
lum
ns
in r
ow
) ex
ceed
s 7
$ a
wk
'(N
F >
7)
{pri
nt
}' /
etc
/ne
tco
nfi
g
Try
also
wit
h N
F >
6 a
nd
sp
ot
the
dif
fere
nce
Pri
nt
firs
t 5
ro
ws
$a
wk
'(N
R <
= 5
) {p
rin
t }'
/e
tc/n
etc
on
fig
this
is th
e s
am
e a
s
$h
ea
d –
n 5
/e
tc/n
etc
on
fig
107
Pri
nt
stat
em
en
t in
aw
k
Inst
ead
of
usi
ng
gen
eric
pri
nt
in a
wk,
it is
po
ssib
le t
o u
se C
-lan
guag
e lik
e p
rin
tf
This
giv
es y
ou
a f
ull
spec
tru
m o
f C
-lik
e fo
rmat
tin
g ca
pab
iliti
es, e
.g.
$d
ate
| a
wk
-F"[
:]" '{p
rin
tf("
Tim
e=
%2d
ho
urs
an
d %
2d
min
ute
s\n
",$
5,$
6)}
'
–P
leas
e d
o n
ot
forg
et t
o s
up
ply
th
e n
ewlin
e "\
n"
in p
rin
tf!
The
gen
eric
p
rin
tal
read
y ad
ds
that
fo
r yo
u –
auto
mat
ical
ly
108
Var
iab
les
in a
wk
awk
has
pre
def
ined
var
iab
les,
use
r d
efin
ed v
aria
ble
s an
d a
rray
s
Pre
def
ine
dva
riab
les
are
fiel
ds
colu
mn
s ($
1,$
2,…
), t
he
wh
ole
lin
e ($
0)
or
inte
rnal
var
iab
les
(kep
t in
cap
ital
lett
ers)
like
NF,
NR
, F
S,
RS
Use
r d
efi
ne
d v
aria
ble
s ar
e u
sual
ly g
iven
in a
low
erca
se t
o a
void
mix
-u
p, e
.g. a
, b
, tm
p
–Fo
r in
stan
ce lo
op
co
un
ters
: {f
or
(i=
1;
i<=
NF
; i+
+)
pri
nt
$i}
–O
r st
rin
g va
riab
les:
myte
xt=
"ja
da
, ja
da
"
Var
iab
les
are
set
eith
er
–in
sid
e th
e sc
rip
t
–o
r as
arg
um
ent:
$ a
wk
-F:
'{ p
rin
t$n
}' n
=1
/etc
/passw
d
109
awk
arra
ys a
re in
fac
t a
sso
cia
tive
arr
ays
–Th
is m
ean
s th
e in
dex
into
an
arr
ay
do
es n
ot
ha
ve t
o b
e a
n in
teg
er
nu
mb
er
It c
an b
e an
yth
ing
fro
m n
um
eric
al v
alu
es (
even
flo
atin
g p
oin
t) t
o
char
acte
r st
rin
gs, a
nd
can
be
loo
ped
th
rou
gh:
BE
GIN
{tm
p[1
5.6
]=0;
tmp
["s
an
om
ale
hti
"]=
"Ilta
san
om
at"
; tm
p["
Vo
lvo
"]=
"Ch
ine
se
"}
EN
D{f
or
(iin
tm
p)
{pri
nt
i,tm
p[i
]}}
Save
into
slid
e1
2.a
wk
and
ru
n: $
aw
k-f
slid
e12.a
wk
–N
ote
: th
e o
rder
in w
hic
h t
he
arra
y is
sca
nn
ed t
hro
ugh
is a
rbit
rary
–In
ord
er t
o s
ee s
om
eth
ing
you
hav
e to
sen
d a
EO
F (C
trl+
D)
to s
tdin
Var
iab
les
in a
wk,
ctd
.
110
Bu
ilt-i
n f
un
ctio
ns
in a
wk
Som
e n
um
eric
al f
un
ctio
ns:
in
t, e
xp
, lo
g, sin
, co
s, sq
rt, …
e.g
., $
for
((x
=1
; x<
=1
80
; x+
+))
; {
e
ch
o $
x;
} |
aw
k'{
pri
nt
$1,
co
s($
1*3
.141
59
27
/18
0.0
)}'
Som
e st
rin
g h
and
ling
fun
ctio
ns:
su
bs
tr,
ma
tch
, s
pri
ntf
, to
low
er,
tou
pp
er,
…
e.g.
ch
angi
ng
ever
yth
ing
to u
pp
er-c
ase,
$a
wk
'{p
rin
t to
up
pe
r($
0)}
' /e
tc/n
etc
on
fig
Bit
man
ipu
lati
on
fu
nct
ion
s: a
nd
, o
r, x
or,
ls
hif
t, c
om
pl,…
$a
wk
'BE
GIN
{pri
ntf
"an
d(1
,0)=
%#
x o
r(1
,0)=
%#
x \
n",a
nd
(1,0
), o
r(1
,0)}
‘
111
Co
ntr
ol s
tate
me
nts
awk
con
tain
s if
-els
est
atem
ents
fo
r co
nd
itio
nal
co
mp
uta
tio
n$
aw
k'{
if (
$1
> 0
) {
pri
nt
“p
os
itiv
e" }
e
lse
{ p
rin
t “n
eg
ati
ve
" ;
} }
'
–Yo
u c
an a
dd
th
is t
o t
he
pre
vio
us
cosi
ne
-pip
elin
e
–Se
mic
olo
n (
";")
is o
nly
nee
ded
if y
ou
hav
e se
vera
l aw
kcl
ause
s af
ter
each
o
ther
Can
als
o b
e p
rogr
amm
ed a
s a
lad
der
:
if(c
on
dit
ion
1)
{acti
on
1};
els
eif
(co
nd
itio
n2
) {a
cti
on
2};
… e
lse
{acti
on
N};
Logi
cal o
per
ato
rs: o
r ||
; an
d &
&:
if (
(co
nd
itio
n1
a||
co
nd
itio
n1
b)
&&
co
nd
itio
n2
) {a
cti
on
};
112
Furt
her
re
adin
g
Ple
ase
do
rea
d a
wk
Un
ix m
anu
al p
ages
:
$m
an
aw
k
$in
fo a
wk
Web
co
nta
ins
a p
len
ty o
f ad
dit
ion
al in
fo
–D
o g
oo
gle
for
inst
ance
on
"aw
ktu
tori
al"
113