UNIX Utilities and Shell Programming Part1
-
Upload
leoricksimon -
Category
Documents
-
view
247 -
download
0
Transcript of UNIX Utilities and Shell Programming Part1
-
8/11/2019 UNIX Utilities and Shell Programming Part1
1/155
C O N F I D E N T I A L
UNIX Utilities and ShellProgramming
Brian M. Appold
Director of Design Solutions
US Office Sunnyvale, CAOctober 2!
-
8/11/2019 UNIX Utilities and Shell Programming Part1
2/155
C O N F I D E N T I A L 2October 2!
Co"rse Sched"le
" U#$% Utilities
" &ednesday, October '(, )*''a+
" riday, October '-, )*''a+" uesday, October 2', 2*/p+
" C S0ell 1rogra++ing
" &ednesday, October 22, 2*/p+
-
8/11/2019 UNIX Utilities and Shell Programming Part1
3/155
C O N F I D E N T I A L !October 2!
Co"rse #oals
" U#$% co++ands" Use wcco++and to count c0aracters, ords, 3 lines" Use sortco++and to sort files" Use findco++ands to locate files" Use grep/egrepco++ands to select lines of a file
" Use sedco++and to edit a file" Use awkco++and to create for+atted output fro+ a
structured file
" C S0ell co++ands" 4ead user inputs
" Update a file" 1erfor+ arit0+etic" 5ist file contents sorted" 5ist a file by user specified searc0 pattern
-
8/11/2019 UNIX Utilities and Shell Programming Part1
4/155
C O N F I D E N T I A L /October 2!
UNIX Utilities
" wcco++and
" sortco++and
" findco++and
" S0ell +etac0aracters
" grep/egrepco++ands
" 4egular e6pression +etac0aracters
" sedco++and
" awkco++and
-
8/11/2019 UNIX Utilities and Shell Programming Part1
5/155
C O N F I D E N T I A L (October 2!
UNIX Utilities E$ercises
" 76ercise ' wc, sort, 3 find
" 76ercise 2 S0ell Metac0aracters
"76ercise ! 4egular 76pression
" 76ercise / 4eg. 76pression Metac0aracters
" 76ercise ( sedCo++ands
" 76ercise 8 Multi*line sedCo++ands
" 76ercise - awkCo++ands
" 76ercise 9 More awk
-
8/11/2019 UNIX Utilities and Shell Programming Part1
6/155
C O N F I D E N T I A L 8October 2!
UNIX Utilities % wc" Synta6:
wc;options< filena+e
" Options:
*l count lines* count ords
=note: spaces and returns define >ord?@
*c count c0aracters
-
8/11/2019 UNIX Utilities and Shell Programming Part1
7/155
C O N F I D E N T I A L -October 2!
UNIX Utilities % wc" 76a+ples:
c petc0ip
) '( /8 petc0ip
c l petc0ip
) petc0ip
c petc0ip
'( petc0ip
c c petc0ip
/8 petc0ip
-
8/11/2019 UNIX Utilities and Shell Programming Part1
8/155
C O N F I D E N T I A L 9October 2!
UNIX Utilities % sort" Synta6:
sort;options< ;s;*s
-
8/11/2019 UNIX Utilities and Shell Programming Part1
9/155
C O N F I D E N T I A L )October 2!
UNIX Utilities % sort" 76a+ples:
sort t: petc0ip
Cory =Big Boy@:cat:DSE:!F'9F'))):(:red:S+it0:)(!(
red:ferret::'2F/F2':':Brian:Gennedy:/-'(/
5ucy:cat:D5E:(F2'F'))(:(:ran:Andres:)/9(2Maggy:dog:5ab Mi6:)F2!F2':'2:o+:Hones:)(''-
Milly:cat:DSE:'F2F22:!:Betty:S+it0:)/(!)
Otis:cat:DSE:'F2F22:!:Ben:Stiller:/9-!/
1retIel:dog:errier Mi6:)F!F'))8:(:5ena:Eatc0er:)/98
S0elby:dog:Jolden 4etriever:-F!F22:'2:Diane:5ee:9(82!Spuny:dog:Jer+an S0eppard:8F'(F2:-:Gi+:4eed:)/(!8
#ote': default is for starting field to sort
#ote 2: petc0ip file s0on in Appendi6 A
-
8/11/2019 UNIX Utilities and Shell Programming Part1
10/155
C O N F I D E N T I A L 'October 2!
UNIX Utilities % sort" 76a+ples:
sort t: 8 - ( 8 petc0ip
5ucy:cat:D5E:(F2'F'))(:(:ran:Andres:)/9(2
1retIel:dog:errier Mi6:)F!F'))8:(:5ena:Eatc0er:)/98
Maggy:dog:5ab Mi6:)F2!F2':'2:o+:Hones:)(''-red:ferret::'2F/F2':':Brian:Gennedy:/-'(/
S0elby:dog:Jolden 4etriever:-F!F22:'2:Diane:5ee:9(82!
Spuny:dog:Jer+an S0eppard:8F'(F2:-:Gi+:4eed:)/(!8
Milly:cat:DSE:'F2F22:!:Betty:S+it0:)/(!)
Cory =Big Boy@:cat:DSE:!F'9F'))):(:red:S+it0:)(!(Otis:cat:DSE:'F2F22:!:Ben:Stiller:/9-!/
#ote: if first sort finds duplicate data, t0en data is sorted by ;( 8sort r t: ' petc0ip? ill give you t0e sa+e result
-
8/11/2019 UNIX Utilities and Shell Programming Part1
13/155
C O N F I D E N T I A L '!October 2!
UNIX Utilities % sort" 76a+ples:
sort n t: - petc0ip
red:ferret::'2F/F2':':Brian:Gennedy:/-'(/
Otis:cat:DSE:'F2F22:!:Ben:Stiller:/9-!/
S0elby:dog:Jolden 4etriever:-F!F22:'2:Diane:5ee:9(82!
1retIel:dog:errier Mi6:)F!F'))8:(:5ena:Eatc0er:)/98
Spuny:dog:Jer+an S0eppard:8F'(F2:-:Gi+:4eed:)/(!8
Milly:cat:DSE:'F2F22:!:Betty:S+it0:)/(!)
5ucy:cat:D5E:(F2'F'))(:(:ran:Andres:)/9(2
Cory =Big Boy@:cat:DSE:!F'9F'))):(:red:S+it0:)(!(Maggy:dog:5ab Mi6:)F2!F2':'2:o+:Hones:)(''-
#ote: #u+eric ', 2, !, ', 2, K
ASC$$ ', ', 2, 2, !, K
-
8/11/2019 UNIX Utilities and Shell Programming Part1
14/155
C O N F I D E N T I A L '/October 2!
UNIX Utilities % find
" Synta6:
finddirectory ;ot0er options< ;output options?
a : *f a' petc0ip
Cory =Big Boy@ cat DSE !F'9F')))( red S+it0
Spuny dog Jer+an S0eppard 8F'(F2
- Gi+ 4eed
Maggy dog 5ab Mi6 )F2!F2'
'2 o+ Hones
K
-
8/11/2019 UNIX Utilities and Shell Programming Part1
75/155
C O N F I D E N T I A L -(
October 2!
awk% C"rl/ 5races" `ou +ay include +ultiple co++ands inside t0e curly
braces. 1ut a carriage return after eac0 co++and, or elseseparate t0e co++ands it0 se+icolons.
Tprint [',[2,[!,[/
print [(,[8,[-print >?
Tprint [',[2,[!,[/W print [(,[8,[-W print >?
T
print [',[2,[!,[/print [(,[8,[-
print >?
-
8/11/2019 UNIX Utilities and Shell Programming Part1
76/155
C O N F I D E N T I A L -8
October 2!
awk% Printing +et"rns ( Tabs" Use Vn to print a carriage return, and Vt to print a tab.
cat a2
Tprint [',[2,[!,[/ >Vn? [(,[8,[- >Vn?
a : *f a2 petc0ip
Cory =Big Boy@ cat DSE !F'9F')))( red S+it0
Spuny dog Jer+an S0eppard 8F'(F2
- Gi+ 4eed
Maggy dog 5ab Mi6 )F2!F2'
'2 o+ Hones
K
-
8/11/2019 UNIX Utilities and Shell Programming Part1
77/155
C O N F I D E N T I A L --
October 2!
awk% Processing a S"bset o1
+oBs Using Strings" A subset of t0e ros of t0e files are processed by using astring searc0 before t0e curly braces.
" String alues
Fregular*e6pressionFfield Fregular*e6pressionF
^ Fregular*e6pressionF
field ^ Fregular e6pressionF
" Boolean alues
]] logical O4
33 logical A#D
-
8/11/2019 UNIX Utilities and Shell Programming Part1
78/155
C O N F I D E N T I A L -9
October 2!
awk% Processing a S"bset o1
+oBs Using Strings" 76a+ples: a : QF5enaF Tprint [8,[-,[( petc0ip
5ena Eatc0er (
a : Q['FMillyF Tprint [',[2,[!,[/ petc0ip
Milly cat DSE 'F2F22
a : Q['F\MF 33 ['Fy[F Tprint [',[( petc0ipMaggy '2
Milly !
-
8/11/2019 UNIX Utilities and Shell Programming Part1
79/155
C O N F I D E N T I A L -)
October 2!
awk% Processing a S"bset o1
+oBs Using N"meric 6al"es" A subset of t0e ros of t0e files are processed by using anu+eric searc0 before t0e curly braces.
" #u+eric alues
field value
field ^ value
field L value
field L value
field N value
field N value
-
8/11/2019 UNIX Utilities and Shell Programming Part1
80/155
C O N F I D E N T I A L 9
October 2!
awk% Processing a S"bset o1
+oBs Using N"meric 6al"es" 76a+ples: a : Q[( N / Tprint [8,[-,[( petc0ip
Betty S+it0 !
Ben Stiller !
Brian Gennedy '
a : Q[( '2 Tprint [8,[-,[( petc0ip
o+ Hones '2
Diane 5ee '2
a : Q[( L ( 33 [( N ' Tprint [',[( petc0ip
Spuny -
-
8/11/2019 UNIX Utilities and Shell Programming Part1
81/155
C O N F I D E N T I A L 9'
October 2!
awk% Processing a S"bset o1
+oBs Bith )"lti,le Searches cat a![2FcatF Tprint >VtBuy so+e cat food for > ['
[2FdogF Tprint >VtBuy so+e dog food for > ['
[2FferretF Tprint >VtBuy so+e ferret food for > ['
a : *f a! petc0ip
Buy so+e cat food for Cory =Big Boy@
Buy so+e dog food for Spuny
Buy so+e dog food for MaggyBuy so+e dog food for 1retIel
Buy so+e cat food for Milly
K
-
8/11/2019 UNIX Utilities and Shell Programming Part1
82/155
C O N F I D E N T I A L 92
October 2!
E$ercise @ %awk Commands
4efer to t0e file in Appendi6 C, rite an awkco++and to:
'. 1rint a list of parties contained in t0e file c0ecingAnser: a : QTprint [! c0ecing
2. 1rint a list of parties and ite+ nu+bers for ite+s rittenin ebruary
Anser: a : Q[2F\2VFF Tprint [!,[' c0ecing
!. 1rint t0e nu+bers for c0ecs ritten for +ore t0at ['Anser: a : Q['^F\;Dd
-
8/11/2019 UNIX Utilities and Shell Programming Part1
83/155
C O N F I D E N T I A L 9!
October 2!
awk% 5E#IN ( END Sections" `ou +ay add a B7J$# or 7#D section to your awkscript
in order to process infor+ation before or after any rosare processed.
cat a/
B7J$# Tprint >1artyVtVtC0ec A+ount?['F\;*)Vt? [/
a : *f a/ c0ecing
1arty C0ec A+ount
A+er. 76press 2/!.Citiban isa '/.)9
1ac. Jas37lec.!).-
1acific Bell 8(.((
-
8/11/2019 UNIX Utilities and Shell Programming Part1
84/155
C O N F I D E N T I A L 9/
October 2!
awk% 6ariables
" Any ord t0at is not a ey ord ill be considered avariable na+e by awk. awkdoes contain so+evariables t0at it eeps trac of auto+atically.
" awkariables
$57#AM7 na+e of t0e data file
S field separator, defaults to spaces
# nu+ber of fields in current record
#4 nu+ber of t0e current record =i.e. line Z@
OS output field separator, representedby a co++a in print state+ents, defaults
to a space
-
8/11/2019 UNIX Utilities and Shell Programming Part1
85/155
C O N F I D E N T I A L 9(
October 2!
awk% 6ariables
" 76a+ples:
a : Q#4 L ! 33 #4 N ( Tprint [8,[- petc0ip
o+ Hones
5ena Eatc0erBetty S+it0
a : Q7#D Tprint >0ere are >#4? records in >$57#AM7 petc0ip
0ere are ) records in petc0ip
-
8/11/2019 UNIX Utilities and Shell Programming Part1
86/155
C O N F I D E N T I A L 98
October 2!
awk% 6ariables
" 76a+ples:
cat locost
B7J$# TS>:?WOS>K?
[( N / Tprint [8,[-,[(
a f locost petc0ip
BettyKS+it0K!
BenKStillerK!BrianKGennedyK'
-
8/11/2019 UNIX Utilities and Shell Programming Part1
87/155
C O N F I D E N T I A L 9-
October 2!
awk% User De1ined 6ariables
" ariables can be user defined.
cat a8
B7J$# T
S>:?
total
['F\DF Ttotaltotal[/
['F\;*)Balance is: > total
a f a8 c0ecing
Balance is: '/'(./
-
8/11/2019 UNIX Utilities and Shell Programming Part1
88/155
C O N F I D E N T I A L 99
October 2!
awk% Arithmetic
" awkcan be used to perfor+ arit0+etic operations.
" Su++ary
* R F standard arit0+etic re+ainder
add '
** subtract '
-
8/11/2019 UNIX Utilities and Shell Programming Part1
89/155
C O N F I D E N T I A L 9)
October 2!
awk% Standard Arithmetic
" Addition
ec0o '2 /(8-.( !((.(/! !/ ] a QTprint ['[!
!(('2.(/!
" Division and Multiplication
ec0o '2 /(8-.( !((.(/! !/ ] a QTprint ['F[!
.!!92!
ec0o '2 ./(8- !((.(/! .!/- ] a QTprint [2R[/
.'(9/-(
-
8/11/2019 UNIX Utilities and Shell Programming Part1
90/155
C O N F I D E N T I A L )
October 2!
awk% +emainder Arithmetic
" 4e+ainder ec0o '2 /(8-.( !((.(/! !/ ] a QTprint [22
'.(
ec0o '2 /(8-.( !((.(/! !/ ] a QTprint ['2
ec0o '2 /(8-.( !((.(/! !/ ] a QTprint [/2
ec0o '2 /(8-.( !((.(/! !/ ] a QTprint [!2
.(/!
-
8/11/2019 UNIX Utilities and Shell Programming Part1
91/155
C O N F I D E N T I A L )'
October 2!
awk% Add8S"btract & Arithmetic
" Add one or subtract one
ec0o '2 /(8-.( !((.(/! !/ ] a QTprint [/
!(
ec0o '2 /(8-.( !((.(/! !/ ] a QTprint [/
!/
ec0o '2 /(8-.( !((.(/! !/ ] a QTprint **[/
!!
-
8/11/2019 UNIX Utilities and Shell Programming Part1
92/155
C O N F I D E N T I A L )2
October 2!
E$ercise ; %)oreawk
4eferring to t0e petc0ip file, rite an awkco++and to:'. Jive a total of all t0e +a6i+u+ a+ounts oners are
illing to pay for e+ergency servicesAnser:
B7J$# TS>:?
total
Ttotaltotal[(
7#D Tprint >otal for e+ergency service: > total2. &rite a co++and to +ultiply t0e nu+ber (8.)- by
(/.!.Anser: ec0o (8.)- (/.! ] a QTprint ['R[2
-
8/11/2019 UNIX Utilities and Shell Programming Part1
93/155
C O N F I D E N T I A L )!
October 2!
E$ercise ; %)oreawk
!. 1rint a c0art of pet na+es, species, and id nu+bers.1ut 0eaders on t0e output data. 1rint a total
nu+ber of pets at t0e botto+ of t0e c0art.Anser:
B7J$# Tprint >#a+eVtVtSpeciesVt$D?Tprint ['?Vt??Vt?[2?Vt?[9
7#D Tprint >otal nu+ber of pets: >#4
/. 5ist lines (*'Anser: a : Q#4 L ( 33 #4 N ' Tprint [ petc0ip
(. 5ist ani+als 0ose oners are illing to pay L[',. on +edical costs.
Anser: a : Q[( L ' Tprint [' petc0ip
-
8/11/2019 UNIX Utilities and Shell Programming Part1
94/155
C O N F I D E N T I A L )/
October 2!
awk% F"nctions
" awkco+es it0 so+e built*in functions. Eere are aselect fe:
" split taes a string and inserts its fields into an array
split=string, array, field*separator@
" substr e6tracts a set of c0aracters out of a string
substr=string, start*position, +a6*lengt0@
" lengt0 returns t0e nu+ber of c0aracters in a string
lengt0=string@
-
8/11/2019 UNIX Utilities and Shell Programming Part1
95/155
C O N F I D E N T I A L )(
October 2!
awk% F"nctions
" 76a+ples:
Use substr to s0orten t0e data output
T
petna+e substr=[',',(@
print petna+e >Vt? [(
-
8/11/2019 UNIX Utilities and Shell Programming Part1
96/155
C O N F I D E N T I A L )8
October 2!
awk% F"nctions
" 76a+ples:
Use lengt0 to deter+ine t0e data output
$f =lengt0=['@ L (@T
petna+e substr=[',',(@
print petna+e >Vt? [(
else
Tprint [' >Vt? [(
-
8/11/2019 UNIX Utilities and Shell Programming Part1
97/155
C O N F I D E N T I A L )-
October 2!
C Shell Programming" Uses of t0e C s0ell
" 4unning C s0ell scripts" 5ocal variables" Obtaining user input" uoting" Using s0ell variables in awkand sed"
C s0ell redirection input, output, 3 error +essages" 0e ifstate+ent" Co++and line input" C s0ell arit0+etic" 0e whileloop" 0e foreachloop
" 0e switchstate+ent" Jlobal variables" 0e breakand continuestate+ents" 76it status
-
8/11/2019 UNIX Utilities and Shell Programming Part1
98/155
C O N F I D E N T I A L )9
October 2!
C Shell E$ercises
" 76ercise ) &riting a Si+ple C S0ell Script
" 76ercise ' uoting
" 76ercise '' 4edirection
" 76ercise '2 rueFalse Operators
" 76ercise '! Co++and 5ine Argu+ents
-
8/11/2019 UNIX Utilities and Shell Programming Part1
99/155
C O N F I D E N T I A L ))
October 2!
C Shell % Uses o1 the C Shell
" 0e C s0ell can be used as a co++and interpreteror a progra++ing language. $n t0is training, t0efocus is on t0e progra++ing aspects of t0e C s0ell.
" A co++and interpreter ls
file' file2 file!
" A progra++ing language cat firstscript
Z^ FbinFcs0
Z 0is is a co++ent7c0o >0is is +y first C s0ell script?
firstscript
0is is +y first C s0ell script
-
8/11/2019 UNIX Utilities and Shell Programming Part1
100/155
C O N F I D E N T I A L '
October 2!
C Shell % +"nning C Shell
Scri,ts0ere are ! ays to run a C s0ell script:'. Use t0e source co++and. 0is runs t0e script
co++ands in t0e current s0ell. source +yscript
2. ype in t0e script na+e at t0e pro+pt. 0is starts upa sub*s0ell in 0ic0 to run t0e +yscript co++ands. +yscript
!. 1recede t0e script na+e it0 FbinFcs0. 0is startsup a sub*s0ell in 0ic0 to run +yscript co++ands.
FbinFcs0 +yscript
#ote: you +ust 0ave read 3 e6ecute per+issions to use tec0niue 2,
read per+ission to use tec0niue ' or !
-
8/11/2019 UNIX Utilities and Shell Programming Part1
101/155
C O N F I D E N T I A L ''
October 2!
C Shell % Local 6ariables
" 5ocal variables:
" Created it0 t0e setco++and
" isible only to your current s0ell
" Use setco++and to see all local variables
" 76a+ples:
set abc >'2!?
ec0o [abc
'2!
-
8/11/2019 UNIX Utilities and Shell Programming Part1
102/155
C O N F I D E N T I A L '2
October 2!
C Shell % Local 6ariables
" 76a+ples:
set
cd ForingFtraining
0istory 20o+e Fe6portF0o+eF
noclobber
pat0 =FusrFbin usrFlocalFbin Fe6portF0o+eFbin .@
pro+pt s0ell FbinFcs0
abc '2!
-
8/11/2019 UNIX Utilities and Shell Programming Part1
103/155
C O N F I D E N T I A L '!
October 2!
C Shell % Local 6ariables
" $f t0ere is any roo+ for confusion, use curly bracesaround your variable na+e.
" 76a+ple: ec0o [Tabc/(8
'2!/(8
-
8/11/2019 UNIX Utilities and Shell Programming Part1
104/155
C O N F I D E N T I A L '/
October 2!
C Shell % Obtaining User In,"t
" Obtaining user input interactively, 0ile t0e script isrunning.
cat +yscript
Z^ FbinFcs0ec0o n >$nput a value: >
set na+e [N
ec0o [na+e
+yscript
$nput a value: &2!? %3riting a Sim,le C
Shell Scri,t!. Eave your script ec0o t0e folloing to t0e user:
C0ecing
'. Add a c0ec or deposit2. 4e+ove a c0ec or deposit
!. Calculate Balance
C0oose an Option:
/. 4ead t0e users response into a variable.
-
8/11/2019 UNIX Utilities and Shell Programming Part1
107/155
C O N F I D E N T I A L '-
October 2!
E$ercise > %3riting a Sim,le C
Shell Scri,tAnser:Z^ FbinFcs0
Z Brian M. Appold
Z c0ecing.cs0
Z 0is script +anages a c0ecing account
Z 5ast updated: Oct '! 2!
ec0o >C0ecing?
ec0o >?
ec0o >'. Add a c0ec or deposit?
ec0o >2. 4e+ove a c0ec or deposit?
ec0o >!. Calculate Balance?ec0o >?
ec0o n >C0oose and Option: >
set c0oice [N
-
8/11/2019 UNIX Utilities and Shell Programming Part1
108/155
C O N F I D E N T I A L '9
October 2!
C Shell 0 "oting
" uoting
Q uotes everyt0ing e6cept
> uotes everyt0ing e6cept , , and [ run co++and first, insert output into
surrounding co++and
V uotes ne6t ' c0aracter =including @
-
8/11/2019 UNIX Utilities and Shell Programming Part1
109/155
C O N F I D E N T I A L ')
October 2!
C Shell 0 "oting
" 76a+ples:
" Set*up variable
set var' >abc?
" Double uotes dont uote [, single uotes do
ec0o Q[var' [var'
ec0o >[var'? abc
" 0e bac uote c0aracter is different t0an singleuote c0aracter
ec0o date Sun #ov '2 22:!:!- 1S '))(
-
8/11/2019 UNIX Utilities and Shell Programming Part1
110/155
C O N F I D E N T I A L ''
October 2!
C Shell 0 "oting
" #eit0er single or double uotes or on t0e ^C0aracter. Only t0e bacslas0 ill or.
ec0o 0ello^t0ere
t0ere: 7vent not found
ec0o >0ello^t0ere?
Un+atc0ed >.
ec0o Q0ello^t0ere
Un+atc0ed Q.
ec0o 0elloV^t0ere
0ello^t0ere
-
8/11/2019 UNIX Utilities and Shell Programming Part1
111/155
C O N F I D E N T I A L '''
October 2!
C Shell 0 "oting
" Single and double uotes bot0 protect t0ebacslas0 c0aracter = V @.
ec0o Vvar' var' ec0o QV[var' V[var'
ec0o >V[var'? Vabc
C S S
-
8/11/2019 UNIX Utilities and Shell Programming Part1
112/155
C O N F I D E N T I A L ''2
October 2!
C Shell % Using Shell 6ariables
in sedand awk" $n +ost cases, you can use a s0ell variable by ending yoursingle uoted string rig0t before t0e variable, t0en start it upagain rig0t after.
" 76a+ple: set lineno 2
a Q#4',#4Q[lineno Tprint [ petc0ip
Cory =Big Boy@:cat:DSE:!F'9F'))):(:red:S+it0:)(!(
Spuny:dog:Jer+an S0eppard:8F'(F2:-:Gi+:4eed:)/(!8
sed n Q',[linenop petc0ip
Cory =Big Boy@:cat:DSE:!F'9F'))):(:red:S+it0:)(!(
Spuny:dog:Jer+an S0eppard:8F'(F2:-:Gi+:4eed:)/(!8
C Sh ll U i Sh ll 6 i bl
-
8/11/2019 UNIX Utilities and Shell Programming Part1
113/155
C O N F I D E N T I A L ''!
October 2!
C Shell % Using Shell 6ariables
in sedand awk" Eoever, if your variable is a string containing spaces,you +ust include double uotes around t0e variable.
" 76a+ple:
set na+e QBig Boy
a : QF?[na+e?F Tprint [ petc0ip
Cory =Big Boy@:cat:DSE:!F'9F'))):(:red:S+it0:)(!(
sed n QF?[na+e?Fp petc0ipCory =Big Boy@:cat:DSE:!F'9F'))):(:red:S+it0:)(!(
C Sh ll U i Sh ll 6 i bl
-
8/11/2019 UNIX Utilities and Shell Programming Part1
114/155
C O N F I D E N T I A L ''/
October 2!
C Shell % Using Shell 6ariables
in sedand awk" or awk: $f your variable is a string containing spacesand you are inside a print state+ent, t0en synta6 is +oreco+ple6. $nclude one double uote rig0t before your firstsingle uoted string ends, and one rig0t after yoursecond single uoted string begins.
" 76a+ple:
set +ypet >`our ani+als na+e is >
a : QTprint >?[+ypet?? [' petc0ip
`our ani+als na+e is Cory =Big Boy@
`our ani+als na+e is SpunyK
#ote: t0is breas into ! pieces QTprint > >[+ypet? Q? ['
-
8/11/2019 UNIX Utilities and Shell Programming Part1
115/155
C O N F I D E N T I A L ''(
October 2!
E$ercise & %"oting
'. &rite a sedco++and to list lines containing t0estring stored in t0e variable [petna+e.
Anser: sed n QF?[petna+e?Fp petc0ip
2. &rite an awkco++and to list t0e na+es of pets0ose +a6i+u+ e+ergency costs can e6ceed t0evalue stored in [+a6a+ount.
Anser: a : Q[( L?[+a6a+ount? Tprint [' petc0ip
C S
-
8/11/2019 UNIX Utilities and Shell Programming Part1
116/155
C O N F I D E N T I A L ''8
October 2!
C Shell 0 +edirection
" 4edirecting outputL redirect output
L3 redirect output and errors
LL append
LL3 append output and errors
] pipe
" o prevent accidentally over*riting files it0
redirection, set noclobber. o over*ride noclobber,use an e6cla+ation point =^@L^ or L3^
C Sh ll + di ti
-
8/11/2019 UNIX Utilities and Shell Programming Part1
117/155
C O N F I D E N T I A L''-
October 2!
C Shell 0 +edirection
" $f noclobber is set, t0e append sy+bol ill only appendto t0e end of an e6isting file. `ou need to use ane6cla+ation point if you ant it to create t0e file.
LL^
LL3^" Multiple co++and redirection
ls W date W 0o L outfile
=ls W date W 0o@ L outfile
=ls bananas L outfile@ L3 errfile" 4edirection input
N
C Sh ll + di ti
-
8/11/2019 UNIX Utilities and Shell Programming Part1
118/155
C O N F I D E N T I A L''9
October 2!
C Shell 0 +edirection
" #ote: &0en t0e s0ell sees t0e redirection sy+bol =Lor L3@, t0e s0ell Ieros out t0e file being ritten to if ite6ists, or creates an e+pty file if it does not. `oullnotice t0is 0en:
'. `our interrupt a progra+ it0 \C before any output isgenerated t0e e+pty file gets created
2. `ou >sort abc L abc? 0e file abc is Ieroed out before
being sorted. `ou inadvertently erase your file.
E i && + di ti
-
8/11/2019 UNIX Utilities and Shell Programming Part1
119/155
C O N F I D E N T I A L'')
October 2!
E$ercise && %+edirection
Anser t0e folloing uestions for eac0 of t0ee6a+ples belo. &0at files are created by t0eco++and, and 0at infor+ation is stored in eac0 fileX
'. =ls L outfile8@ L3 outfile -Anser: outfile8 created, contains ls output
outfile- created, contains errors
2. ==ls W date W 0o@ L outfile9@ L3 outfile)Anser: outfile9 created, contains output of ls, date,
and 0o co++ands
outfile) created, contains errors
E i && + di ti
-
8/11/2019 UNIX Utilities and Shell Programming Part1
120/155
C O N F I D E N T I A L'2
October 2!
E$ercise && %+edirection
!. &0at is t0e difference beteen t0e folloing 2co++andsX
ls L +ore
ls ] +oreAnser: 'st rites to file +ore
2nd pipe ls output to +ore
/. &0at ill 0appen if you run t0is co++andX
sort +yfile L^ +yfile
Anser: get an e+pty file, +yfile gets erased
E i && + di ti
-
8/11/2019 UNIX Utilities and Shell Programming Part1
121/155
C O N F I D E N T I A L '2'
October 2!
E$ercise && %+edirection
(. &0y +ig0t you set up a co++and t0is ayX
=find F *na+e abc print L abc.out@ L3 FdevFnull
Anser: errors go to a >blac 0ole?
8. &0at does t0is co++and doX
=find F *na+e abc print L Qtty@ L3 FdevFnullAnser: output goes to tty =ter+inal nu+ber@
errors go to >blac 0ole?
C Sh ll Th if St t t
-
8/11/2019 UNIX Utilities and Shell Programming Part1
122/155
C O N F I D E N T I A L '22
October 2!
C Shell % The ifStatement
" Use an ifstate+ent to perfor+ an operation if avalue is >true?. or t0e C s0ell ifstate+ent, true+eans ' =i.e. non*Iero@ and false +eans .
" Synta6
if = [var '(@ t0en
ec0o >eurea?
endif
C Sh ll Th if St t t
-
8/11/2019 UNIX Utilities and Shell Programming Part1
123/155
C O N F I D E N T I A L '2!
October 2!
C Shell % The ifStatement
if ='@ t0en
ec0o >t0is is true?
else
ec0o >t0is is false?endif
#ote:
then+ust be on t0e sa+e line as if, or else use a bacslas0 =V@`ou +ust include spaces around t0e trueFfalse operators suc0 as>>
C Sh ll Th it St t t
-
8/11/2019 UNIX Utilities and Shell Programming Part1
124/155
C O N F I D E N T I A L '2/
October 2!
C Shell % The exitStatement
" o stop e6ecuting a script, use t0e exitstate+ent.
" Synta6
if = [anser >uit?@ t0en
e6it
endif
#ote: you can c0ec t0e e6it status using [status =i.e. ec0o[status@
C Sh ll T 8F l O t
-
8/11/2019 UNIX Utilities and Shell Programming Part1
125/155
C O N F I D E N T I A L '2(
October 2!
C Shell % Tr"e8False O,erators
" #u+bers onlyL , L , N , and N
" #u+bers and strings
, ^" Strings
, ^
Set 6 >0i t0ere?
$f = >[6? 0iR @ t0en K.'. 0e ildcard c0aracters used 0ere are not regular e6pressions, t0ey
are t0e file na+e idcards used in co++ands suc0 as ls
2. Dont use uotation +ars around t0e 0iR 0ere
C Sh ll T 8F l O t
-
8/11/2019 UNIX Utilities and Shell Programming Part1
126/155
C O N F I D E N T I A L '28
October 2!
C Shell % Tr"e8False O,erators
" Co+binations33 logical A#D
]] logical O4
$f = [var 2( 33 [var2 L 2( @ t0en K
" ile attributes`ou can test a files attributes: r =read@, =rite@, 6=e6ecutable@, e =e6istence@, o =oners0ip@, I =Iero
lengt0@, f =plain file@, or d =directory@
$f = *r Ft+pF+yfile 33 *o Ft+pF+yfile @ t0en K.
C Shell 6ariables ( Constants
-
8/11/2019 UNIX Utilities and Shell Programming Part1
127/155
C O N F I D E N T I A L '2-
October 2!
C Shell % 6ariables ( Constants
" `ou can get a trueFfalse value by testing a variable:
[Xvar rue if variable 0as a value
alse if variable 0as not yet been
assigned a value
[var rue if variable is not Iero, variable
+ust be a nu+ber
alse if variable is Iero or null7rror +essage if variable is not a
nu+ber
C Shell 6ariables ( Constants
-
8/11/2019 UNIX Utilities and Shell Programming Part1
128/155
C O N F I D E N T I A L '29
October 2!
C Shell % 6ariables ( Constants
" 76a+ples:set var >a?
= [Xvar @ true
= [var @ synta6 error
= a @ synta6 error= >a? @ synta6 error
set var 9
= [Xvar @ true
= [var @ true= 9 @ true
= >9? @ true
C Shell 6ariables ( Constants
-
8/11/2019 UNIX Utilities and Shell Programming Part1
129/155
C O N F I D E N T I A L '2)
October 2!
C Shell % 6ariables ( Constants
" 76a+ples:set var
= [Xvar @ true
= [var @ false
= @ false= >? @ false
set var >?
= [Xvar @ true
= [var @ false= >? @ false
= @ false
-
8/11/2019 UNIX Utilities and Shell Programming Part1
130/155
E$ercise &2 Tr"e8False
-
8/11/2019 UNIX Utilities and Shell Programming Part1
131/155
C O N F I D E N T I A L '!'
October 2!
E$ercise &2 %Tr"e8False
O,erators!. =[Xvar'@Anser: rue
/. =[var'@
Anser: alse
(. =[Xvar!@
Anser: rue
8. =9(@
Anser: rue
-. =@
Anser: alse
-
8/11/2019 UNIX Utilities and Shell Programming Part1
132/155
C Shell Command Line In,"t
-
8/11/2019 UNIX Utilities and Shell Programming Part1
133/155
C O N F I D E N T I A L '!!
October 2!
C Shell % Command Line In,"t
" `ou can use variables ['*[) to access para+eterstyped after t0e script na+e
" [ 0olds t0e co++and na+e, [' 0olds t0e value of t0efirst argu+ent, and so on.
scriptna+e var' var2 var! var/
[ scriptna+e
[' var'
[2 var2
[! var!
[/ var/
C Shell Command Line In,"t
-
8/11/2019 UNIX Utilities and Shell Programming Part1
134/155
C O N F I D E N T I A L '!/
October 2!
C Shell % Command Line In,"t
" 76a+ple: cat +yscript
Z^ FbinFcs0
ec0o >your co++and as: [?
ec0o >your first para+eter as : ['?ec0o >all your para+eters ere: [R?
+yscript 0orray for captain spaulding
your co++and as: +yscript
your first para+eter as: 0orray
all your para+eters ere: 0orray for captain spaulding
C Shell Command Line In,"t
-
8/11/2019 UNIX Utilities and Shell Programming Part1
135/155
C O N F I D E N T I A L '!(
October 2!
C Shell % Command Line In,"t
" More variables associated it0 t0e co++and line:
[Zargv nu+ber of co++and line argu+ents
[R abbreviation for all para+eters
[argv sa+e as [
[argv;'< sa+e as ['K
[argv;)< sa+e as [)
[argv;'< note: not t0e sa+e as ['
[argv;''[' is odd?
endif
_var2 [2 2
if =[var2 @ t0en
ec0o >[2 is even?
else
ec0o >[2 is odd?
endif
set avg ec0o >scale2W=['[2@F2? ] bc
ec0o >0e average of t0e 2 nu+bers is [avg?
C Shell % The while Loo,
-
8/11/2019 UNIX Utilities and Shell Programming Part1
141/155
C O N F I D E N T I A L '/'
October 2!
C Shell % The whileLoo,
" $f you ould lie a set of co++ands to e6ecuterepeatedly, you can use a whileloop.
" Synta6
set nu+ber '
0ile = [nu+ber N '8 @
ec0o [nu+ber
_ nu+ber
end
C Shell % The while Loo,
-
8/11/2019 UNIX Utilities and Shell Programming Part1
142/155
C O N F I D E N T I A L '/2
October 2!
C Shell % The whileLoo,
" 76a+pleset badinput >true?0ile = >[badinput? >true? @
ec0o >7nter so+e input: >
set input [N
if = >[input? >?@ t0en Z user did not enter a value
set badinput >true?
ec0o >Bad input, try again?
ec0o >Eit return to continue?
set Pun [N Z pause t0e screen
clear Z clear t0e screen
else Z if user entered a valueset badinput >false?
endif
end
-
8/11/2019 UNIX Utilities and Shell Programming Part1
143/155
C Shell % The switch Statement
-
8/11/2019 UNIX Utilities and Shell Programming Part1
144/155
C O N F I D E N T I A L '//
October 2!
C Shell % The switchStatement
" So+eti+es youll need to test a value for+any different values. `ou can eit0er rite+any ifstate+ents, or you can use switch
state+ent instead.
C Shell % The switch Statement
-
8/11/2019 UNIX Utilities and Shell Programming Part1
145/155
C O N F I D E N T I A L '/(
October 2!
C Shell % The switchStatement
" Synta6sitc0 =[userna+e@
case ;7e
-
8/11/2019 UNIX Utilities and Shell Programming Part1
146/155
C O N F I D E N T I A L '/8
October 2!
C Shell #lobal 6ariables
" Created it0 t0e setenvco++and
" isible to your current s0ell and all your c0ildprocesses
" Use printenvor envto see all globalvariables
" Jlobal variables are auto+atically passed to
your scripts, local variables are not
C Shell % The PAT 6ariable
-
8/11/2019 UNIX Utilities and Shell Programming Part1
147/155
C O N F I D E N T I A L '/-
October 2!
C Shell The PAT 6ariable
" 0e 1AE variable contains a list of directories to besearc0ed for co++ands typed at t0e pro+pt orincluded in your scripts. o ensure t0at yourco++ands ill be found 0en your script is run, you
can define t0e 1AE at t0e top of your script." Set using t0e global variable 1AE
setenv 1AE FusrFbin:Fbin:FusrFucb:.
setenv 1AE [T1AE:FusrFlocalFbin
" Set using t0e local variable pathset pat0 =FusrFbin Fbin FusrFucb .@
set pat0 =[pat0 FusrFlocalFbin@
C Shell % The break ( continue
-
8/11/2019 UNIX Utilities and Shell Programming Part1
148/155
C O N F I D E N T I A L '/9
October 2!
C Shell The break( continue
Statements0e breakand continuestate+ents are used insidelooping structures suc0 as a whileloop. =0ey alsoor in a foreachloop@
" continue +oves you bac to t0e beginning oft0e 0ile loop
" break +oves you out past t0e 0ile loop
C Shell % The break ( continue
-
8/11/2019 UNIX Utilities and Shell Programming Part1
149/155
C O N F I D E N T I A L '/)
October 2!
" 76a+ple:0ile = [var! ( @
ec0o >enter a value?
set anser [N
if =[Xanser@ t0en continue
else
if =[anser >uit?@ t0en
brea
endifK
end
C Shell The break( continue
Statements
C Shell % E$it Stat"s
-
8/11/2019 UNIX Utilities and Shell Programming Part1
150/155
C O N F I D E N T I A L '(
October 2!
C Shell E$it Stat"s
U#$% co++ands 0ave an e6it status
" Access t0roug0 $status
" #ote: +ost U#$% co++ands ill return a
Iero if successful. 0is translates into afalse if you ant to use it in an ifstate+ent.
Ac4noBledgements
-
8/11/2019 UNIX Utilities and Shell Programming Part1
151/155
C O N F I D E N T I A L '('
October 2!
Ac4noBledgements
" 1resentation adapted fro+ UC 76tensionclass entitled >Advanced U#$% Utilities andS0ell 1rogra++ing?, instructor Susan Barr
A,,endi$ A
-
8/11/2019 UNIX Utilities and Shell Programming Part1
152/155
C O N F I D E N T I A L '(2
October 2!
A,,endi$ A
" 0e >petc0ip? file:Cory =Big Boy@:cat:DSE:!F'9F'))):(:red:S+it0:)(!(
Spuny:dog:Jer+an S0eppard:8F'(F2:-:Gi+:4eed:)/(!8
Maggy:dog:5ab Mi6:)F2!F2':'2:o+:Hones:)(''-
1retIel:dog:errier Mi6:)F!F'))8:(:5ena:Eatc0er:)/98Milly:cat:DSE:'F2F22:!:Betty:S+it0:)/(!)
Otis:cat:DSE:'F2F22:!:Ben:Stiller:/9-!/
red:ferret::'2F/F2':':Brian:Gennedy:/-'(/
5ucy:cat:D5E:(F2'F'))(:(:ran:Andres:)/9(2
S0elby:dog:Jolden 4etriever:-F!F22:'2:Diane:5ee:9(82!
#ote:ile or+at petna+e:species:breed:DOB:vete6penseli+it:onerfna+e:onerlna+e:id
A,,endi$ 5
-
8/11/2019 UNIX Utilities and Shell Programming Part1
153/155
C O N F I D E N T I A L '(!
October 2!
A,,endi$ 5" 0e >grepfile? file:
0is line contains a nu+ber: '2!/
0is line ends in an %
0is line ends in a dollar sign [
\ 0is line begins it0 a carat
0is line ends in a carat \Ei, +y na+e is Bob
Ei, +y na+e is Bib
Ei, +y na+e is Babs
0is is a date in t0e eig0ties :9F'!F9/:
0is is a date in t0e nineties :-F(F)2:Eere is an :A:
Eere are ! :AAA:s
Eere are ( :AAAAA:s
A,,endi$ C
-
8/11/2019 UNIX Utilities and Shell Programming Part1
154/155
C O N F I D E N T I A L '(/
October 2!
A,,endi$ C
" 0e >c0ecing? file:
'':'F!F'))/:A+er. 76press:2/!.
'2:2F'F'))/:Citiban isa:'/.)9
'!:2F'2F'))/:1ac. Jas37lec.:!).-'/:2F'2F'))/:1acific Bell:8(.((
D2:2F'/F'))/:ABC $nc.:'/.
D':'F!F'))/:$4S:(/.
#ote: ile for+at c0ec nu+ber:date:na+e of party:a+ount
-
8/11/2019 UNIX Utilities and Shell Programming Part1
155/155