of 105
8/12/2019 UNIX for Intermediate Users
1/105
UNI X f or I nt er medi at e Users
Devel oped by:
User Li ai son Sect i on, D- 7131
[ Name and number s r emoved at aut hor ' s r equest ]
Revi si on Dat e:
TABLE OF CONTENTS
I . I NTRODUCTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i iA. Audi ence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i iB. Cour se Obj ect i ves. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i iC. Cour se Handout Convent i ons. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i i i
1. THE FI LE CALLED . pr of i l e AND PROCESSES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11. 1 HOME. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11. 2 PATH. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21. 3 I NGRES Envi r onment Var i abl es. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21. 4 I NG_HOME. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31. 5 TERM_I NGRES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31. 6 I NG_EDI T. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31. 7 Pr ocesses. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41. 8 Execut i ng a Command. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41. 9 Pr ocess I dent i f i cat i on. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51. 10 I nt er r upt Handl i ng. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2. COMPI LI NG " C" PROGRAMS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102. 1 "C": Sampl e Pr ogr am wi t h a Mai n and Two Funct i ons
8/12/2019 UNIX for Intermediate Users
2/105
i n One . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102. 2 " C" : Compi l i ng a Pr ogr am. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122. 3 " C" : Renami ng t he Execut abl e Modul e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132. 4 "C": Gi vi ng a Name t o t he Out put Fi l e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142. 5 "C": Pr oduci ng an Ass embl y Li st i ng. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152. 6 "C": Mai n and Two Funct i ons i n Thr ee Separ at e
Sour ce Fi l es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162. 7 "C": Compi l i ng but Not Pr oduci ng an Execut abl e
Modul e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3. COMPI LI NG FORTRAN PROGRAMS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183. 1 FORTRAN: Sampl e Pr ogr am a Mai n and TwoSubr out i nes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3. 2 FORTRAN: Compi l i ng a Pr ogr am. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193. 3 FORTRAN: Renami ng t he Execut abl e Modul e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203. 4 FORTRAN: Gi vi ng a Name t o t he Out put Fi l e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213. 5 FORTRAN: Pr oduci ng an Ass embl y Li s t i ng. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223. 6 FORTRAN: Mai n and Two Subr out i nes i n Thr ee
Separ at e Sourc e Fi l es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233. 7 FORTRAN: Compi l i ng But Not Pr oduci ng an Execut abl e
Modul e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243. 8 FORTRAN: Compi l i ng Obj ect Fi l es t o Pr oduce an
Exec ut abl e Modul e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
4. COMPI LI NG COBOL PROGRAMS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264. 1 COBOL: Sampl e Pr ogr am wi t h a Mai n and TwoSubr out i nes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
4. 2 COBOL: Compi l i ng a Pr ogr am. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274. 3 COBOL: Runni ng a Pr ogr am. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28Wor kshop 2- 4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
5. UNI X TOOLS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345. 1 The make Ut i l i t y. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
p: A Patt er n Mat chi ng Fi l t er . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375. 2. 1 More on Regul ar Expr essi ons. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385. 2. 2 Cl osur e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425. 2. 3 Some Ni ce gr ep Opt i ons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435. 2. 4 Summar y of Regul ar Expr essi on Char act er s. . . . . . . . . . . . . . . . . . . . . . . . . . . 44
5. 3 sed: Edi t a Fi l e t o Standard Output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455. 4 awk: A Pat t er n Mat chi ng Pr ogr ammi ng Language. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495. 5 sor t : Sort a Fi l e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535. 6 Ar chi ver and Li br ar y Mai nt ai ner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565. 7 Cr eat i ng an Ar chi ve Fi l e wi t h Obj ect Modul es. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575. 8 Veri f yi ng t he Cont ent s of t he Ar chi ve Fi l e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575. 9 Removi ng Dupl i cat e Obj ect Fi l es. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585. 10 Compi l i ng Mai n and Ar chi ve Fi l es. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58Wor kshop 5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
6. UNI X UTI LI TI ES PART I - DI SPLAY AND MANI PULATE FI LES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
7. UNI X UTI LI TI ES PART I I - DI SPLAY AND ALTER STAUTS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
8. UNI X UTI LI TI ES PART I I I - MI SCELLANEOUS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
9. ADVANCED FEATURES OF FTP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 909. 1 I ni t i al i zi ng FTP on UMAX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 919. 2 Mul t i pl e Fi l e Tr ansf er s. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 929. 3 Auto Logi n Feat ur e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 939. 4 Macr os. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 959. 5 Fi l ename Tr ansl at i on. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 969. 6 Abor t i ng Tr ansf er s. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 979. 7 Mor e Remot e Comput er Commands. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98Wor kshop 10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
APPENDI X A - sh. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
8/12/2019 UNIX for Intermediate Users
3/105
APPENDI X B - f t p. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
APPENDI X C - C Compi l er . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
APPENDI X D - FORTRAN Compi l er . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
APPENDI X E - l i nt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
APPENDI X F - cb. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
APPENDI X G - ar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
I NDEX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
I . I NTRODUCTI ON
A. Audi ence
Thi s cour se i s f or i ndi vi dual s who need t o use ut i l i t i es andadvanced f eat ures of t he UNI X oper at i ng s yst em.
B. Cour se Obj ect i ves
Upon successf ul compl et i on of t hi s cour se t he st udent wi l l beabl e t o:
1. Compi l e C, FORTRAN, and COBOL pr ogr ams.
2. Cr eat e pr oc es s es to r un i n t he bac kgr ound
3. Use advanced f eat ur es of FTP such as: mul t i pl e f i l et r ansf er s, aut o l ogi ns, macr os, gl obbi ng, f i l enamet r ansl at i on, abort i ng t r ansf ers, and ot her r emote
comput er commands .
4. Use UNI X ut i l i t y progr ams such as grep, sed, awk, sort ,and ot hers .
5. Us e t he make ut i l i t y.
6. Under st and pr ocesses, i ncl udi ng str uct ur e, execut i ng acommand, process i denti f i cati on, exi t st at us, pl us .( dot) and exec processi ng.
C. Cour se Handout Convent i ons
There ar e sever al convent i ons used i n t hi s handout f or
consi st ency and easi er i nter pret ati on:
1. Sampl es of act ual t er mi nal sessi ons ar e s i ngl e- l i nedboxed.
2. Us er ent r i es a re s hown i n bol d pr i nt and areunder l i ned.
exi t
3. Al l keyboar d f unct i ons i n t he t ext wi l l be bol d.
8/12/2019 UNIX for Intermediate Users
4/105
( Ret ) BackspaceTab Ct r l - F6Pr i nt ( Shi f t - F7) Go t o DOS ( 1)
NOTE: ( Ret ) i ndi c at e s t he Ret u r n or Ent er k eyl ocat ed above t he r i ght Shi f t key.
4. Exampl es of user ent r i es not showi ng t he comput er' s
r esponse ar e i n dot t ed- l i ned boxes.
5. Command f or mat s are doubl e- l i ned boxed.
6. Thr ee dot s ei t her i n ver t i cal or hori zont al al i gnmentmean cont i nuat i on or t hat dat a i s mi ss i ng f r om di agr am.
Mul t i max, Nanobus, and UMAX ar e t r ademar ks of Encor e Comput er Cor por at i on.
Annex i s a t r ademar k of XYLOGI CS, I nc.
UNI X and Tel et ype ar e r egi st ered t r ademarks of AT&T Bel l Labor at or i es
Et hernet i s a t r ademark of Xer ox Corpor ati on
1. UNI X PROCESSES AND A FI LE CALLED . prof i l e
1. 1 Processes
A pr ocess i s t he execut i on of a command by UNI X. Pr ocesses canal so be execut ed by t he oper ati ng syst em i t sel f . Li ke t he f i l es tr uc tu re, th e p roces s s t r uc tu re i s hi era rc hi c al . I t c ont ai nspar ent s, chi l dr en, and even a r oot . A par ent can f ork ( or spawn)
a chi l d pr ocess. That chi l d can, i n t urn, f ork ot her pr ocesses.The f i r st t hi ng t he oper at i ng sys t em does t o begi n execut i on i st o cr eat e a si ngl e pr ocess, PI D number 1. PI D st ands f or ProcessI denti f i cat i on. Thi s process wi l l hol d t he same posi t i on as t heroot di re c tory i n t he f i l e s t r uc tu re. Thi s pr oc es s i s t heancest or t o al l pr ocesses t hat each user works wi t h. I t f orks apr ocess f or each t ermi nal . Each one of t hese pr ocesses becomes aShel l process when t he user l ogs i n.
1. 2 Process I dent i f i cat i on
The UNI X oper at i ng sys t em as si gns a uni que pr oces s i dent i f i cat i onnumber ( PI D) t o each process. I t wi l l keep t he same PI D as l ong
8/12/2019 UNIX for Intermediate Users
5/105
as t he pr ocess i s i n exi st ence. Dur i ng one sessi on, t he samepr ocess i s al ways execut i ng t he l ogi n Shel l . When you execut eanot her command, a new pr ocess i s f or ked and a new PI D i sassi gned t o t hat pr ocess. When t hat chi l d pr ocess i s f i ni shed,you ar e r etur ned t o t he l ogi n pr ocess, whi ch i s r unni ng t heShel l , and t hat par ent process has t he same PI D as when youl ogged i n.
The Shel l st or es t he PI D i n Shel l var i abl e cal l ed $$. The PI D
can al so be shown wi t h t he process st at us ( ps) command. Thef or mat f or ps i s as f ol l ows:
Command For mat : ps [ opt i ons]
See on- l i ne manual f or opt i ons
Wi t h no opt i ons gi ven t he ps command wi l l gi ve you cer t ai ni nf or mati on about processes associ at ed wi t h t he cont r ol l i ngt ermi nal . The out put consi st s of a shor t l i st i ng cont ai ni ng t hepr ocess i d, t ermi nal i d, cumul at i ve execut i on t i me, and t he
command name. Ot her wi se, opt i ons wi l l cont r ol t he di spl ay.
Sampl e ses si on:
$echo $$ 8347 $ps PI D TTY TI ME COMMAND 8347 r t 021a0 0: 03 ksh 8376 r t 021a0 0: 06 ps $
The PI D number s of t he Shel l ar e t he same i n t he sampl e sessi onbecause t he Shel l wi l l subst i t ute i t s own PI D number f or $$.
The Shel l makes t he subs t i t ut i on bef or e i t f or ks a new pr oces s t oexecut e t he echo command. Ther ef or e, echo wi l l di spl ay t he PI Dnumber of t he pr ocess t hat cal l ed i t , not t he PI D of t he pr ocesst hat i s execut i ng i t .
The - l opt i on wi l l di s pl ay mor e i nf or mat i on about t he pr oces ses.
Sampl e Sess i on:
$ps - l
F S UI D PI D PPI D C PRI NI ADDR SZ WCHAN TTY TI ME COMD f 0000 S 115 8347 309 2 30 20 1009000 140 94014 r t 021a0 0: 03 ks h f 0000 O 115 8386 8347 16 68 20 1308000 72 r t 021a0 0: 01 ps $ps - l F S UI D PI D PPI D C PRI NI ADDR SZ WCHAN TTY TI ME COMD f 0000 S 115 8347 309 1 30 20 1009000 140 94014 r t 021a0 0: 03 ks h f 0000 O 115 8387 8347 26 73 20 1146000 72 r t 021a0 0: 01 ps $
1. 3 Execut i ng a Command
8/12/2019 UNIX for Intermediate Users
6/105
When you gi ve a command t o t he Shel l , i t wi l l f ork a process t oexecut e t he command. Whi l e t he chi l d process i s execut i ng t hecommand, t he par ent wi l l go t o sl eep. Sl eepi ng means t hat t hepr ocess wi l l not use any CPU t i me. I t r emai ns i nact i ve unt i l i ti s awakened. When t he chi l d pr ocess has f i ni shed execut i ng t hecommand, i t di es. The par ent process, whi ch i s r unni ng t heShel l , wakes up and pr ompt s you f or anot her command.
When you r equest a process t o r un i n t he backgr ound ( by endi ngt he command l i ne wi t h an amper sand char acter ( &) , t he Shel l f orks
a chi l d pr ocess t hat i s al l owed t o r un t o compl eti on. The parentpr ocess wi l l r eport t he PI D of t he chi l d pr ocess and t hen pr omptyou f or anot her command. The chi l d and par ent are nowi ndependent pr ocess es.
1. 4 The . ( dot ) and exec Commands
There ar e t wo ways t o execut e a pr ogr am wi t hout f or ki ng a newpr ocess. The . ( dot ) command wi l l execut e t he scr i pt as par t oft he curr ent process. When t he new scr i pt has f i ni shed execut i ng,t he cur r ent pr ocess wi l l cont i nue t o execut e t he ori gi nal scr i pt .
The exec command wi l l execut e t he new scr i pt i n pl ace of( over l ays) t he ori gi nal scri pt and never r et ur ns t o t he ori gi nal
scri pt .
The . ( dot ) command wi l l not execut e compi l ed f i l es ( bi nar y) and i t does not r equi r e execut epermi ssi on on t he scr i pt f i l e t hat i s bei ng execut ed. The exec command does r equi r e accesspermi ssi on t o ei t her a bi nar y pr ogr am or a shel l scr i pt .
Sampl e ses si on:
$l s - l pr og2 - r w- r - - r - - 1 t eacher cl ass 22 J an 18 10: 30 prog2 $cat pr og2 echo ' pr og2 PI D =' $$
$cat dot _exampl e echo $0 ' PI D=' $$ . pr og2 echo ' Thi s l i ne i s execut ed' $dot _ exampl e dot _ exampl e PI D= 6942 prog2 PI D = 6942 Thi s l i ne i s execut ed $
The exec command wi l l over l ay t he sh and cont r ol wi l l never r et ur n t o t he cal l i ng scr i pt .Let ' s l ook at another exampl e wi t h a cal l t o pr og2 usi ng exec i nst ead of . ( dot) :
Sampl e ses si on:
$l s - l pr og2 - r wxr- xr- x 1 t eacher cl ass 22 J an 18 10: 30 prog2 $cat pr og2 echo ' pr og2 PI D =' $$ $cat exec _ exampl e echo $0 ' PI D=' $$ exec prog2 echo ' Thi s l i ne i s never execut ed' $exec _ exampl e exec_ exampl e PI D= 6950 prog2 PI D = 6950
8/12/2019 UNIX for Intermediate Users
7/105
$
Backgr ound Pr ocessi ng
When a pr ogr am i s r unni ng i n backgr ound you do not have t o wai t f or i t t o f i ni sh bef orest art i ng anot her program. Thi s i s usef ul because you can st ar t l ong/ l arge j obs and t hencont i nue t o do anot her t ask on your t ermi nal .
To r un a pr ogr am i n backgr ound si mpl y t ype an amper sand char act er ( &) at t he end of t hecommand l i ne bef or e t he ( Ret ) key. The Shel l wi l l r etur n t he PI D of t he background processand t hen gi ve you anot her syst em pr ompt.
Sampl e ses si on:
$l s - l | l p & [ 1] 21334 $request i d i s mt _600- 2736 ( st andar d i nput)
$
I f t he backgr ound t ask sends out put t o st andar d out put and you f ai l t o r edi r ect i t , t heout put wi l l appear on your t ermi nal even i f you are r unni ng anot her program at t he t i me.
I t i s necessary t o use t he ki l l command t o st op a pr ocess t hat i s r unni ng i n backgr ound t he( DEL) key or i t s equi val ent wi l l not work.
Exi t St at us
When a pr ocess st ops execut i ng f or any r eason, i t wi l l r eturn an exi t st at us t o t he par entpr ocess. Thi s exi t st atus i s al so r ef err ed t o as a condi t i on code or r eturn code. The Shel lst ores t he exi t st atus i n a Shel l var i abl e cal l ed $?. By convent i on, a non- zero exi t st atusmeans t hat i t has a f al se val ue and t he command f ai l ed. On t he other hand, a zero st atusi ndi cat es t r ue and t he command was successf ul .
I t i s possi bl e f or you t o speci f y t he exi t st at us when you exi t a scri pt . Thi s i s done byspeci f yi ng t he number t o be used as t he exi t st at us usi ng t he exi t command. The f ol l owi ngscr i pt i s an exampl e:
Sampl e Sess i on:
$cat exi t _exampl e echo ' Thi s pr ogr am r eturns an exi t st atus' echo ' of 7. ' exi t 7 $exi t _ exampl e Thi s pr ogr am r eturns an exi t st atus of 7.
$echo $? 7 $echo $? 0 $
Thi s scr i pt wi l l di s pl ay t he message and t hen exi t wi t h an exi t code of 7. The exi t st at usi s st or ed i n t he Shel l var i abl e cal l ed $?. The second echo command above di spl ays t he exi tst at us of t he f i r st echo command. Si nce i t compl et ed successf ul l y i t has a val ue of zer o.
1. 4 I nter r upt Handl i ng
8/12/2019 UNIX for Intermediate Users
8/105
A si gnal i s a r eport t o a pr ocess about a condi t i on. UNI X usest hese si gnal s t o r eport bad syst em cal l s, br oken pi pes, i l l egali nst r uct i ons, and ot her condi t i ons. Ther e ar e t hr ee si gnal s t hatare usef ul when pr ogr ammi ng i n t he Shel l . They are t he t er mi nali nt err upt si gnal ( number 2) , t he ki l l si gnal ( number 9) and t hesof t war e t er mi nat i on si gnal ( number 15).
You can use t he t r ap command t o capt ur e a si gnal and t hen t akewhatever act i on you speci f y. I t can cl ose f i l es or f i ni sh ot her
pr ocessi ng t hat needs t o be done, di spl ay a message, t ermi nat eexecut i on i mmedi at el y, or i gnor e t he si gnal .
Command For mat : t r ap [ ' commands' ] si gnal _ number s
See onl i ne man pages f or det ai l s
The si gnal _ number s ar e t he number s cor r espondi ng t o t he si gnal st hat wi l l be t r apped by t he t r ap command. Ther e must be at l eastone number pr esent . The ' commands' por t i on of t he command i sopt i onal . I f i t i s not pr esent , t he command r eset s t he t r ap t o
i t s i ni t i al condi t i on, whi ch i s t o exi t t he pr ogr am. When t hecommands i s pr esent t he Shel l execut es t he commands when i tcatches one of t he si gnal s. Af t er execut i ng the commands, t heShel l cont i nues execut i ng t he scr i pt wher e i t l ef t of f .
You can i nt er r upt a pr ogr am you ar e r unni ng i n t he f or egr ound bypr essi ng t he Del et e key. When you press t hi s key a si gnal( number 2) , a t ermi nal i nt er r upt , t o t he pr ogr am. The Shel l wi l lt ermi nate t he execut i on of t he pr ogr am i f t he pr ogr am does nott r ap t he si gnal . The f ol l owi ng exampl e demonst r at es t he t r apcommand t hat wi l l t r ap t he si gnal and r et ur n an exi t st atus of 1.
Sampl e ses si on:
$cat i nt er t r ap ' echo PROGRAM I NTERRUPTED; exi t 1' 2 whi l e ( t r ue) do echo ' Pr ogr am r unni ng' done $
The f i r st l i ne of i nt er set s up a t r ap f or si gnal number 2, t het ermi nal i nt err upt . When t he si gnal i s caught , t he Shel l wi l lexecut e t he commands bet ween t he t wo si ngl e quot e mar ks. I n t hi sexampl e, t he echo command wi l l di spl ay PROGRAM I NTERRUPTED. Theexi t command wi l l t hen r eturn cont r ol t o t he Shel l and a syst em
pr ompt i s di spl ayed. I f t he exi t wer e mi ssi ng, cont r ol woul dr evert t o t he whi l e l oop af t er di spl ayi ng t he message.
You can send a sof t war e t er mi nat i on t o a backgr ound pr oces s usi ngt he ki l l command wi t hout a s i gnal number . However , a t r apcommand can be s et t o cat ch t hi s si gnal ( number 15) . A ki l lsi gnal can be sent t o ki l l a pr ocess wi t h a si gnal number 9 andt he Shel l cannot cat ch a ki l l si gnal .
The f i l e cal l ed . pr of i l e
The Bour neShel l decl ar es and i ni t i al i zes var i abl es t hat det er mi nesuch t hi ngs as your home di r ect or y, what di r ector i es t he Shel l
8/12/2019 UNIX for Intermediate Users
9/105
wi l l l ook i n when you gi ve commands, how of t en t o l ook f or mai l ,your syst em prompt , and many ot her t hi ngs. We wi l l l ook at someof t hese Shel l var i abl es and t hei r f unct i ons. You can assi gn newval ues t o t hese var i abl es f r om t he command l i ne or by execut i ngt he cont ent s of a f i l e cal l ed . pr of i l e. The Bour neShel l execut est he commands i n t hi s f i l e i n t he same envi r onment as t he Shel leach t i me t he user l ogs i n. The . prof i l e must be i n t he user'home di r ect ory. Each user has a di f f erent . pr of i l e. I t usual l yspeci f i es t he t ermi nal t ype and est abl i shes t ermi nalcharact eri st i cs and other housekeepi ng f unct i ons as r equi r ed by
the user.
1. 5 HOME
The f i r st Bour neShel l var i abl e t hat we wi l l l ook at i s t he HOMEvar i abl e. By def aul t , t he home di r ect ory i s t he cur r ent worki ngdi r ect ory af t er you l ogi n. The syst em admi ni st r ator deter mi nesyour home di r ect or y when you est abl i sh an account and pl aces t hati nf or mati on i n t he / etc/ passwd f i l e. When you l ogi n, t heBour neShel l gets t hat pat hname and assi gns i t t o t he HOMEvar i abl e.
When you ent er a cd command wi t h no ar gument , t he ut i l i t y t akest he name of t he di r ector y f r om t he HOME var i abl e and makes i t t hecur r ent worki ng di r ect ory. I f you change t he HOME var i abl e t oanot her di r ector y pat hname, t he uti l i t y wi l l make t he newdi r ect ory t he cur r ent worki ng di r ect ory.
Sampl e Sess i on:
$echo $HOME / user 0/ r har di ng
$cd $pwd / user 0/ r har di ng $HOME=/ user 0/ r har di ng/ eng $cd $pwd / user 0/ r har di ng/ eng $
Thi s exampl e shows how t he val ue of t he HOME var i abl e af f ec t s t hecd ut i l i t y. The cd command wi l l use t he val ue of t he HOMEvar i abl e as t he pathname f or t he cur r ent worki ng di r ect ory.
1. 6 PATH
Thi s Bour neShel l var i abl e wi l l desc r i be t he di r ect or i es t hat wi l lbe sear ched l ooki ng f or t he pr ogr am t hat you want t o execut e.
The Bour neShel l l ooks i n sever al di r ect or i es f or a f i l e t hat hast he same name as t he c ommand t hat you ent er ed. The PATH var i abl econt r ol s t hi s sear ch pat h. Normal l y, t he f i rst di r ector ysear ched i s t he cur r ent worki ng di r ect ory. I f t he pr ogr am i s notf ound, t he sear ch cont i nues i n t he / bi n and t hen t he / usr / bi ndi r ect ory. General l y, t hese di r ect ori es cont ai n execut abl epr ogr ams. I f t he pr ogr am i s not f ound i n one of t hesedi r ect ori es, t he Bour neShel l r eport s t hat t he pr ogr am can' t bef ound ( or execut ed) .
8/12/2019 UNIX for Intermediate Users
10/105
The PATH var i abl e l i st s t he pat hnames i n t he or der i n whi ch t hesear ch wi l l pr oceed. The pathnames ar e separat ed by a col on ( : ) .I f nothi ng ( nul l st r i ng) pr ecedes t he col on, t hat i ndi cat es t ostart t he sear ch at t he cur r ent wor ki ng di r ect or y.
Exampl e:. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. $PATH=: / us er 0/ r har di ng/ bi n: / bi n: / us r / bi n .. $ .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Thi s PATH var i abl e i ndi c at es t o st ar t t he sear ch f or t he pr ogr amat t he cur r ent worki ng di r ect ory, t hen l ook i n t he di r ect ory/ user 0/ r har di ng/ bi n, t hen / bi n, and f i nal l y / usr / bi n.
I f each user has a uni que pat h speci f i ed, each user can execut e adi f f er ent pr ogr am by gi vi ng t he same command. The sear ch f or t hepr ogr am st ops when i t i s sat i sf i ed; t hus, you can use t he samename f or your own programs as t he st andar d UNI X uti l i t i es. To dot hi s, si mpl y put your pr ogr am i n one of t he f i r st di r ect ori est hat t he Bour neShel l sear ches.1. 7 I NGRES Envi r onment Var i abl es
There ar e some envi r onment var i abl es t hat need t o be i n t he. pr of i l e t hat set up I NGRES. The f ol l owi ng exampl es are gi ven asgeneral gui del i nes, not actual ent r i es t o be made i n your. prof i l e.
1. 8 I NG_ HOME
Thi s i s t he I NGRES home di r ect or y. Thi s var i abl e i s val i d f orver si on 5 of I NGRES. Thi s var i abl e i s set up i n t he f ol l owi ngmanner .
Exampl e:
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. $I NG_ HOME=/ user 5/ i ngr es .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Noti ce t hat t hi s envi r onment var i abl e i s al l capi t al l ett ers.Thi s i s a r equi r ement i n UNI X.
1. 9 TERM_ I NGRES
I f t hi s var i abl e i s not set, I NGRES wi l l use t he def aul t t ermi nalt ype def i ned by t he TERM var i abl e i n UNI X. I t i s not r equi r edbut di f f i cul t y i n usi ng t he mai n I NGRES menu can be exper i encedi f i t i s not used.
Exampl e:
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. $TERM_ I NGRES=vt 100f .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1. 10 I NG_ EDI T
8/12/2019 UNIX for Intermediate Users
11/105
Thi s var i abl e def i nes t he edi t or t o use any t i me a user ent er s acommand t hat r equi r es t he use of an edi t or. The def aul t i s t ous e t he ' ed' edi t or .
Exampl e:
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. $I NG_ EDI T=/ usr / bi n/ vi .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Wor ks hop 1
Thi s wor ks hop wi l l r ei nf or ce your under st andi ng of t he mat er i alpr esented i n t hi s chapter . Logi n usi ng t he user name and t hepassword gi ven t o you by t he i nst r uct or. Each st udent i s t o
compl et e t he ent i r e wor kshop.
DESK EXERCI SES
1. What i s t he name of t he f i l e t hat i s execut ed f r om yourhome di r ect or y every t i me you l og i n?
2. What does t he Shel l var i abl e HOME r epr esent ?
3. What does t he Shel l var i abl e PATH r epr esent?
4. What i s a UNI X process ?
5. When a command i s gi ven t o t he Shel l i t wi l l f ork a chi l dpr ocess t o execut e t he command.
Tr ue/ Fal se
6. What i s a process i dent i f i cat i on number ( PI D) ?
8/12/2019 UNIX for Intermediate Users
12/105
Cont i nue on t he next page7. What i s t he pur pose of t he t r ap command?
COMPUTER EXERCI SES
8. Logon
9. What i s t he PI D of your pr ocess?
10. Edi t t he . pr of i l e t o i ncl ude your home di r ect ory i n t hepat h.
11. Modi f y t he . prof i l e so every t i me you l ogi n a l i st i ng oft he f i l es i n your cur r ent worki ng di r ect ory ( HOME) i sdi spl ayed.
12. Send a l ong l i st i ng of al l t he f i l es i n t he cur r entworki ng di r ect ory t o t he def aul t pr i nt er and do i t i tt he background.
13. Logof f
NOTES 2. COMPI LI NG " C" PROGRAMS
Thi s chapt er wi l l exami ne compi l i ng sour ce code pr ogr ams i n t hr eehi gh l evel l anguages " C" , FORTRAN, and COBOL. The second par t oft he chapt er wi l l l ook at t he archi ve and l i br ary mai nt ai ner. Thear chi ve al l ows you t o cr eat e a l i br ary of obj ect modul es. Thesef i l es ar e used by t he l i nk edi t or.
2. 1 "C": Sampl e Pr ogr am wi t h a Mai n and Two Funct i ons i n OneFi l e
8/12/2019 UNIX for Intermediate Users
13/105
Based on t he command l i ne opt i ons, cc compi l es, ass embl es, andl oads C l anguage sour ce code pr ogr ams. I t can al so assembl e andl oad assembl y l anguage sour ce pr ogr ams or mer el y l oad obj ectpr ogr ams.
Command For mat : cc [ opt i ons] f i l e- l i st
( See Appendi x E f or a compl et e l i st of opt i ons)
When usi ng t he cc uti l i ty, t he f ol l owi ng convent i ons areobser ved:
1. A f i l ename wi t h t he extensi on of . c i ndi cat es a Cl anguage sour ce pr ogr am.
2. A f i l ename wi t h an extensi on of . s i ndi cat es anass embl y l anguage sour ce pr ogr am.
3. A f i l ename wi t h an ext ensi on of . o i ndi cat es an obj ectpr ogr am.
The cc ut i l i t y wi l l t ake i t s i nput f r om t he f i l e or f i l es youspeci f y on t he command l i ne. Unl ess you use t he - o opt i on, i twi l l st or e t he execut abl e pr ogr am i n a f i l e cal l ed a. out .Sampl e C L anguage Sour ce Code Pr ogr am:
$cat hel l o. c mai n ( ) { pr i ntf ( " Hel l o f r om mai n!\ n\ n") ; pr i ntf ( " Cal l i ng f unct i on1! \ n\ n") ; f unct 1( ) ; pr i nt f ( "\ t Back f r om f unct i on1! \ n\ n") ; pr i ntf ( " Cal l i ng f unct i on2! \ n\ n") ;
f unct 2( ) ; pr i nt f ( "\ t Back f r om f unct2!\ n\ n") ; pr i nt f ( "That ' s al l ! \ n\ n") ; } f unct 1( ) { pr i nt f ( "\ t \ t Hel l o f r om f unct i on1! \ n\ n) ; } f unct 2( ) { pr i nt f ( "\ t \ t Hel l o f r om f unct i on2! \ n\ n) ; } 2. 2 "C": Compi l i ng a Pr ogr am
To compi l e t he pr evi ous exampl e pr ogr am i nt o an execut abl emodul e, ent er t he f ol l owi ng command at t he command l i ne.
Sampl e Sess i on:
$cc hel l o. c $
Wi t hout any opt i ons, cc accepts C s our ce code and assembl yl anguage programs t hat f ol l ow the convent i ons out l i ned above. I twi l l compi l e, assembl e, and l oad t hese progr ams t o produce an
8/12/2019 UNIX for Intermediate Users
14/105
8/12/2019 UNIX for Intermediate Users
15/105
2. 4 "C": Gi vi ng a Name t o t he Output Fi l e
I t i s possi bl e t o have t he out put sent t o a f i l e you speci f yi nst ead of a. out by usi ng t he f ol l owi ng command.
Command For mat : cc - o out put sour ce
out put - t he name of t he execut abl e f i l e sour ce - t he name of t he C sour ce code f i l e
The - o opt i on t el l s cc t o t el l t he l i nk edi t or t o use t hespeci f i ed name f or t he out put i nst ead of t he def aul t a. out .
NOTE: I t i s not neces sar y f or t he - o opt i on t o appear af t ert he cc command. The f i l ename t hat appears af t er t he - oi s t he name of t he out put f i l e. For exampl e, cc sour ce- o out put i s t he same as cc - o out put sour ce.
Sampl e Sess i on:
$cc - o hel l o. c $hel l o Hel l o f r om mai n! Cal l i ng f unct i on1!
Hel l o f r om f unct i on1!
Back f r om f unct i on1!
Cal l i ng f unct i on2!
Hel l o f r om f unct i on2!
Back f r om f unct i on2!
That ' s al l ! $ 2. 5 "C": Pr oduci ng an Assembl y Li st i ng
Thi s opt i on caus es cc t o compi l e C pr ogr ams and l eave t hecor r espondi ng assembl y l anguage sour ce progr ams i n a f i l e wi t hf i l ename ext ensi ons of . s.
Command For mat : cc - S hel l o. c
- S = Compi l e onl y
Sampl e Sess i on:
$cc - S hel l o. c $l s - C exampl e. f hel l o hex. c oct al . c hel l o. c hel l o. s mul t i pl y. c $
8/12/2019 UNIX for Intermediate Users
16/105
2. 6 "C": Mai n and Two Funct i ons i n Thr ee Separ at e Source Fi l es
Thi s i s t he same C pr ogr am t hat we have seen bef or e, except i t i snow i n t hr ee f i l es r at her t han one as bef ore. The t hree f i l esare mai n. c, f unct 1. c, and f unct 2. c.
$cat mai n. c
mai n ( ) { pr i ntf ( " Hel l o f r om mai n!\ n\ n") ; pr i ntf ( " Cal l i ng f unct i on1! \ n\ n") ; f unct 1( ) ; pr i nt f ( "\ t Back f r om f unct i on1! \ n\ n") ; pr i ntf ( " Cal l i ng f unct i on2! \ n\ n") ; f unct 2( ) ; pr i nt f ( "\ t Back f r om f unct2!\ n\ n") ; pr i nt f ( "That ' s al l ! \ n\ n") ; } $cat f unct 1. c f unct 1( ) {
pr i nt f ( "\ t \ t Hel l o f r om f unct i on1! \ n\ n) ; } $cat f unct 2. c f unct 2( ) { pr i nt f ( "\ t \ t Hel l o f r om f unct i on2! \ n\ n) ; } 2. 7 "C": Compi l i ng but Not Pr oduci ng an Execut abl e Modul e
Usi ng t he pr evi ous pr ogr am, t he f ol l owi ng command wi l l compi l ebut not pr oduce an execut abl e modul e.
Command For mat : cc - c mai n. c f unct 1. c f unct 2. c
- c = Compi l e, but do not l oad obj ect f i l es. Thi s opt i on causes cc t o compi l e and/ or assembl e sour ce code pr ogr ams and l eave the cor r espondi ng obj ect pr ogr ams i n f i l es wi t h f i l ename ext ensi ons of . o.
Sampl e Sess i on:
$cc - c mai n. c f unct 1. c f unct 2. c mai n. c:
f unct 1. c: f unct 2. c: $l s a. out a. out not f ound $l s - C * . o f unct 1. o f unct 2. o mai n. o $
The - c opt i ons caus es t he compi l at i on sys t em t o suppr ess t he l i nkedi t phase. Thi s pr oduces an obj ect f i l e or f i l es, i n t hi sexampl e ( mai n. o f unct 1. o f unct2. o) , t hat can be l i nk edi ted at al ater t i me wi t h t he cc command wi t h no opt i ons.3. COMPI LI NG FORTRAN PROGRAMS
8/12/2019 UNIX for Intermediate Users
17/105
3. 1 FORTRAN: Sampl e Pr ogr am a Mai n and Two Subr out i nes
There ar e sever al convent i ons f or use wi t h t he FORTRAN compi l er .They ar e:
1. The name of t he f i l e cont ai ni ng t he FORTRAN sour ce codemust end wi t h . f .
2. The compi l er i s i nvoked wi t h f 77.
3. Several opt i ons ar e avai l abl e wi t h t he compi l er .( - c , - o, - p, - S)
4. Preconnect i ons are made f or stdi n ( uni t 5) and st dout( uni t 6) .
Thi s i s t he FORTRAN sour ce code exampl e t o be used i n t hef ol l owi ng di scussi ons of t he FORTRAN compi l er .
Sampl e Sess i on:
$cat hel l o. f pr ogr am cal l i ng wr i t e( 6, 100) 100 f ormat ( ' Hel l o f r om mai n! ' , / ) wr i t e( 6, 110) 110 f or mat( ' Cal l i ng subr out i ne1! ' , / ) cal l sub1 wr i t e( 6, 120) 120 f ormat( t 15' Back f r om subr out i ne1! ' , / ) wr i t e( 6, 130) 130 f or mat( ' Cal l i ng subr out i ne2! ' , / ) cal l sub2 wr i t e( 6, 140) 140 f ormat( t 15' Back f r om subr out i ne2! ' , / )
wr i t e( 6, 150) 150 f ormat ( ' That ' s al l , f ol ks!' ) end subrout i ne sub1 wr i t e( 6, 200) 200 f ormat( t 20, ' Hel l o f r om subr out i ne1! ' , / ) end subrout i ne sub2 wr i t e( 6, 210) 210 f ormat( t 20, ' Hel l o f r om subr out i ne2! ' , / ) end 3. 2 FORTRAN: Compi l i ng a Pr ogr am
The FORTRAN compi l er i s i nvoked wi t h t he f ol l owi ng command:
Command For mat : f 77
To compi l e t he above pr ogr am i nt o an execut abl e pr ogr am, use t hef ol l owi ng command at t he command l i ne.
Sampl e Sess i on:
$f 77 hel l o. f
8/12/2019 UNIX for Intermediate Users
18/105
$
Wi t hout any opt i ons, f 77 accept s FORTRAN sour ce code and assembl yl anguage programs t hat f ol l ow the convent i ons out l i ned above. I twi l l compi l e, assembl e, and l oad t hese progr ams t o produce anexecut abl e cal l ed a. out . The f 77 ut i l i t y out put s t he obj ect codei nt o f i l es wi th t he same base f i l ename ( everyt hi ng bef or e t heperi od) as t he sour ce but wi t h a f i l ename ext ensi on of . o.
The a. out st ands f or assembl y out put . Thi s i s t he def aul t .
Sampl e Sess i on:
$f 77 hel l o. f $a. out Hel l o f r om mai n!
Cal l i ng f unct i on1!
Hel l o f r om f unct i on1!
Back f r om f unct i on1!
Cal l i ng f unct i on2! Hel l o f r om f unct i on2!
Back f r om f unct i on2!
That ' s al l ! $
NOTE: The a. out f i l e t hat was c r eat ed by t he f 77 ut i l i t y hast he f ol l owi ng per mi ss i ons:
user - r ead, wr i t e, and execut egr oup - r ead and execut eother - r ead and execut e
I t i s not necessary f or you t o change t he permi ssi ons usi ng t hechmod command because t he f 77 ut i l i t y set t he execut e per mi ssi onsf or you.
3. 3 FORTRAN: Renami ng t he Execut abl e Modul e
You can r ename t he execut abl e modul e usi ng t he mv command. The
f i l e per mi ssi ons wi l l be t he same as bef or e t he f i l e i s r enamed.
Sampl e Sess i on:
$mv a. out hel l o $hel l o Hel l o f r om mai n!
Cal l i ng f unct i on1!
Hel l o f r om f unct i on1!
Back f r om f unct i on1!
8/12/2019 UNIX for Intermediate Users
19/105
Cal l i ng f unct i on2!
Hel l o f r om f unct i on2!
Back f r om f unct i on2!
That ' s al l ! $
3. 4 FORTRAN: Gi vi ng a Name t o t he Out put Fi l e
I t i s possi bl e t o have t he out put sent t o a f i l e you speci f yi nst ead of t he def aul t , a. out , by usi ng t he f ol l owi ng command.
Command For mat : f 77 - o out put sour ce
out put - t he name of t he execut abl e f i l e
sour ce - t he name of t he For t r an sour ce code f i l e
The - o opt i on t el l s t he f 77 ut i l i t y t o t el l t he l i nk edi t or t ouse t he speci f i ed name f or t he out put i nst ead of t he def aul ta. out .
NOTE: I t i s not neces sar y f or t he - o opt i on t o appear af t ert he f 77 command. The f i l ename t hat appear s af t er t he -o i s t he name of t he out put f i l e. For exampl e, f 77sour ce - o out put i s t he same as f 77 - o out put sour ce.
Sampl e Sess i on:
$f 77 - o hel l o. f $hel l o
Hel l o f r om mai n! Cal l i ng f unct i on1!
Hel l o f r om f unct i on1!
Back f r om f unct i on1!
Cal l i ng f unct i on2!
Hel l o f r om f unct i on2!
Back f r om f unct i on2!
That ' s al l !
$ 3. 5 FORTRAN: Pr oduci ng an Ass embl y Li st i ng
Thi s opt i on caus es f 77 t o compi l e For t r an pr ogr ams and l eave t hecor r espondi ng assembl y l anguage sour ce progr ams i n a f i l e wi t hf i l ename ext ensi ons of . s.
Command For mat : f 77 - S hel l o. f
- S = Compi l e onl y
8/12/2019 UNIX for Intermediate Users
20/105
Sampl e Sess i on:
$f 77 - S hel l o. f $l s - C exampl e. f hel l o hex. c oct al . c hel l o. c hel l o. s mul t i pl y. c $
The f i l e hel l o. s cont ai ns t he assembl y l i st i ng.3. 6 FORTRAN: Mai n and Two Subr out i nes i n Thr ee Separ at e Sourc eFi l es
Sampl e Sess i on:
$cat mai n. f progr am cal l i ng wr i t e( 6, 100) 100 f or mat ( ' Hel l o f r om mai n!' , / ) wr i t e( 6, 110)
110 f or mat ( ' Cal l i ng s ubr out i ne1! ' , / ) cal l sub1 wr i t e( 6, 120) 120 f ormat( t 15' Back f r om subr out i ne1! ' , / ) wr i t e( 6, 130) 130 f or mat ( ' Cal l i ng s ubr out i ne2! ' , / ) cal l sub2 wr i t e( 6, 140) 140 f ormat( t 15' Back f r om subr out i ne2! ' , / ) wr i t e( 6, 150) 150 f ormat ( ' That ' s al l , f ol ks!' ) end $cat sub1. f subr out i ne sub1
wr i t e( 6, 200) 200 f or mat ( t 20, ' Hel l o f r om subr out i ne1! ' , / ) end $cat sub2. f subr out i ne sub2 wr i t e( 6, 210) 210 f or mat ( t 20, ' Hel l o f r om subr out i ne2! ' , / ) end 3. 7 FORTRAN: Compi l i ng But Not Pr oduci ng an Execut abl e Modul e
Usi ng t he above pr ogr am, t he f ol l owi ng command wi l l compi l e butnot pr oduce an execut abl e modul e.
Command For mat : f 77 - c mai n. f sub1. f sub2. f
- c = Compi l e, but do not l oad obj ect f i l es. Thi s opt i on causes f 77 t o compi l e and/ or assembl e sour ce code pr ogr ams and l eave t he cor r espondi ng obj ect progr ams i n f i l es wi t h f i l ename ext ensi ons of . o.
Sampl e Sess i on:
$f 77 - c mai n. f sub1. f sub2. f
8/12/2019 UNIX for Intermediate Users
21/105
mai n. f : MAI N: cal l i ng: sub1. f : sub1: sub2. f : sub2: $l s a. out *. o a. out not f ound f unct 1. o f unct 2. o
hel l o. o mai n. o sub1. o sub2. o $
The - c opt i ons caus es t he compi l at i on sys t em t o suppr ess t he l i nkedi t phase. Thi s pr oduces an obj ect f i l e or f i l es, i n t hi sexampl e ( mai n. o sub1. o sub2. o) , t hat can be l i nk edi t ed at al ater t i me wi t h t he f 77 command wi t h no opt i ons.3. 8 FORTRAN: Compi l i ng Obj ect Fi l es t o Pr oduce an Execut abl e
Modul e
The command t o pr oduce an execut abl e nodul e f r om s ever al obj ec tf i l es i s done i n t he f ol l owi ng manner :
Command For mat : f 77 obj _1 obj _2 obj _3
obj _1 t hr ough obj _n - t he obj ect f i l es
Sampl e Sess i on:
$f 77 mai n. o sub1. o sub2. o
$l s - C f unct 1. o f unct 2. o hel l o. o mai n. o sub1. o sub2. o a. out $ 4. COMPI LI NG COBOL PROGRAMS
4. 1 COBOL: Sampl e Pr ogr am wi t h a Mai n and Two Subr out i nes
Sampl e Sess i on:
$cat t eacher . cob i dent i f i cat i on di vi si on.
pr ogr am- i d. t eacher . envi r onment di vi si on. conf i gurat i on sect i on. dat a di vi si on. wor ki ng- st or age sect i on. pr ocedur e di vi si on. begi n sect i on. begi n- i t . di spl ay " Hel l o f r om mai n! " . di spl ay " Cal l i ng subr out i ne1!" . per f or m subrout i ne1. di spl ay " Back f r om subr out i ne1! " . di spl ay " Cal l i ng subr out i ne2!" . per f or m subrout i ne2.
8/12/2019 UNIX for Intermediate Users
22/105
8/12/2019 UNIX for Intermediate Users
23/105
sour ce code progr ams?
2. "C": What i s t he f i l ename ext ensi on that i ndi cat es asour ce code pr ogr am? An assembl y l anguage pr ogr am? Anobj ect code f i l e?
3. "C": What i s t he def aul t f i l ename assi gned t o t heexecut abl e f i l e?
4. "C": What command can be used to r ename t he execut abl ef i l e produced by t he cc compi l er? What are t he f i l eprot ecti ons associ ated wi t h t he execut abl e?
5. "C": What opt i on wi l l produce an assembl y l i st i ng? Whati s t he f i l ename ext ensi on of t hi s f i l e?
Cont i nue on t he next page6. "C": What command wi l l compi l e t he s our ce code pr ogr am
but wi l l not l oad obj ect f i l es but wi l l keep t he obj ectf i l es i n f i l es wi t h ext ensi ons of . o?
7. FORTRAN: What i s t he command t o i nvoke t he c ompi l er ?
8. FORTRAN: What i s t he f i l ename extensi on f or sour ce codepr ogr ams?
9. FORTRAN: What i s t he name of t he def aul t
8/12/2019 UNIX for Intermediate Users
24/105
execut abl e f i l e?
10. FORTRAN: How can you change t he permi ssi ons on t heexecut abl e modul e so anyone can execut e i t ?
11. FORTRAN: What opt i on on t he cal l t o t he compi l er wi l lal l ow you t o speci f y t he name of t he execut abl e f i l e?
Cont i nue on t he next page12. FORTRAN: What opt i on on t he cal l t o t he compi l er wi l lpr oduce an assembl y l i st i ng? What i s t he f i l enameext ensi on of t hi s f i l e?
13. FORTRAN: What opt i on wi l l pr oduce obj ect modul es butnot pr oduce an execut abl e modul e?
14. FORTRAN: What command wi l l pr oduce an execut abl e modul ef r om several obj ect modul es?
15. COBOL: What i s t he command t o cal l t he compi l er ?
16. COBOL: What are t he t hr ee f i l es creat ed by t hecompi l er ? What ar e t he f i l ename ext ensi ons?
8/12/2019 UNIX for Intermediate Users
25/105
8/12/2019 UNIX for Intermediate Users
26/105
modi f i ed any of t he pr erequi si te f i l es more r ecentl y t han t het arget f i l e, make wi l l updat e t he t arget f i l e based onconst r uct i on commands t hat f ol l ow t he dependency l i nes.
Command For mat : make [ opt i ons] [ t ar get _f i l es]
See t he onl i ne man pages f or a det ai l ed l i st of opt i ons
The t ar get _ f i l es r ef er t o t ar get s on dependency l i nes i n t he f i l ecal l ed makef i l e. I f you do not speci f y a t ar get _f i l e, make wi l lupdat e t he f i r st dependency l i ne i t f i nds i n makef i l e.
The makef i l e has t he f ol l owi ng cons t r uct i on:
t ar get : pr ere qui s i t e_ l i s tta b c ons tr uc t i on_ c ommands
The dependency l i ne i s composed of t ar get and t hepr erequi si t e_l i st , separat ed by a col on. The
const r uct i on_commands must st art wi t h a t ab char act er and mustf ol l ow t he dependency l i ne.
The t ar get i s t he name of t he f i l e t hat i s dependent on t he f i l esi n t he pr erequi si t e_l i st . The const r ucti on_commands are shel lcommands t hat const r uct t he t arget, t hese are usual l y compi l ecommands .
The make ut i l i t y wi l l execut e t he cons t r uct i on_commands when t hemodi f i cat i on t i me of one or mor e of t he f i l es i n t hepr erequi si t e_l i st i s mor e r ecent t han t he t arget .
Sampl e makef i l e:
payr ol l : sal es. c sal ary. ccc sal es. c sal ary. c - o payr ol l
I n t he exampl e, t he t arget i s cal l ed payr ol l . I t i s dependent onsal es. c and sal ary. c. I f t he modi f i cat i on t i me of ei t her oft hese i s more r ecent t han payr ol l , t he const r uct i on_commands wi l lbe execut ed. I n t hi s case, t he sour ce code programs ar e compi l edand st ored i n payr ol l .
I n t he pr evi ous exampl e, t o get t he updat e t o occur si mpl y t ypemake.
Exampl e:
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. $make .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Si nce no t arget was speci f i ed, t he f i r st dependency l i ne i s t heone t hat make wi l l att empt t o execut e.
Each of t he prer equi si t es on one dependency l i ne can be a t argeton ot her dependency l i nes. Thi s nest i ng of speci f i cati ons cancont i nue, cr eat i ng a compl ex hi erar chy t hat can speci f y a l ar gesyst em of programs.
Sampl e makef i l e:
8/12/2019 UNIX for Intermediate Users
27/105
f orm: si ze. o l engt h. occ si ze. o l engt h. o - o f orm
si ze. o: si ze. c f or m. hc c - c s i ze. c
l engt h. o: l engt h. c f or m. hcc - c l engt h. c
f or m. h: num. h t abl e. hcat num. h t abl e. h > f or m. h
Not i ce t hat f orm i s dependent on t wo obj ect f i l es, si ze. o andl engt h. o. These t wo obj ect f i l es ar e, i n t ur n, dependent upont hei r r espect i ve sour ce code programs and t he header f i l e,f or m. h. The header f i l e i s dependent upon t wo ot her headerf i l es. Note t hat t he const r ucti on_commands f or f or m. h can useany shel l command, i n t hi s case cat cr eat es t he header f i l e.
Thi s makef i l e can be qui t e di f f i cul t t o wr i t e, especi al l y i ft here ar e a number of i nter dependenci es. The make uti l i t y canr el y upon i mpl i ed dependenci es and const r uct i on_c ommands t o makeyour j ob of wr i t i ng t he makef i l e easi er. I f you do not i ncl ude adependency l i ne f or a f i l e, make assumes t hat obj ect programf i l es are dependent on compi l er or assembl er sour ce code f i l es.I f a pr er equi s i t e f or a t arg et f i l e i s . o and
. o i s not a t arget wi t h i t s own pr erequi si t es, makewi l l sear ch f or one of t he f ol l owi ng f i l es i n t he cur r ent worki ngdi rectory.
Fi l ename Type of f i l e
. c C sour ce code. f FORTRAN sour ce code. s As sembl er s our c e code
I f you do not i ncl ude a const r ucti on_command f or one of t he f i l esl i st ed, make wi l l cr eat e a def aul t const r uct i on_command l i ne t hatwi l l cal l t he appropri at e compi l er or assembl er t o cr eat e t heobj ect f i l e.
grep: A PATTERN MATCHI NG FI LTER
The gr ep ut i l i t y can sear ch t hr ough a f i l e t o see i f i t cont ai nsa speci f i ed st r i ng of character s. The ut i l i t y wi l l not changet he f i l e i t sear ches but di spl ays each l i ne t hat cont ai ns t hest r i ng. The f ormat f or t he st r i ng i s as f ol l ows.
Command For mat : gr ep [ opt i ons] l i mi t ed_r egul ar - expr essi on [ f i l e]
Use t he man command f or a compl et e l i st of opt i ons
The gr ep ut i l i t y sear ches f i l es f or a pat t er n and di s pl ays al ll i nes t hat cont ai n t he patt ern. I t uses l i mi t ed- r egul ar-expr essi ons ( t hese are expr essi ons t hat have st r i ng val ues t hatuse a subset of al l t he possi bl e al phanumeri c and speci alcharact er s) l i ke t hose used wi t h ed t o mat ch t he patt erns.
Be c ar ef ul us i ng th e c har ac te rs $, * , [ , , | , ( , ) , and \ i n t her egul ar expressi on because t hey wi l l be eval uat ed by t he Shel l .I t i s good pr act i ce t o encl ose t he r egul ar expr essi on i n si ngl equotes. Thi s wi l l pr event t he Shel l f r om eval uati ng t hesespeci al character s.
8/12/2019 UNIX for Intermediate Users
28/105
The gr ep ut i l i t y wi l l assume st andar d i nput i f no f i l es ar egi ven. Nor mal l y, each l i ne f ound i n t he f i l e wi l l be di spl ayedto standard output.
Sampl e ses si on:
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. $gr ep ' di sc' memo .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Thi s command wi l l sear ch t he f i l e " memo" f or t he st r i ng " di s c" .I t wi l l i ncl ude words l i ke di scover and i ndi scr eet because t heycont ai n t he charact ers "di sc". The si ngl e quot e mar ks ar e notnecessar y, and f or t hi s exampl e, t hey woul dn' t have made anydi f f erence. They do al l ow you t o i ncl ude spaces i n t he sear chpattern.5. 0. 1 More on Regul ar Expr essi ons
The gr ep command can be best under st ood by a di s cus si on ofr egul ar expressi ons. Let ' s cr eat e a dat abase of phone number scal l ed phone. l i s and t hen use r egul ar expressi ons t o sear cht hr ough t he dat abase. Here i s as l i st i ng of t he cont ent s of phone. l i s
Sampl e ses si on:
$cat phone. l i s Smi t h, J oan 7- 7989 Adams, Fr an 2- 3876 St Cl ai r , Fr ed 4- 6122 J ones, Ted 1- 3745 St ai r , Ri ch 5- 5972 Benson, Sam 4- 5587 $
The f or mat f or t he r ecor ds i n t hi s dat abase i s :
Last name, Fi r st name #- ####
Usi ng t he dat abase ( phone. l i s) above. What grep command woul d weuse t o sear ch t hr ough t he database and get al l t he r ecords t hathad a per son whose name cont ai ns an " S".
An al phabeti c character r epr esent s i ts el f .
Sampl e ses si on:
$gr ep S phone. l i s
Smi t h, J oan 7- 7989 St Cl ai r , Fr ed 4- 6122 St ai r , Ri ch 5- 5972 Benson, Sam 4- 5587 $
Thi s gr ep command sear ched f or t he st r i ng " S" and t hen l i st ed al lt he l i nes i n phone. l i s t hat mat ched.A si ngl e . ( dot ) i s used t o r epr esent any si ngl e char act er .
Sampl e ses si on:
8/12/2019 UNIX for Intermediate Users
29/105
$gr ep . S phone. l i s Bens on, Sam 4- 5587 $
A $ r epr esent s t he end of t he l i ne.
Sampl e ses si on:
$gr ep 5$ phone. l i s J ones, Ted 1- 3745 $
A ^ r epr esent s t he begi nni ng of t he l i ne
Sampl e ses si on:
$gr ep S phone. l i s Smi t h, J oan 7- 7989 St Cl ai r , Fr ed 4- 6122 St ai r , Ri ch 5- 5972
$
Regul ar expr essi ons must get t o gr ep i n order f or t hem t o beeval uated properl y. Let ' s say we want t o get t he r ecords ofempl oyees t hat have a phone number t hat begi ns wi t h a " 4".
What does t he f ol l owi ng expr ess i on do?
Sampl e ses si on:
$gr ep 4 phone. l i s St Cl ai r , Fr ed 4- 6122
J ones, Ted 1- 3745 Benson, Sam 4- 5587 $
Why di d we get t he r ecor d of Ted J ones? The t ab char act er waseval uat ed by t he Shel l and so t he sear ch was actual l y madel ooki ng f or a " 4". Thi s i s t he same as i f we had ent ered $gr ep 4phone. l i s.We must prevent t he Shel l f r om eval uat i ng t hese char acter s, t hi si s done wi t h t he \ ( backsl ash) char act er as shown i n t he nextexampl e.
Sampl e ses si on:
$gr ep \4 phone. l i s St Cl ai r , Fr ed 4- 6122 Benson, Sam 4- 5587 $
Now i t worked proper l y. I t sear ched f or a charact erf ol l owed by t he number 4. The [ ] ( l ef t and r i ght br acket s) ar eused t o i dent i f y a r ange of character s.
Sampl e ses si on:
8/12/2019 UNIX for Intermediate Users
30/105
$grep \ [ AF] phone. l i s Adams, Fr an 2- 3876 St Cl ai r , Fr ed 4- 6122 $
Why do [ ] need t o be quot ed? I n t he previ ous exampl e t he sear chmakes a mat ch on " A" or "F" .
A - ( dash) can i ndi cate i ncl usi on. For exampl e, we want t o makea mat ch on a phone number t hat has a 1, 2, 3, or 4. How can t hi sbe done? Her e' s an exampl e:
Sampl e Sess i on:
$grep \ [ 1- 4] phone. l i s Adams, Fr an 2- 3876 St Cl ai r , Fr ed 4- 6122 J ones, Ted 1- 3745 St ai r , Ri ch 5- 5972 Benson, Sam 4- 5587 $
A ^ character l ooks f or al l character s NOT i nsi de t he [ ]br acket s.
For exampl e,
[ 0- 9] mat c hes al l non- di gi t s
[ a- zA- Z] matches al l non- al phabeti c character s
NOTE: \ , * , and $ l os e thei r met ac har ac te r meani ngsi nsi de t he [ ] . Al so t he ^ char act er i s speci alonl y i f i t appear s f i r st .
What i s t he f ol l owi ng command sear chi ng f or ?
Sampl e Sess i on:
$grep ' [ 789] $' phone. l i s Adams, Fr an 2- 3876 St Cl ai r , Fr ed 4- 6122 J ones, Ted 1- 3745 St ai r , Ri ch 5- 5972 $
5. 0. 2 St i l l More Regul ar Expr essi ons
The * ( ast er i sk ) r epr esent s zer o or mor e of t he char act er spr ecedi ng t he ast eri sk.
A* r epr esent s 0 or mor e As.
AA* r epr esent s 1 or mor e As.
[ 0- 9] * $ 0 or mor e di gi t s at t he end of a l i ne( l ast f our di gi t s i n a phone number )
. * r epr esent s 0 or mor e of any char act er .
8/12/2019 UNIX for Intermediate Users
31/105
How woul d you wr i t e a grep command usi ng r egul ar expr essi ons t of i nd t he l ast name st art i ng wi t h an "S" and t he f i r st name wi t han "F"?
S Begi ns wi t h an " S"
. * , F Any number of c har ac t er s bef or e , F
Sampl e ses si on:
$gr ep S. \ *, F phone. l i s St Cl ai r , Fr ed 4- 6122 $
NOTE: The * ( a s t er i s k) was quot ed s o t he Shel l di dn' t t r y t oeval uate i t.
I t i s ver y desi r abl e t o quot e t he ent i r e st r i ng t o keep t he Shel lf r om doi ng an expansi on or subst i t ut i on. I t al so i ncr eases
r eadabi l i t y of t he r egul ar expressi on as i n t he f ol l owi ngexampl e.
Sampl e ses si on:
$grep ' S. *, F' phone. l i s St Cl ai r , Fr ed 4- 6122 $ 5. 0. 3 Some Ni ce grep Opt i ons
The gr ep pr ovi des sever al opt i ons t hat modi f y how t he sear ch i s
per f or med.
- c Report count of mat chi ng l i nes onl y
- v Pr i nt th os e l i nes th at don' t matc h t he pat t er n.
What wi l l t hese l i nes pr i nt ?
Sampl e ses si on:
$gr ep - c ' [ J - Z] ' phone. l i s 5 $
Why di d we get t hi s r esul t ? Let ' s anal yze t he command. I nEngl i sh, t hi s command coul d be i nt er pr et ed t o mean "Tel l me howmany r ecords i n t he f i l e "phone. l i s" cont ai n a l ett er f r om t heset J t hr ough and i ncl udi ng Z. " Look at t he phone. l i s f i l e ands ee th at f i ve r ec or ds f i t th i s r es tr i c t i on. So t he ans wer i s 5.
Now l ook at anot her exampl e and see what t hi s one does.
Sampl e ses si on:
$gr ep - v ' [ J - Z] ' phone. l i s
8/12/2019 UNIX for Intermediate Users
32/105
Adams , Fr an 2- 3876 $
Why i s t hi s t he onl y r ecord t hat was f ound? The - v opt i on sayst o s el ect r ecords t hat don' t mat ch t he patt ern. Thi s i s t he samepatt ern as t he pr evi ous exampl e and t heref ore i t sel ect s r ecordst hat don' t mat ch t he patt ern. The "Adams" r ecord i s t he onl y onet hat doesn' t make a match. I t doesn' t have a character f r om t heset J t hr ough and Z.
5. 0. 4 Summar y of Regul ar Expr essi on Char act er s
^ Begi nni ng of t he l i ne
$ End of t he l i ne
* 0 or mor e pr ecedi ng char act er s
. Any si ngl e char act er
[ . . . ] A r ange of char act er s
[ . . . ] Exc l us i on r ange of c har a ct e r s
sed: EDI T A FI LE TO STANDARD OUTPUT
UNI X pr ovi des a method of edi t i ng s t r eams of dat a. I t i s t he sedut i l i t y. The name of t hi s ut i l i t y i s der i ved f r om St r eam EDi t or.
Thi s i s not t he same as t he vi edi t or . The vi edi t or edi t s t exti n a f i l e. The s ed ut i l i t y edi t s t ext i n a s t re am. I n order toedi t a char acter st r eam t wo t hi ngs are r equi r ed. Fi r st , t he l i net o edi t must be i denti f i ed ( r egul ar expressi ons) and second, howt o edi t t he l i ne.
Command Format : sed [ - n] [ - e scri pt ] [ - f sf i l e] [ f i l es]
Det ai l s i n on- l i ne man pages
The sed ut i l i t y copi es t he named f i l es ( st andar d i nput def aul t )t o t he st andar d out put , edi t ed accordi ng t o a set ( scri pt ) of commands. The - f opt i ons cause t he scr i pt t o be t aken f r om f i l e"sf i l e".
The gener al f or m i s :
$sed / addr ess/ i nst r uct i on
NOTE: I f no addr e s s i s s pec i f i ed, al l l i nes ar e c hos en t oedi t .
' sed' addresses can be l i ne number s or r egul ar expressi ons.
Exampl e:
l i ne number s 2, 42, $ ( $ r e pr e s ent s t he l as t l i ne)
t ext ual addr ess / r egul ar - expr es si on/
8/12/2019 UNIX for Intermediate Users
33/105
NOTE: For war d s l as hes enc l os e t extual addr es s es
The sed i nst r uct i ons i ndi c at e what edi t i ng f unct i on t o per f or m.Her e some usef ul sed i nst r uct i ons:
s subst i t ut e
d del et e
NOTE: Most sed command l i nes cont ai n spaces or metacharacter sand t hey shoul d be quoted t o pr otect t hem f r om t heShel l . Ther e ar e many mor e edi t i ng commands pr ovi dedby sed. The f ol l owi ng i s a sampl e sed command t o edi tt he r ecords i n t he database f i l e t hat we ar e al r eadyf ami l i ar wi t h; namel y, phone. l i s.
Sampl e ses si on:
$sed / s/ Smi t h/ Smyt he/ phone. l i s Smyt h e, J oan 7- 7989 Adams , Fr an 2- 3876
St Cl ai r , Fr ed 4- 6122 J ones, Ted 1- 3745 St ai r , Ri ch 5- 5972 Bens on, Sam 4- 5587 $
sed i s an edi t or. I t si mpl y copi es t he st andar d i nput t o t hest andard out put , edi t i ng t he l i nes t hat mat ch t he i ndi cat edaddr ess. The ori gi nal f i l e i s not changed.
Her e' s anot her exampl e of a sed command.
Sampl e ses si on:
$sed ' 2, 4 s/ 2$/ 3/ ' phone. l i s Smi t h, J oan 7- 7989 Adams , Fr an 2- 3876 St Cl ai r , Fr ed 4- 6123 J ones, Ted 1- 3745 St ai r , Ri ch 5- 5972 Bens on, Sam 4- 5587 $
What does t hi s sed command do? I f you r ead command i n Engl i sh i treads l i ke t hi s: On l i nes 2 t hr ough 4 subst i t ut e t he 2 at t he end
of t he l i ne wi t h a 3. Noti ce t hat t he phone number f orSt Cl ai r , Fred changed f r om 4- 6122 t o 4- 6123. The number f orSt ai r , Ri ch di dn' t change because i t was out si de t he r ange.
The sed ut i l i t y can al s o be use t o del et e part s of a l i ne ofdat a. Thi s i s done by subst i t ut i ng not hi ng f or t he par t s youwant t o del et e. I t l ooks l i ke t hi s:
Sampl e ses si on:
$sed ' s/ . *, / / ' phone. l i s J oan 7- 7989 Fr an 2- 3876
8/12/2019 UNIX for Intermediate Users
34/105
Fr ed 4- 6122 Ted 1- 3745 Ri ch 5- 5972 Sam 4- 5587 $
Readi ng t hi s command i t means:
Subst i t ut e f r om t he begi nni ng of t he l i ne f ol l owed by any number
of char acter s f ol l owed by a comma wi t h t he nul l st r i ng ( not hi ng) .Thi s has t he ef f ec t of r emovi ng t he t ext .
Her e' s a del et e command and how i t ' s used.
Sampl e ses si on:
$sed d phone. l i s $
Why i s t here no out put ? Wel l , i t r ead st andar d i nput and di d t heedi t i ng f unct i on on al l t he sel ect ed l i nes. Si nce no l i nes wer e
speci f i ed al l l i nes wer e sel ect ed t o be edi t ed. The edi t i ng wast o del ete t he l i ne.
Quest i on: Has t he or i gi nal f i l e been dest r oyed?
Mul t i pl e commands are al l owed i n sed. Each i nst r uct i on i sappl i ed t o each i nput l i ne.
Sampl e ses si on:
$sed ' / Stai r / d >/ Adams/ d' phone. l i s Smi t h, J oan 7- 7989
St Cl ai r , Fr ed 4- 6122 J ones, Ted 2- 1136 Bens on, Sam 4- 5587 $
The r ecor ds f or Adams and St ai r have bot h been r emoved f r om t hedat abase.
NOTE: The > char acter i s the Bour neShel l secondar y pr ompt .
awk: A PATTERN MATCHI NG PROGRAMMI NG LANGUAGE
Suppose you want ed t o change t he f or mat of t he dat abase phone. l i st o be t he f i r st name f ol l owed by t he l ast name. Ther e i s no easyway t o do t hi s wi t h sed. For t unatel y, UNI X not onl y pr ovi des ast r eam edi t or ( sed) but i t al so has a f or mat t i ng t ool . Thef or mat t i ng t ool i n UNI X i s cal l ed awk. Thi s t ool i s named af t eraut hors who wr ot e i t Al f r ed V. Aho, Pet er J . Wei nberger , andBr i an W. Ker ni nghan so i t r eal l y doesn' t have any meani ng.
The awk ut i l i t y i s a pat t er n scanni ng and pr oces si ng l anguage.I t wi l l sear ch one or mor e f i l es f or a speci f i ed patt ern and t henperf orms an act i on, such as wr i t i ng t o st andard out put ori ncrement i ng a counter when i t f i nds a mat ch. You can use awk t ogener at e r eport s or f i l t er t ext. I t works equal l y wel l wi t h
8/12/2019 UNIX for Intermediate Users
35/105
numbers or t ext. The aut hors desi gned i t t o be easy t o use andsacr i f i ced execut i on speed t oward t hi s end.
Whi l e t he sed ut i l i t y al l ows us t o change t he t ext i n a st r eam,awk al l ows us t o easi l y r ear r ange, add, or del et e t ext i n ast r eam.
The awk t akes advant age of many cons t r uct s f r om t he C pr ogr ammi ngl anguage. I t has t he f ol l owi ng f eat ures:
f l exi bl e f or matcondi t i onal execut i onl oopi ng st at ement snumer i c var i abl esst r i ng var i abl esr egul ar expr essi onsC' s pr i nt f
The awk wi l l t ake i t s i nput f r om t he f i l es you speci f y on t hecommand l i ne or f r om st andar d i nput . The f ol l owi ng i s t he f or matf or awk:
Command f or mat : awk [ - Fc] [ prog] [ f i l es]
The awk wi l l scan each l i ne of f i l e f or l i nes t hat mat ch a set ofpatt erns speci f i ed by pr og. Wi t h each patt ern i n pr og t her e canbe an associ ated act i on t o be per f or med when t he l i ne i s f ound.
The set of pat t er ns may appear l i t er al l y as pr og, or i n a f i l especi f i ed as - f f i l e. The pr og st r i ng shoul d be encl osed i nsi ngl e quotes t o pr otect i t f rom t he Shel l .
Fi l es ar e r ead i n or der and i f t her e ar e none speci f i ed t hest andard i nput i s r ead. Each l i ne i s mat ched agai nst t he pat t ernport i on of ever y patt ern- act i on st atement . The associ ated act i on
i s per f ormed f or each mat ched pat t ern. An i nput l i ne i s made upf i el ds separat ed by whi t e space. $1, $2. . def i ne t he f i el ds. $0r ef ers t o t he whol e l i ne.
A pat t ern- act i on s t atement has t he f orm:
patt ern {act i on}
A mi ss i ng act i on means pr i nt t he l i ne; a mi ss i ng pat t er n al waysmakes a mat ch. A s t at ement can be one of t he f ol l owi ng:
i f ( condi t i onal ) st atement [ el se st at ement ]whi l e ( condi t i onal ) st at ementf or ( expr essi on; condi t i onal ; expr essi on) st at ementbreakcont i nue{[ st at ement ] . . . }var i abl e=expres si onpr i nt [ expressi on- l i st ] [ >expressi on]pr i nt f f or mat [ , expr essi on- l i st ] [ >expressi on]next # ski p r emai ni ng pat t ern on t hi s i nput l i neexi t # ski p t he rest of t he i nput
St at ement s ar e t ermi nat ed by semi col ons, new l i nes ( Ret ) , or
8/12/2019 UNIX for Intermediate Users
36/105
r i ght br aces.
Let ' s l ook at t he synt ax f or awk i n a l i t t l e si mpl er manner.
awk ' commands' [ f i l ename]
An awk pr ogr am ( commands) consi st s of a opt i onal pat t ern t o mat chand an act i on t o perf orm i f a mat ch i s f ound on t he cur r ent l i ne.
Thi s synt ax l ooks l i ke t hi s :
awk ' / pat t er n/ {act i on}' [ f i l ename]The pat t er n used i s a r egul ar expr essi on encl osed i n f or war dsl ashes. I f no pat t er n i s l i st ed, t he act i on wi l l be per f ormedf or ever y l i ne. An act i on can cont ai n sever al commands. Ther ecan be mul t i pl e pat t erns and act i ons.
awk ' / pat t er n1/ {act i on1}/ pat t er n2/ {act i on2}' [ f i l ename}
One of awk' s commands i s pri nt. I t put s t he cur r ent l i ne on
st andard out put .
Sampl e ses si on:
$awk ' {pr i nt }' phone. l i s Smi t h, J oan 7- 7989 Adams , Fr an 2- 3876 St Cl ai r , Fr ed 4- 6122 J ones, Ted 1- 3745 St ai r , Ri ch 5- 5972 Benson, Sam 4- 5587 $
The awk spl i t s ever y i nput l i ne at whi t espac e and keeps t r ack oft he number of f i el ds on each l i ne and count s t he number of l i nesr ead. Each f i el d i s i dent i f i ed by i t s f i el d number and a $.
$1 I dent i f i es t he f i r s t f i el d
$2 I dent i f i es th e s ec ond f i el d
.
$0 I dent i f i es th e ent i re l i ne
NF I dent i f i es t he number of f i el ds on t he l i ne
NR I dent i f i es t he number of l i nes t hat have been r eadSampl e ses si on:
$awk ' {pr i nt NR, $1}' phone. l i s 1 Smi t h, 2 Adams, 3 St Cl ai r , 4 J ones, 5 St ai r , 6 Benson, $
8/12/2019 UNIX for Intermediate Users
37/105
To change t he or der of t he names i n phone. l i s, use awk. Thecomma i n t he pr i nt command t el l s awk t o separ at e each f i el d wi t ha s pace. Wi t hout t he comma, t he out put woul d have no spaci ng.
Sampl e ses si on:
$awk ' {pr i nt $2, $1 " " $3}' phone. l i s J oan Smi t h, 7- 7989
Fr an Adams , 2- 3876 Fr ed St Cl ai r , 4- 6122 Ted J ones, 1- 3745 Ri ch St ai r , 5- 5972 Sam Bens on, 4- 5587 $
sor t : SORT A FI LE
The sor t ut i l i t y sor t s l i ne of al l t he named f i l es t oget her andwr i t es t he r esul t t o st andar d out put . The st andar d i nput i s usedi f - i s used as a f i l e name or no i nput f i l es ar e speci f i ed.
Compar i sons ar e based one or more sort keys ext r act ed f r om eachl i ne of i nput . Ther e i s onl y one key by def aul t , t hat ' s t heent i re l i ne, and or der i ng i s l exi cogr aphi c by bytes i n machi necol l at i ng sequence.
Command f or mat : sor t [ - cmu] [ - oout put ] [ - ykmem] [ - zr ecsz] [ - df i Mnr] [ - btx] [ +pos] [ - pos2] [ f i l es]
See on- l i ne manual f or opt i ons etc.
The easi es t way t o use sor t i s t o add i t at t he end af a
pi pel i ne. What does t he f ol l owi ng command l i ne accompl i sh:
Sampl e ses si on:
$gr ep '[ 45] ' phone. l i s | sed ' s//73/ ' | s ort Bens on, Sam 734- 5587 St Cl ai r , Fr ed 734- 6122 St ai r , Ri ch 735- 5972 $
The gr ep command wi l l sel ect onl y t hose r ecor ds t hat have a 4 ofa 5 i n t he phone number, t hose r ecords ar e t hen sent t o sed whi ch
wi l l add " 73" j ust af t er t he t ab char acter , t hen t he r ecor ds aresent t o sor t and put i n al phabet i cal order . Not i ce t hat t here i sa probl em here, shoul d StCl ai r come bef ore St ai r i n anal phabeti cal l i st i ng? The answer i s NO. Why di d t hi s happen?I t occur r ed because of t he col l ati ng sequence f or t he def aul ts ort .
Thi s can be f i xed by speci f yi ng some opt i ons on t he cal l t o t hes ort ut i l i t y. Here are s ome opt i ons f or s ort . Let ' s s ee i f wecan det er mi ne how t o r emedy t he probl em di scover ed i n t he def aul ts ort .
sor t opt i ons:
- f Fol d l ower c as e i nto upper c as e
8/12/2019 UNIX for Intermediate Users
38/105
- r Rever s e t he s or t f r o m hi ghes t t o l owes t- b I gnore l eadi ng bl ank spaces- d Di ct i onar y sort - i gnore nonal phanumer i c char act er s- m Mer ge t wo s or t ed f i l es t oget her- n Sort th e l i s t as n umber s not di gi t c hara c te rs
Noti ce t he - f opt i ons f ol ds l ower case i nt o upper case. Thi sopt i on wi l l make t he sor t f or our pr obl em wor k corr ect l y.
Sampl e ses si on:
$gr ep '[ 45] ' phone. l i s| sed ' s//73/ ' | sort - f Bens on, Sam 734- 5587 St ai r , Ri ch 735- 5972 St Cl ai r , Fr ed 734- 6122 $
The sor t can al s o be di r ect ed t o use onl y a por t i on of t he l i neas a sort i ng key ver sus t he ent i r e l i ne. The ut i l i t y wi l laut omat i cal l y break each l i ne i nto f i el ds at whi t espacedel i mi t er s. You can use a character ot her t han whi t espace byusi ng t he - t opt i on. The f i el ds are set up l i ke t hi s:
0 1 2/ - - - - | / - - - | / - - - - - - - - - - - - - |
Adams , Fr an 2- 3876I n order t o sor t by t he second f i el d, her e i s t he sor t command t oenter.
Sampl e ses si on:
$sort +1 phone. l i s Adams , Fr an 2- 3876 St Cl ai r , Fr ed 4- 6122
Smi t h, J oan 7- 7989 St ai r , Ri ch 5- 5972 Bens on, Sam 4- 5587 J ones, Ted 1- 3745 $
Here' s a sampl e of a sor t on t he 3r d f i el d.
Sampl e ses si on:
$sort +2 phone. l i s J ones, Ted 1- 3745
Adams , Fr an 2- 3876 Bens on, Sam 4- 5587 St Cl ai r , F r ed 4- 6122 St ai r , Ri ch 5- 5972 Smi t h , J oan 7- 7989 $
A sort can al so be per f ormed by a character posi t i on wi t hi n af i el d.
Sampl e ses si on:
8/12/2019 UNIX for Intermediate Users
39/105
$sort +2. 4 phone. l i s St Cl ai r , Fr ed 4- 6122 Benson, Sam 4- 5587 J ones, Ted 1- 3745 Adams, Fr an 2- 3876 St ai r , Ri ch 5- 5972 Smi t h, J oan 7- 7989 $
NOTE: The f i r s t c har a ct e r o f a f i el d i s t he del i mi t e r f ort hat f i el d.5. 1 ARCHI VER AND LI BRARY MAI NTAI NER
Thi s command wi l l mai nt ai n gr oups of f i l es combi ned i nt o a si ngl ear chi ve f i l e. The mai n use of ar i s t o creat e and updat e l i br ar yf i l es as used by t he l i nk edi t or. I t can al so be used f or anyot her si mi l ar pur pose. The f i l e header consi st s of pr i nt abl eASCI I char act er s. I f t he ar chi ve consi st s of pr i nt abl echaract er s, t hen t he ent i r e archi ve i s al so pr i nt abl e.
Command For mat : ar key [ posname] af i l e [ name] . . . Unl i ke command opt i ons, t he command key i s r equi r ed. The key, usual l y a - si gn, i s f or med wi t h one of t he f ol l owi ng l et t ers drqt pmx. Ar gument s t o t he key ar e made f r om one or more of t he f ol l owi ng set, vuai bci s. See Appendi x I f or a compl ete l i st of command keys.
posname i s an ar chi ve member name used as a r ef er ence f or posi t i oni ng other f i l es i n t he ar chi ve.
af i l e i s t he name of t he archi ve.
name[s] ar e t he const i t uent f i l es i n t he archi ve.
To i l l ust r at e how t o cr eat e and use an ar chi ve f i l e, we wi l l uset he "C" program cal l ed mai n. c and t he t wo f unct i ons, f unct 1. c andf unct 2. c. Fi r st , creat e t he obj ect f i l es t hat we i nt end t o puti nt o t he archi ve f i l e.
Sampl e Sess i on:
$cc - c mai n. c f unct 1. c f unct 2. c mai n. c: f unct 1. c: f unct 2. c:
$l s - C * . o f unct 1. o f unct 2. o mai n. o $
Remember t he - c opt i on wi l l not produce an execut abl e modul e, buti t does cr eat e t he obj ect modul es. These obj ect modul es ar e f i l ef i l es t hat we wi l l pl ace i nt o an archi ve.
5. 2 Cr eat i ng an Ar chi ve Fi l e wi t h Obj ect Modul es
I n t hi s cal l t o ar, we wi l l use t he r command key whi ch wi l lr epl ace t he named f i l es i n t he archi ve. The v opt i on wi l l gi ve a
8/12/2019 UNIX for Intermediate Users
40/105
ver bose f i l e- by- f i l e descr i pt i on of t he maki ng of t he new ar chi vef i l e.
Sampl e Sess i on:
$ar r v f unct s