b sh programming

193
7/28/2019 b Sh Programming http://slidepdf.com/reader/full/b-sh-programming 1/193 1 UNI X Bour ne Shel l Pr ogr amm i ng Devel oped by: User Li ai son Secti on, D- 7131 Denver Of f i ce [Name and number removed at author's request] Revi si on Date: Febr uary 7, 1991 I. I NTRODUCTI ON .............................................. v A. Audi ence............................................. v B. Cour se Obj ect i ves .................................... v C. Cour s e Handout Convent i ons ........................... vi 1. BOURNESHELL OVERVI EW ...................................... 1 1. 1 What i s t he Bour neShel l ? ............................ 2 1. 2 Maki ng a Bour ne Shel l Scri pt Execut abl e............. 3 1. 3 Tr aci ng Mechani sms .................................. 6 Wor kshop 1............................................... 9 2. USER, SHELL, AND READ- ONLY SHELL VARI ABLES................ 11 2. 1 User Var i abl es ...................................... 11 2. 2 Shel l Var i abl es ..................................... 14 2. 2. 1 HOME......................................... 14 2.2.2 IFS.......................................... 15 2. 2. 3 MAI L ......................................... 15 2. 2. 4 MAI LPATH ..................................... 15 2. 2. 5 MAI LCHECK .................................... 16 2. 2. 6 PATH ......................................... 16 2. 2. 7 PS1.......................................... 17 2. 2. 8 PS2.......................................... 17 2. 3 Read- Onl y User Var i abl es ............................ 18 2. 4 Read- Onl y Shel l Var i abl es ........................... 19 2. 4. 1 Name of t he Cal l i ng Program .................. 19 2. 4. 2 Ar gument s .................................... 19 2. 4. 3 Shi f t ........................................ 21 2. 4. 4 Set .......................................... 22 2. 4. 5 expr ......................................... 23 Wor kshop 2............................................... 27 3. POSI TI ONAL PARAMETERS..................................... 33 3. 1 Readi ng I nput I nto a Shel l Vari abl e................. 34 3. 2 Command Subst i t ut i on................................ 36 3. 3 Comments i n BourneShel l Scri pts ..................... 38 3. 4 Bour neShel l Envi r onment - Expor t i ng Var i abl es ....... 39 Wor kshop 3............................................... 41 4. CONTROL CONSTRUCTS: ....................................... 45 4. 1 Types of Test s Used wi t h Cont r ol Const r ucts: ........ 46 4. 2 Test on Numer i c Val ues .............................. 47 1

Upload: snehasrinivasa

Post on 03-Apr-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 1/193

1 UNI X Bour ne Shel l Pr ogr ammi ng

Devel oped by:

User Li ai son Sect i on, D- 7131Denver Of f i ce

[ Name and number r emoved at aut hor ' sr equest ]

Revi si on Date: Febr uary 7, 1991

I . I NTRODUCTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vA. Audi ence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vB. Cour se Obj ect i ves. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vC. Course Handout Convent i ons . . . . . . . . . . . . . . . . . . . . . . . . . . . vi

1. BOURNESHELL OVERVI EW. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11. 1 What i s t he Bour neShel l ?. . . . . . . . . . . . . . . . . . . . . . . . . . . . 21. 2 Maki ng a Bour ne Shel l Scri pt Execut abl e. . . . . . . . . . . . . 31. 3 Tr aci ng Mechani sms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Wor kshop 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

2. USER, SHELL, AND READ- ONLY SHELL VARI ABLES. . . . . . . . . . . . . . . . 112. 1 User Var i abl es. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112. 2 Shel l Var i abl es. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

2. 2. 1 HOME. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142. 2. 2 I FS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152. 2. 3 MAI L. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152. 2. 4 MAI LPATH. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152. 2. 5 MAI LCHECK. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162. 2. 6 PATH. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162. 2. 7 PS1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172. 2. 8 PS2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

2. 3 Read- Onl y User Var i abl es. . . . . . . . . . . . . . . . . . . . . . . . . . . . 182. 4 Read- Onl y Shel l Var i abl es. . . . . . . . . . . . . . . . . . . . . . . . . . . 19

2. 4. 1 Name of t he Cal l i ng Program. . . . . . . . . . . . . . . . . . 192. 4. 2 Ar gument s. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192. 4. 3 Shi f t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212. 4. 4 Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222. 4. 5 expr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

Wor kshop 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

3. POSI TI ONAL PARAMETERS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333. 1 Readi ng I nput I nto a Shel l Var i abl e. . . . . . . . . . . . . . . . . 343. 2 Command Subst i t ut i on. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363. 3 Comments i n BourneShel l Scr i pts . . . . . . . . . . . . . . . . . . . . . 383. 4 Bour neShel l Envi r onment - Expor t i ng Var i abl es. . . . . . . 39Wor kshop 3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

4. CONTROL CONSTRUCTS: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454. 1 Types of Test s Used wi t h Cont r ol Const r ucts: . . . . . . . . 464. 2 Test on Numer i c Val ues. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

1

Page 2: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 2/193

4. 3 Test on Char act er St r i ngs. . . . . . . . . . . . . . . . . . . . . . . . . . . 474. 4 Test on Fi l e Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494. 5 i f t hen. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504. 6 i f t hen el se. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524. 7 i f t hen el i f . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544. 8 f or . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554. 9 whi l e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574. 10 unt i l . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584. 11 case. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60Wor kshop 4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

5. COMPI LI NG PROGRAMS I N UNI X. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675. 1 "C" : Sampl e Progr am wi t h a Mai n and Two Funct i ons

i n One . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675. 2 " C" : Compi l i ng a Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . 695. 3 "C": Renami ng t he Execut abl e Modul e. . . . . . . . . . . . . . . . . 715. 4 "C": Gi vi ng a Name t o t he Out put F i l e. . . . . . . . . . . . . . . 725. 5 "C" : Produci ng an Assembl y Li s t i ng. . . . . . . . . . . . . . . . . . 735. 6 "C" : Mai n and Two Funct i ons i n Thr ee Separat e Sour ce

Fi l es. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

5. 7 "C" : Compi l i ng but Not Produci ng an Execut abl eModul e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755. 8 FORTRAN: Sampl e Progr am a Mai n and Two Subrout i ne. . . 765. 9 FORTRAN: Compi l i ng a Program. . . . . . . . . . . . . . . . . . . . . . . . 775. 10 FORTRAN: Renami ng t he Execut abl e Modul e. . . . . . . . . . . . 795. 11 FORTRAN: Gi vi ng a Name t o t he Out put Fi l e. . . . . . . . . . 805. 12 FORTRAN: Pr oduci ng an Assembl y Li st i ng. . . . . . . . . . . . . 815. 13 FORTRAN: Mai n and Two Subrout i nes i n Three Separat e

Sour ce Fi l es. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825. 14 FORTRAN: Compi l i ng But Not Pr oduci ng an Execut abl e

Modul e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835. 15 FORTRAN: Compi l i ng Obj ect Fi l es t o Produce an

Execut abl e Modul e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845. 16 COBOL: Sampl e Pr ogr am wi t h a Mai n and Two

Subr out i nes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855. 17 COBOL: Compi l i ng a Program. . . . . . . . . . . . . . . . . . . . . . . . . 865. 18 COBOL: Runni ng a Program. . . . . . . . . . . . . . . . . . . . . . . . . . . 87Wor kshop 5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

6. UNI X TOOLS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 956. 1 Pr ocesses. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 956. 2 Execut i ng a Command. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 956. 3 Pr ocess I dent i f i cat i on. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 956. 4 grep: A Pat t ern Mat c hi ng Fi l t er . . . . . . . . . . . . . . . . . . . . . 98

6. 4. 1 More on Regul ar Expressi ons. . . . . . . . . . . . . . . . . . 996. 4. 2 Cl osur e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1036. 4. 3 Some Ni ce grep Opt i ons . . . . . . . . . . . . . . . . . . . . . . 104

6. 4. 4 Summary of Regul ar Expr essi on Charact ers . . . . . 1056. 5 sed: Edi t a Fi l e to Standard Output . . . . . . . . . . . . . . . . . 1066. 6 awk: A Pat t ern Matchi ng Progr ammi ng Language. . . . . . . . 1106. 7 sor t : Sor t a Fi l e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1146. 8 What Ot her Usef ul UNI X Tool s ar e Avai l abl e. . . . . . . . . . 1176. 9 Archi ver and Li brary Mai ntai ner . . . . . . . . . . . . . . . . . . . . . 118

6. 9. 1 ar : Cr eat i ng an Ar chi ve Fi l e wi t h Obj ectModul es. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

6. 9. 2 ar : Ver i f yi ng t he Cont ent s of t he Ar chi veFi l e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

2

Page 3: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 3/193

6. 9. 3 ar : Removi ng Dupl i cat e Obj ect Fi l es. . . . . . . . . . 1206. 9. 4 ar : Compi l i ng Mai n and Ar chi ve Fi l es. . . . . . . . . 120

Wor kshop 6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121

7. VAX DCL TO UNI X SHELL SCRI PT CONVERSI ON. . . . . . . . . . . . . . . . . . . 1257. 1 Pr ocesses. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1277. 2 Pi pes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1287. 3 I nput , Out put , and Err or Redi rect i on. . . . . . . . . . . . . . . . 1297. 4 Command St r uct ur e and Fi l e Nami ng Convent i ons. . . . . . . 1317. 5 Fi l e Management Commands. . . . . . . . . . . . . . . . . . . . . . . . . . . . 1337. 6 Met achar act er s. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1357. 7 Wi l dcards: Are They Real l y Wi l d? . . . . . . . . . . . . . . . . . . . . 1367. 8 Summar y. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137Wor kshop 7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139

8. ADVANCED FEATURES OF FTP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1438. 1 I ni t i al i zi ng FTP on UMAX. . . . . . . . . . . . . . . . . . . . . . . . . . . . 1448. 2 Mul t i pl e Fi l e Tr ansf er s. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1458. 3 Aut o Logi n Feat ur e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146

8. 4 Macr os. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1488. 5 Fi l ename Tr ansl at i on. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1498. 6 Abor t i ng Tr ansf er s. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1508. 7 Mor e Remot e Comput er Commands. . . . . . . . . . . . . . . . . . . . . . . 151Wor kshop 8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153

9. OPTI ONAL CHAPTER - KORNSHELL PROGRAMMI NG. . . . . . . . . . . . . . . . . . 1559. 1 Kor nShel l Var i abl es. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1559. 2 User Def i ned Var i abl es. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1579. 3 Val ues of Var i abl es Between Chi l d and Par ent

Pr ocesses. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1589. 4 ksh: Al i ases. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1599. 5 ksh: Command Li ne Edi t i ng. . . . . . . . . . . . . . . . . . . . . . . . . . . 1619. 6 ksh: I nt er acti ve Command Li ne Edi t i ng. . . . . . . . . . . . . . . 1629. 7 ksh: Funct i ons. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1649. 8 ksh: The Sel ect Const r uct . . . . . . . . . . . . . . . . . . . . . . . . . . . 1669. 9 ksh: Traci ng and Condi t i onal Execut i on. . . . . . . . . . . . . . 168Wor kshop 9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169

APPENDI X A - sh. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173

APPENDI X B - t est . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189

APPENDI X C - expr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193

APPENDI X D - f t p. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195

APPENDI X E - cc. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209

APPENDI X F - f 77. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219

APPENDI X G - l i nt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231

APPENDI X H - cb. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235

APPENDI X I - ar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237

3

Page 4: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 4/193

APPENDI X J - t i me. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243

APPENDI X K - ksh. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245

I NDEX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279

I . I NTRODUCTI ON

A. Audi ence

 Thi s cour se i s f or i ndi vi dual s who have compl et ed "UNI X f orBegi nni ng Users" ( or equi val ent experi ence) and want t o wr i t e UNI XBour neShel l scri pt f i l es. A scri pt f i l e cont ai ns a sequence of UNI X commands whi ch can be executed by enter i ng one command. I ti s assumed t hat t he st udent al r eady has a good underst andi ng of t heUNI X operat i ng syst em, be abl e t o use a UNI X edi t or , and bef ami l i ar wi t h a comput er t er mi nal or t ypewr i t er keyboard.

B. Cour se Obj ect i ves

Upon successf ul compl et i on of t hi s cour se the student wi l l be abl et o:

1. Wr i t e moderatel y compl ex Bour neShel l scr i pt s.

2. Make a Bour neShel l scr i pt execut abl e.

3. Demonst r at e how t o use t he f ol l owi ng Bour neShel lcommands: shi f t , exi t , expr , t est , i f t hen, i f t hen el se,i f then el i f , f or , whi l e, unt i l , and case.

4. Use t he f ol l owi ng Bour neShel l const r uct s: t r aci ngmechani sms ( f or debuggi ng) , user var i abl es, Bour neShel lvar i abl es, r ead- onl y var i abl es, posi t i onal par amet er s,r eadi ng i nput t o a Bour neShel l scr i pt , commandsubst i t ut i on, comment s, and expor t i ng var i abl es. I naddi t i on, t est on numer i c val ues, t est on f i l e t ype, andt est on char act er st r i ngs are cover ed.

6. Cr eat e a ". pr of i l e" scri pt to cust omi ze t he userenvi r onment .

7. Use advanced f eat ur es of Fi l e Tr ansf er Pr ot ocol ( FTP)

8. Compi l e sour ce code i nt o obj ect and execut abl e modul es.

9. Opt i onal : Kor nShel l pr ogr ammi ng. Thi s i s of pr i mar yi nt erest t o pr ogr ammers.

10. Conver t VMS DCL command f i l es t o UNI X Shel l .C. Course Handout Convent i ons

4

Page 5: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 5/193

  There ar e sever al convent i ons used i n t hi s handout f or consi st encyand easi er i nt er pr et at i on:

1. Sampl es of act ual t er mi nal sessi ons ar e si ngl e- l i nedboxed.

2. User ent r i es ar e shown i n bol d pr i nt and ar e under 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.

( Ret ) Backspace Tab Ct r l - F6Pr i nt ( Shi f t - F7) Go t o DOS ( 1)

NOTE: ( Ret ) i ndi cat es t he Ret ur n or Ent er key l ocat edabove t he ri ght Shi f t key.

4. Exampl es of user ent r i es not showi ng t he comput er ' sr 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 hor i zont al al i gnmentmean cont i nuat i on or t hat dat a i s mi ssi ng f r omdi agr am.

Ú_______________________________________________________________¿| || Mul t i max, Nanobus, and UMAX are t r ademarks of || Encore Comput er Corporat i on. || || || Annex i s a t r ademark of XYLOGI CS, I nc. || || || UNI X and Tel etype are r egi st ered t r ademarks of |

| AT&T Bel l Labor ator i es || || || Et hernet i s a t r ademark of Xerox Corporat i on || |À_______________________________________________________________Ù

NOTES

1. BOURNESHELL OVERVI EW 

5

Page 6: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 6/193

  The BourneShel l i s bot h a command- l i ne i nt er pret er and a hi gh-l evel pr ogr ammi ng l anguage. When i t i s act i ng as a command- l i nei nt erpreter , i t pr ocesses commands as you ent er t hem at t he commandprompt . When you use i t as a progr ammi ng l anguage, i t processescommands t hat are st ored i n f i l es known as Bour neShel l scr i pt s. Thi s cour se wi l l show you how t o cr eat e and execut e BourneShel lscr i pt s. We wi l l expl ore Bour neShel l pr ogr ammi ng i ncl udi ng suchf eat ur es as var i abl es, cont r ol st r uctur es, pr ocesses, andexecut abl e f i l es.

 The BourneShel l i s one of t hree shel l s avai l abl e on most UNI Xsyst ems. Bour ne i s t he accept ed st andard f or Syst em V UNI X. Theother shel l s are bei ng used more and more. The other shel l s aret he CShel l and t he KornShel l . The CShel l i s BSD ( Berkel ey Sof t wareDi st r i but i on) UNI X. BSD was devel oped at t he Uni ver si t y of Cal i f or ni a at Ber kel ey, Cal i f or ni a. Most of t he f eat ur es f ound i nt he Bour neShel l ar e al so f ound i n t he ot her shel l s; t her e ar edi f f erences, however . The CShel l and KornShel l are not st andard

on UNI X Syst em V but are general l y avai l abl e.

Bour neShel l scr i pt s al l ow you t o group command l i nes t oget her andexecut e them by ent er i ng a si ngl e command at t he command l i ne. Thi sal l ows compl ex f unct i ons t o be compl eted by any user , andr epet i t i ve f unct i ons can be compl eted easi l y. I nput and out putcan al so be redi r ect ed f r om a Bour neShel l scr i pt .1. 1 What i s t he Bour neShel l ?

Bour neShel l i s a hi gh l evel progr ammi ng l anguage and a command l i nei nt er pr et er .

 The command t o i nvoke t he BourneShel l i s:

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command Format : sh [ - acef hi knr st uvx] [ args] ºº ºº ( See Appendi x A f or a compl et e l i st of opt i ons et c) ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼ A Shel l scr i pt i s an execut abl e pl ai n f i l e t hat cont ai ns UNI X andshel l commands. To execut e t he shel l scr i pt t ype the name of t hescri pt at t he pr ompt . A si mpl e shel l scri pt cal l ed shel l _ex i sshown i n t he f ol l owi ng exampl e. The out put f r omt he execut i on of t he shel l i s al so shown.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $cat shel l _ex || echo "Thi s i s a ver y si mpl e shel l pr ocedur e " || echo "cr eat ed wi t h t he basi c echo command " || echo "and t hree ot her very basi c commands " || echo || ps || echo |

6

Page 7: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 7/193

| who || echo || l s || $sh shel l _ex || Thi s i s a ver y si mpl e shel l pr ocedur e || cr eated wi t h t he very basi c echo command || and t hree ot her very basi c commands || || PI D TTY TI ME COMMAND || 10443 r t 02120 0: 01 sh || 10427 r t 02120 0: 04 ksh || || sgavl i ck r t 021e0 Sep 7 13: 26 || t eacher r t 021b0 Sep 7 14: 39 || || memo || cl ass_not es || $ |À_______________________________________________________________Ù1. 2 Maki ng a Bour ne Shel l Scr i pt Execut abl e

A Bour ne Shel l scr i pt i s an or di nary f i l e that cont ai ns commandswhi ch can be executed i n sequence by enter i ng one command at t heBour ne Shel l pr ompt . I n or der f or a scr i pt t o be execut ed, i t mustf i r st be execut abl e. Thi s i s done wi t h t he chmod command.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $cat shel l _ex || echo "Thi s i s a ver y si mpl e shel l pr ocedur e " || echo "cr eat ed wi t h t he basi c echo command " || echo "and t hree ot her very basi c commands " || echo || ps || echo || who || echo || l s || $ |À_______________________________________________________________Ù

I f t he l s - l shel l _ex command were ent ered, we woul d see t hepr ot ecti ons assi gned t o t hi s f i l e.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $l s - l shel l _ex || - r w- r - - r - - 1 t eacher cl ass 66 Sep 7 10: 24 shel l _ex || $ |À_______________________________________________________________Ù

 The char act er i n col umn one i s t he t ype of f i l e.

- = ordi nary ( pl ai n) di sk f i l e

7

Page 8: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 8/193

d = di r ect or y

b = bl ock speci al f i l e

c = char acter speci al f i l e

p = f i f o f i l e ( "named pi pe") speci al f i l e

l = symbol i c l i nk

Not i ce t hat t he scr i pt f i l e i n t he pr evi ous sampl e sessi on has t hef ol l owi ng f i l e pr ot ecti ons:

User - Read and Wr i t eGr oup - ReadOt her - Read

No execut e permi ssi ons have been grant ed f or user , gr oup, or ot her .I f we t r y t o execut e t hi s scri pt by t ypi ng i t s name, t he f ol l owi ngwoul d r esul t .

Sampl e Sessi on:

Ú_______________________________________________________________¿| $shel l _ex || shel l _ex: execut e per mi ssi on deni ed || $ |À_______________________________________________________________Ù

 Thi s er r or message woul d i ndi cat e t hat execut e permi ssi on wasdeni ed. The Bour neShel l scr i pt coul d not be execut ed. To changet he permi ss i ons f or t he Bour neShel l scr i pt , use t he chmod command.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $chmod 755 shel l _ex || $l s - l shel l _ex || - r wxr- xr- x 1 t eacher cl ass 66 Sep 7 10: 26 shel l _ex || $ |À_______________________________________________________________Ù

Now t hat t he per mi ss i ons have been changed t o al l ow user , group,and ot her s t o execut e the f i l e, i t wi l l execut e pr oper l y.Sampl e Sessi on:

Ú_______________________________________________________________¿| $shel l _ex || Thi s i s a ver y si mpl e shel l pr ocedur e || cr eat ed wi t h t he basi c echo command || and t hree ot her very basi c commands || |

8

Page 9: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 9/193

| PI D TTY TI ME COMMAND || 10443 r t 02120 0: 01 sh || 10427 r t 02120 0: 04 ksh || || sgavl i ck r t 021e0 Sep 7 13: 26 |. t eacher r t 021b0 Sep 7 14: 39 .. .. .

 The prot ect i ons wi l l wor k as you expect . Execut e permi ssi on f ort he user wi l l al l ow you ( t he owner ) t o r un t he Bour neShel l scr i pt .Gr oup permi ssi ons al l ow anyone i n your gr oup to execut e t he scr i pt ,and ot her permi ssi on al l ows anyone on t he syst em t o execut e t hescr i pt .1. 3 Traci ng Mechani sms

I t i s possi bl e to have a tr ace made of t he Bour neShel l scr i pt asi t execut es. Thi s i s i nval uabl e f or debuggi ng pur poses. Al l t hati s r equi r ed i s t o gi ve an opt i on t o t he Bour neShel l . Thi s i s done

by i ncl udi ng an opt i on on t he cal l t o "sh" . The command to do t hi si s:

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command Format : sh [ - acef hi knr st uvx] [ args] ºº ºº See Appendi x A f or a compl et e l i st of opt i ons et c ºº ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼  The opt i on t o t urn on t r aci ng i s - x. For an exampl e, l et ' s t r acet he execut i on of t he si mpl e scr i pt shel l _ex.

Sampl e sessi on:

Ú_______________________________________________________________¿| $cat shel l _ex || echo "Thi s i s a ver y si mpl e shel l pr ocedur e " || echo "cr eated wi t h t he basi c echo command " || echo "and t hree ot her very basi c commands " || echo || ps || echo || who || echo || l s || $ |

À_______________________________________________________________Ù

Execut e t he Bour neShel l scr i pt usi ng t he - x opt i on on t he cal l t ot he shel l . The f ol l owi ng sampl e sessi on shows how t o do thi s andi t shows t he r esul t s of t he t r ace.Sampl e sessi on:

Ú_______________________________________________________________¿| $sh - x shel l _ex || + echo Thi s i s a ver y si mpl e shel l pr ocedur e |

9

Page 10: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 10/193

| Thi s i s a ver y si mpl e shel l pr ocedur e || + echo cr eated wi t h t he basi c echo command || cr eated wi t h t he basi c echo command || + echo and t hree ot her very basi c commands || and t hree ot her very basi c commands || + echo || || + ps || PI D TTY TI ME COMMAND || 10443 r t 01120 0: 01 sh || 10427 r t 02120 0: 04 ksh || + echo || || + who || sgavl i ck r t 021e0 Sep 7 13: 26 || t eacher r t 02120 Sep 7 14: 39 || + echo || || + l s || memo |

| cl ass_not es || $ |À_______________________________________________________________Ù

 The commands as r ead f r om t he BourneShel l scr i pt ar e i ndi cat ed byt he pl us si gn ( +) . The next l i ne or l i nes ar e t he resul t s of t heexecut i on of t he command. Usi ng t hi s t r aci ng opt i on al l ows you t ose t he execut i on of each command i n t he scr i pt and see the resul t sof t hat execut i on.

NOTESÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜWor kshop 1

 Thi s wor kshop wi l l r ei nf or ce your underst andi ng of t he i deaspr esent ed i n Chapt er 1. Each st udent i s t o compl ete t he ent i r ewor kshop.

DESK EXERCI SES

1. The Bour neShel l can act as a command l i ne

or a hi gh l evel .

2. The Bour neShel l i s one of t hr ee shel l s gener al l yavai l abl e. What are t he ot her t wo?

3. One advant age of usi ng a shel l scr i pt i s

10

Page 11: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 11/193

.

4. The command t o cal l t he Bour neShel l i s:

a. bour neb. kshc. bd. sh

5. Why woul d you use t r aci ng?

6. What UNI X command do you ent er t o make a Bour neShel lscri pt execut abl e?

 That ' s al lNOTES

ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ2. USER, SHELL, AND READ- ONLY SHELL VARI ABLES

 The BourneShel l has no t r ue numer i c var i abl es. I t uses st r i ngvar i abl es t o r epr esent number s, as wel l as t ext . St r i ng var i abl esare abl e t o t ake on t he val ue of a st r i ng of char act er s. Ther e ar et hr ee t ypes of var i abl es i n t he Bour neShel l . They ar e uservar i abl es, Bour neShel l var i abl es, and Read- onl y Bour neShel lvar i abl es.

 You can decl ar e, i ni t i al i ze, r ead, and modi f y user var i abl es f r oma Bour neShel l scr i pt or f r omt he command l i ne. The Bour neShel li t sel f decl ar es and i ni t i al i zes shel l var i abl es, but you can r eadand modi f y t hem. The Bour neShel l al so i ni t i al i zes t he r ead- onl yshel l var i abl es, and you can r ead but not modi f y them.

2. 1 User Var i abl es

I t i s l egal t o assi gn any sequence of non- bl ank char act er s as t hename of a var i abl e. The sampl e sessi on bel ow cr eat es a var i abl ecal l ed per son and i ni t i al i zes i t wi t h t he st r i ng Ri char d.

I t i s i mport ant t o note that you must NOT pr ecede or f ol l ow t heequal si gn wi t h a space or TAB character .

Sampl e Sessi on:

11

Page 12: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 12/193

 Ú_______________________________________________________________¿| $person=Ri char d |À_______________________________________________________________Ù

 Thi s sampl e sessi on i ndi cat es t hat person does not r epresent t hest r i ng Ri char d. The st r i ng person i s echoed as per son. TheBour neShel l wi l l onl y do t he subst i t ut i on of t he val ue of t hevar i abl e when t he name of t he vari abl e i s preceded wi t h a dol l arsi gn ( $) .

Sampl e Sesssi on:

Ú_______________________________________________________________¿| $echo per son || per son || $echo $person || Ri char d || $ |

À_______________________________________________________________ÙI f you want t o have i mbedded spaces i n a var i abl e, i t i s necessar yt o quot e t he st r i ng.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $person=' Ri chard and Kathl een' || $echo $person || Ri chard and Kathl een || $ |À_______________________________________________________________Ù

 The echo ut i l i t y copi es i t s ar gument s t o t he st andar d output . Thecommand echo $per son di spl ays t he val ue of t he var i abl e person.I t wi l l not di spl ay $per son because t he Bour neShel l doesn' t pass$person as an argument . The l eadi ng dol l ar si gn ( $) causes t heBour neShel l t o subst i t ut e t he val ue of t he var i abl e and t hen passest hat val ue t o t he ut i l i t y. The echo ut i l i t y t hen di spl ays t heval ue of t he vari abl e, not i t s name, never knowi ng t hat you cal l edi t wi t h a var i abl e. The Bour neShel l passed t he same command l i neas i f you had t yped i n echo Ri chard and Kathl een. The Bour neShel lcan be pr event ed f r omdoi ng t hi s subst i t ut i on by ent er i ng one of t he f ol l owi ng:

Sampl e Sess i ons:

Ú_______________________________________________________________¿| $echo $person || Ri chard and Kathl een || $ |À_______________________________________________________________Ù

I n t hi s sessi on the cont ent s of t he var i abl e per son ar e di spl ayed. The BourneShel l made t he subst i t ut i on because t he var i abl e nameper son i s pr eceded by a dol l ar si gn ( $) .

12

Page 13: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 13/193

Ú_______________________________________________________________¿| $echo \ $person || $person || $ |À_______________________________________________________________Ù

I n t he above exampl e the var i abl e person i s preceded by a dol l arsi gn ( $) but t he dol l ar si gn has a backsl ash ( \ ) ahead of i t . Thebacksl ash has t he ef f ect of cancel l i ng t he speci al meani ng of t hechar act er f ol l owi ng t he backsl ash. I n t hi s case, t he speci almeani ng of t he dol l ar si gn i s i gnored and t he subst i t ut i on i s notdone.

Ú_______________________________________________________________¿| $echo ' $per son' || $person || $ |

À_______________________________________________________________Ù

 The si ngl e quot e mar ks ( ' ) causes t he char act er s between t he mar kst o be t aken as l i t er al . The shel l makes no att empt t o i nt erpret t hemeani ngs of t hese charact ers . The shel l passes t hese charact ers onwi t h no subst i t ut i on.

Ú_______________________________________________________________¿| $echo "$person" || Ri chard and Kathl een || $ |À_______________________________________________________________Ù

 The doubl e quot e mar ks do not prevent t he shel l f r om maki ngsubst i t ut i on; and t he val ue of t he var i abl e wi l l be di spl ayed bythe ut i l i t y.2. 2 Shel l Var i abl es

 The BourneShel 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 ect or i es t he shel l wi l ll ook i n when you gi ve commands, how of t en t o l ook f or mai l , yourpr ompt , and many other t hi ngs. We wi l l l ook at sever al of t heseBour neShel l var i abl es and t hei r f unct i ons. You can assi gn newval ues t o these var i abl es f r om t he command l i ne or f r omt heexecut i on of t he . pr of i l e f i l e i n your home di r ect or y.

2. 2. 1 HOME

 The f i r st BourneShel 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 or y i s t he cur r ent wor ki ngdi r ect or y af t er you l ogi n. The system admi ni st r at or det er mi nesyour home di r ect ory when you est abl i sh an account and pl aces t hati nf or mat i on i n t he / et c/ passwd f i l e. When you l ogi n, t heBour neShel l get s t hat pat hname and assi gns i t t o t he HOME var i abl e.

13

Page 14: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 14/193

When you ent er a cd command wi t h no argument , t he ut i l i t y t akest he name of t he di r ect ory f r om t he HOME var i abl e and makes i t t hecur r ent worki ng di r ectory. I f you change t he HOME var i abl e t oanot her di r ect ory pathname, t he ut i l i t y wi l l make t he new di r ect or yt he cur r ent wor ki ng di r ect or y.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $echo $HOME || / user 0/ r har di ng || $cd || $pwd || / user 0/ r har di ng || $HOME=/ user0/ 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 ect s t hecd ut i l i t y. The cd command wi l l use the val ue of t he HOME var i abl eas t he pathname f or t he cur r ent worki ng di r ectory.2. 2. 2 I FS

 Thi s i s t he i nt er nal - f i el d separat or BourneShel l var i abl e. Youcan al ways use a space or t ab to separ at e char act ers on the commandl i ne. When you assi gn t he I FS var i abl e t o anot her charact er , youcan al so use t hi s char act er as t he f i el d separ at or .

Exampl e:. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. $num_args a: b: c: d .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

 Thi s exampl e shows onl y one ar gument , namel y a: b: c: d.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. $I FS=: .. $num_ar gs a: b: c: d .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

 Thi s exampl e now shows f our di f f er ent ar gument s; each bei ngsepar ated by t he new I FS, ( : ) .

2. 2. 3 MAI L

 The MAI L var i abl e cont ai ns t he name of t he f i l e t hat t he mai l ( andmai l x) ut i l i t i es use t o st or e your mai l . Usual l y, t he absol ut epat hname of t hi s f i l e i s / usr / mai l / name, where name i s your l ogi nname.

Exampl e:. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. $MAI L=/ usr / mai l / r har di ng .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

14

Page 15: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 15/193

2. 2. 4 MAI LPATH

 Thi s var i abl e cont ai ns a l i st of f i l enames separat ed by col ons. I f set , t he Bour neShel l wi l l i nf or m you when any of t hese f i l es ar emodi f i ed ( i . e. when new mai l ar r i ves) . Nor mal l y, t hi s var i abl e i snot set.2. 2. 5 MAI LCHECK 

 Thi s var i abl e speci f i es how of t en, i n seconds, t he BourneShel l wi l lcheck f or new mai l . The def aul t i s 600 seconds. I f set t o 0, i twi l l check f or new mai l each t i me bef ore i t gi ves you a pr ompt .

2. 2. 6 PATH

 Thi s BourneShel l var i abl e wi l l descr 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. TheBour neShel l l ooks i n sever al di r ectori es f or a f i l e t hat has t hesame name as t he command t hat you enter ed. The PATH var i abl econt r ol s t hi s sear ch pat h. Nor mal l y, t he f i r st di r ectory sear chedi s t he cur r ent wor ki ng di r ect or y. I f t he pr ogr am i s not f ound,

t he sear ch cont i nues i n t he / bi n and t hen t he / usr / bi n di r ect or y.Gener al l y, t hese di r ect or i es cont ai n execut abl e pr ogr ams. I f t hepr ogr ami s not f ound i n one of t hese di r ect ori es, t he Bour neShel lr epor t s t hat t he pr ogr am can' t be f ound ( or execut ed) .

 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 separated by a col on ( : ) .I f not hi ng ( nul l st r i ng) pr ecedes t he col on, t hat i ndi cat es t ost ar t t he sear ch at t he cur r ent wor ki ng di r ect or y.

Exampl e:. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. $PATH=: / user 0/ r har di ng/ bi n: / bi n: / usr / bi n .. $ .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

 Thi s PATH var i abl e i ndi cat es t o st ar t t he sear ch f or t he programat t he cur r ent wor ki ng di r ect or y, t hen l ook i n t he di r ect or y/ 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 path speci f i ed, each user can execut e adi f f erent pr ogr am by gi vi ng t he same command. The search 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 same namef or your own pr ogr ams as t he st andar d UNI X ut i l i t i es. To do t hi s,si mpl y put your pr ogr am i n one of t he f i r st di r ectori es t hat t heBour neShel l sear ches.2. 2. 7 PS1

 Thi s i s t he BourneShel l prompt whi ch l et s you know t hat t he shel li s wai t i ng f or you t o gi ve i t a command. The def aul t Bour neShel lpr ompt i s a dol l ar s i gn ( $) . The shel l st or es the pr ompt as ast r i ng var i abl e i n PS1. When you change t he val ue of t hi svar i abl e, t he appear ance of t he pr ompt wi l l change. When you arewor ki ng on sever al di f f er ent machi nes, i t mi ght be usef ul t o havet he pr ompt be t he name of t he machi ne you are wor ki ng on.

Sampl e Sessi on:

15

Page 16: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 16/193

 Ú_______________________________________________________________¿| $pwd || / user 0/ r har di ng || $PS1=' domax0: ' || domax0: |À_______________________________________________________________Ù

Not i ce t hat prompt i s now domax0:

2. 2. 8 PS2

 Thi s var i abl e i s cal l ed t he secondary prompt . I f t he command i snot compl et ed on one l i ne and must be cont i nued on t he next l i ne,t he pr ompt f or t hat cont i nued l i ne i s PS2. The def aul t i s >. Thi spr ompt i ndi cat es t hat t he Bour neShel l i s expect i ng you t o f i ni sht he pr evi ous command l i ne.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $echo ' demonst r at i on of pr ompt st r i ng || >2' || demonst r at i on of pr ompt st r i ng || 2 || $PS2=' Cont i nue? ' || $echo ' demonst r at i on of || Cont i nue? pr ompt st r i ng 2' || demonst r at i on of || pr ompt st r i ng 2 || $ |À_______________________________________________________________Ù

Not i ce how t he secondar y pr ompt was changed t o "Cont i nue? " .2. 3 Read- Onl y User Vari abl es

 The cont ent s of t he user var i abl es and t he shel l var i abl es can bemodi f i ed by t he user. I t i s possi bl e t o assi gn a new val ue t ot hem. The new val ue can be assi gned f r om t he dol l ar ( $) prompt orf r om i nsi de a Bour neShel l scri pt . Read- onl y var i abl es ar edi f f er ent . The val ue of r ead- onl y var i abl es can not be changed.

 The var i abl e must be i ni t i al i zed t o some val ue; and t hen, byent er i ng t he f ol l owi ng command, i t can be made r ead onl y.

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »

º Command f ormat : r eadonl y var i abl e_name ºº ºº var i abl e_name = name of t he var i abl e t o be made r ead onl y ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼ Sampl e Sessi on:

Ú_______________________________________________________________¿| $per son=Kat hl een || $r eadonl y person |

16

Page 17: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 17/193

| $echo $person || Kat hl een || $person=Ri chard || per son: i s r ead onl y || $ |À_______________________________________________________________Ù

 The r eadonl y command gi ven wi t hout any ar guments wi l l di spl ay al i st of al l t he read- onl y var i abl es.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $per son=Kat hl een || $r eadonl y person || $exampl e=Ri chard || $readonl y exampl e || $r eadonl y || r eadonl y person || r eadonl y exampl e |

| $ |À_______________________________________________________________Ù2. 4 Read- Onl y Shel l Var i abl es

 The r ead- onl y shel l var i abl es ar e si mi l ar t o t he r ead- onl y uservar i abl es; except t he val ue of t hese var i abl es i s assi gned by t heshel l , and the user CANNOT modi f y t hem.

2. 4. 1 Name of t he Cal l i ng Progr am  The shel l wi l l st or e t he name of t he command you used t o cal l aprogr am i n t he var i abl e named $0.

I t has t he number zero because i t appear s bef ore t he f i r st argumenton t he command l i ne.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $cat name_ex || echo ' The name of t he command used' || echo ' t o execut e t hi s scr i pt was' $0 || $name_ex || The name of t he command used || t o execut e t hi s scr i pt was name_ex || $ |

À_______________________________________________________________Ù

2. 4. 2 Ar gument s

 The BourneShel l wi l l st or e t he f i r st ni ne command l i ne ar gument si n t he var i abl es named $1, $2, . . . , $9. These var i abl es appear i nt hi s sect i on because you cannot change t hem usi ng t he equal si gn.I t i s possi bl e to modi f y t hem usi ng t he set command.

Sampl e Sessi on:

17

Page 18: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 18/193

 Ú_______________________________________________________________¿| $cat ar g_ex || echo ' The f i r st f i ve command l i ne' || echo ' argument s are' $1 $2 $3 $4 $5 || $arg_ex Ri chard Kat hl een Dougl as || The f i r st f i ve command l i ne || argument s are Ri char d Kathl een Dougl as || $ |À_______________________________________________________________Ù

 The scr i pt ar g_ex wi l l di spl ay t he f i r st f i ve command- l i neargument s. The var i abl es r epr esent i ng $4 and $5 have a nul l val ue.

 The BourneShel l var i abl e $* r epresents al l of t he command- l i neargument s as shown i n t he f ol l owi ng exampl e.

Sampl e Sessi on:

Ú_______________________________________________________________¿

| $cat di spl ay_al l || echo $* || $di spl ay_al l Ri chard Kathl een Dougl as || Ri chard Kathl een Dougl as || $ |À_______________________________________________________________Ù

 The BourneShel l var i abl e $# cont ai ns t he number of ar gument s ont he command l i ne. Thi s i s a st r i ng var i abl e t hat r epr esent s adeci mal number. You can use t he expr ut i l i t y t o perf ormcal cul at i ons wi t h t hat number and t est t o per f or m l ogi cal t est s oni t .

Sampl e Sessi on:

Ú_______________________________________________________________¿| $cat num_ar gs || echo ' Thi s scri pt was cal l ed wi t h' || echo $# ' argument s' || $num_ar gs Ri char d Kat hl een Dougl as || Thi s scri pt was cal l ed wi t h || 3 ar guments || $ |À_______________________________________________________________Ù2. 4. 3 Shi f t

 The shi f t command promot es each of t he command- l i ne ar gument s.

 The second ar gument , r epresent ed by $2, i s now t he f i r st ar gument ,r epr esent ed by $1. The t hi r d becomes t he second and so on unt i lt he l ast argument becomes the next t o l ast . You can access onl yt he f i r st ni ne command- l i ne argument s ( as $1 t hr ough $9) . Theshi f t command gi ves you access t o t he t ent h, and the f i r st becomesunavai l abl e. Ther e i s no "unshi f t " command t hat wi l l r et ur n t heargument s t hat are no l onger avai l abl e.

Sampl e Sessi on:

18

Page 19: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 19/193

Ú_______________________________________________________________¿| $cat demo_shi f t || echo ' arg1=' $1 ' arg2=' $2 ' arg3=' $3 || shi f t || echo ' arg1=' $1 ' arg2=' $2 ' arg3=' $3 || shi f t || echo ' arg1=' $1 ' arg2=' $2 ' arg3=' $3 || shi f t || echo ' arg1=' $1 ' arg2=' $2 ' arg3=' $3 || shi f t || $demo_shi f t Ri chard Kathl een Dougl as || arg1=Ri chard arg2=Kat hl een arg3=Dougl as || arg1=Kat hl een arg2=Dougl as arg3= || ar g1=Dougl as ar g2= ar g3= || demo_shi f t : cannot shi f t || $ |À_______________________________________________________________Ù

 The BourneShel l wi l l di spl ay an er r or message when t he scr i ptexecut es a shi f t command af t er i t has run out of var i abl es.

2. 4. 4 Set

 The Set command wi l l di spl ay a l i st of al l t he var i abl es t hat ar eset when i t has no argument s.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $set || HOME=/ user0/ t eacher || I FS= || || LOGNAME=r i char d || MAI L=/ usr / mai l / r i char d || MAI LCHECK=600 || PATH=: / bi n: / usr / bi n || PS1=$ || PS2=> || SHELL=/ bi n/ sh || TERM=vt 100 || TZ=MST7MDT || $ |À_______________________________________________________________Ù

When set i s cal l ed wi t h argument s, i t set s t he val ue of t hecommand- l i ne argument s ( $1- $n) t o the argument s. The exampl e set s

t he f i r st t hr ee ar gument s.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $cat set _ex || set who r eal l y car es || echo $#: $* || $set _ex || 3: who r eal l y car es |

19

Page 20: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 20/193

| $ |À_______________________________________________________________Ù2. 4. 5 expr

 The expr command wi l l per f or m ar i t hmet i c i n t he BourneShel l .

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command f or mat : expr expr essi on ºº ºº See Appendi x C f or a compl ete l i st of expr essi ons ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼  The ar gument s ar e t aken as an express i on. Af t er t he eval uat i on hast aken pl ace, t he r esul t i s wr i t t en t o st andard out put . The t er msof t he expr essi on must be separated by bl anks. Speci al charact erst o t he shel l must be escaped. St r i ngs cont ai ni ng bl anks or ot herspeci al char act ers must be quot ed.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $expr 7 + 8 + 10 || 25 || $expr 10 - 8 || 2 || $expr 10 ' *' 4 || 40 || $expr 135 / 5 || 27 || $ |À_______________________________________________________________Ù

expr wi l l al so wor k wi t h user def i ned var i abl es as i n t he f ol l owi ngexampl e:

Sampl e Sessi on:

Ú_______________________________________________________________¿| $cat dat a || 8 || 15 || 25 || $cat expr ess || count =0 || t ot =0 || f or a i n `cat dat a` || do |

| t ot = expr $t ot + $a` || count = expr $count + 1` || done || avg= expr $t ot / $count ` || echo "The average i s $avg" || $ |À_______________________________________________________________ÙLet ' s execut e t he scr i pt "expr ess" wi t h t r aci ng on so we can f ol l owt he execut i on.

20

Page 21: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 21/193

Page 22: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 22/193

 4. The backsl ash ( \ ) charact er i s used t o r emove t he speci al

meani ng of some char act er s.

 Tr ue/ Fal se

5. What other character can be used t o pr event t he shel lf r om doi ng t he subst i t ut i on?

6. Doubl e quote marks wi l l pr event t he shel l f r om maki ngt he subst i t ut i on.

 Tr ue/ Fal se

Cont i nue on t he next page7. What do t he f ol l owi ng shel l vari abl es do?

HOME

I FS

MAI L

MAI LPATH

MAI LCHECK 

PATH

22

Page 23: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 23/193

 

PS1

PS2

Cont i nue on t he next page8. What i s t he command t o cr eat e a r ead- onl y user var i abl e?

9. What i s t he r ead- onl y shel l var i abl e t hat r epr esent s t hecal l i ng pr ogr am?

10. What do $1, $2, . . . , $9 r epr esent ?

11. What Bour neShel l var i abl e r epr esent s al l of t he commandl i ne argument s?

12. What does the shi f t command do?

13. What i s di spl ayed when you ent er set wi t h no argument s?

23

Page 24: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 24/193

 

Cont i nue on t he next pageCOMPUTER EXERCI SES

14. Logi n t o t he Mul t i max ( domax1) usi ng t he username andpassword gi ven to you by t he i nst r uct or .

15. Cr eat e a subdi r ect or y cal l ed sub_di r .

16. Modi f y your . pr of i l e t o i ncl ude t he f ol l owi ng:

a) Change t he home di r ect ory t o sub_di r

b) Set t he i nt ernal - f i el d separ at or t o a comma

c) Have mai l messages saved i nto mai l 1.

d) Set t he PATH t o l ook f or progr ams i n t he f ol l owi ngdi rect or i es:

$HOME/ bi n/ b i n/ us r / bi n

e) Change t he pr ompt t o ref l ect t he name of t he syst em

  f ) Change t he secondary pr ompt t o ' More?'

17. Execut e t he . pr of i l eEnter $. . prof i l e

24

Page 25: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 25/193

 

18. Ver i f y t hat t he changes ar e cor r ect . I f you have extr at i me go t o t he next page.

Ext r a Mi l e on t he next pageExt r a Mi l e

Change t he . pr of i l e f i l e so t he dat e/ t i me and a l i st of al lusers t hat ar e cur r ent l y l ogged i n wi l l be di spl ayed on yourmoni t or scr een aut omat i cal l y when you l ogi n.

NOTESÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ3. POSI TI ONAL PARAMETERS

A Bour neShel l scr i pt can al so r ead i n command- l i ne argument s. The f i r st ar gument i s r ef er r ed t o as $1, t he second i s $2, and soon. Command- l i ne argument s ar e r ef err ed t o as posi t i onalpar amet ers.

Let ' s l ook at an exampl e Bour neShel l scr i pt t o see how t hese areused.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $cat neat _shel l || echo $1 $2 $3 || echo $0 i s t he name of t he shel l scr i pt || echo "There were $# argument s. " || echo $* || $ |À_______________________________________________________________Ù

I nsur e t hat t he Bour neShel l scr i pt i s execut abl e by i ssui ng t hi scommand:

Sampl e Sessi on:

Ú_______________________________________________________________¿| $chmod a+x neat _shel l || $ |

À_______________________________________________________________Ù

Now, i f we type the name of t he Bour neShel l scr i pt wi t h noargument s, we get t he f ol l owi ng resul t s.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $neat _shel l || |

25

Page 26: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 26/193

| neat _shel l i s t he name of t he shel l scri pt || There were 0 argument s. || || $ |À_______________________________________________________________Ù

I n thi s sampl e sessi on, t here were no ar gument s gi ven so none werepr i nt ed. $0 i s t he posi t i onal par amet er t hat r ef er s t o t he nameof t he scr i pt . Si nce t here were no ar gument s gi ven wi t h t hi si nvocat i on of neat _shel l , t her e were zer o ar gument s l i st ed.

3. 1 Readi ng I nput I nt o a Shel l Var i abl e

 The BourneShel l scr i pt can r ead user i nput f r om st andar d i nput . The r ead command wi l l r ead one l i ne f r om st andar d i nput and ass i gnt he l i ne t o one or more var i abl es. The f ol l owi ng exampl e shows howt hi s wor ks.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $cat r ead_scri pt || echo "Pl ease ent er a st r i ng of your choi ce" || r ead a || echo $a || $ |À_______________________________________________________________Ù

 Thi s si mpl e scr i pt wi l l r ead one l i ne f r om st andar d i nput( keyboar d) and assi gn i t t o t he var i abl e a.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $r ead_scri pt || Pl ease ent er a st r i ng of your choi ce || Here i t i s || Here i t i s || $ |À_______________________________________________________________Ù

 The l i ne r ead f r om st andar d i nput can al so be ass i gned t o sever alvar i abl es as shown i n t he f ol l owi ng exampl e.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $cat r eads || echo "Pl ease ent er t hr ee st r i ngs" || r ead a b c || echo $a $b $c || echo $c || echo $b || echo $a || $ |À_______________________________________________________________Ù

26

Page 27: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 27/193

Page 28: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 28/193

Page 29: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 29/193

Page 30: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 30/193

 3. 4 Bour neShel l Envi r onment - Export i ng Var i abl es

Wi t hi n a pr ocess, you can decl ar e, i ni t i al i ze, r ead, and modi f yvar i abl es. The var i abl e i s l ocal t o t hat pr ocess. When a pr ocessf or ks a chi l d pr ocess, t he par ent pr ocess does not aut omat i cal l ypass t he val ue of t he var i abl e t o t he chi l d pr ocess.

Here i s an exampl e of t he vari abl es not bei ng export ed.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $cat no_expor t || car=mercedes # set t he var i abl e || echo $0 $car $$ # $0 = name of f i l e execut ed || # $car =val ue of var i abl e car || # $$ = PI D number ( process i d) || i nner # execut e another Bour neShel l scr i pt || echo $0 $car $$ # di spl ay same as above |

| $cat i nner || echo $0 $car $$ # di spl ay var i abl es f or t hi s pr ocess || $chmod a+x no_expor t || $chmod a+x i nner || $no_export || no_expor t mer cedes 4790 || i nner 4792 || no_expor t mer cedes 4790 || $ |À_______________________________________________________________Ù

When no_export was execut ed, i t , of cour se, assi gned a val ue of mer cedes t o t he var i abl e car and pr i nt ed i t out . The cal l t o i nnercreat ed a chi l d pr ocess. I t s PI D i s 4792, whi l e t he par ent PI D i s4790. Not i ce, when i nner t r i ed t o pr i nt t he val ue of car , i tpr i nt ed nothi ng. The r eason i s because the val ue of car was notpassed by t he par ent .Can t he val ue be passed f r omparent t o chi l d pr ocess? Yes, byusi ng t he expor t command. Let ' s l ook at an exampl e.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $cat expor t _i t || car =mercedes || expor t car || echo $0 $car $$ |

| i nner 1 || echo $0 $car $$ || $cat i nner 1 || echo $0 $car $$ || car=chevy || echo $0 $car $$ || $chmod a+x expor t _i t || $chmod a+x i nner 1 || $expor t _i t || expor t _i t mercedes 4798 |

30

Page 31: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 31/193

| i nner1 mercedes 4800 || i nner1 chevy 4800 || expor t _i t mercedes 4798 || $ |À_______________________________________________________________Ù

I n t he expor t _i t Bour neShel l scr i pt , t he var i abl e car wasi ni t i al i zed t o mercedes; and t hen i t was export ed. Thi s means thatt he val ue of car i s now avai l abl e t o a chi l d pr ocess. When i nner 1pr i nt s out t he val ue of car i t has t he val ue of mer cedes. Thi s i sas we expect because t he val ue of car was export ed f r om t he parent . The next l i ne of i nner 1 changes t he val ue of car t o chevy. Thi si s shown i n t he next l i ne of t he sampl e sessi on. The l ast l i ne of t he sessi on shows t he retur n to t he parent pr ocess and the val uei s st i l l mer cedes. How i s t hi s possi bl e?

Expor t i ng var i abl es i s onl y val i d f r om t he par ent t o t he chi l dpr ocess. The chi l d pr ocess cannot change t he parent ' s var i abl e.Wor kshop 3

 Thi s wor kshop wi l l r ei nf or ce your underst andi ng of t he i deaspresent ed i n Chapt er 3. Logi n to t he Mul t i max usi ng t he user nameand password gi ven t o you by t he i nst r uct or. Each st udent i s t ocompl et e t he ent i r e workshop.

DESK EXERCI SES

1. What i s t he posi t i onal parameter t hat r epr esent s t he nameof t he command?

2. What posi t i onal parameter st ands f or the number of ar guments on t he command l i ne?

3. What command wi l l r ead one l i ne f r om st andard i nput andassi gn t he val ue t o a var i abl e?

4. What charact er i s used t o i ndi cat e command subst i t ut i on?

31

Page 32: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 32/193

5. What ar e t he t wo charact ers t hat i ndi cat e comment s i nBour neShel l scr i pt s?

Cont i nue on t he next page

6. Why i s i t bad pr act i ce t o put a pound si gn ( #) i n t hef i rst char act er posi t i on of t he f i rst l i ne of aBour neShel l scr i pt ?

7. Var i abl es set by t he par ent pr ocess ar e aut omat i cal l yknown t o t he chi l d pr ocess.

 Tr ue/ Fal se

8. What command wi l l al l ow t he val ue of a var i abl e t o bepassed t o a chi l d pr ocess?

9. Can a chi l d pr ocess change t he val ue of t he parent s'var i abl e? Why?

COMPUTER EXERCI SES

10. Wr i t e a Bour neShel l scri pt cal l ed "r ever se_i t " t hat hast hr ee st r i ngs as par amet er s and t hen di spl ay t he st r i ngsi n opposi t e or der . Be sur e to i ncl ude appr opr i at ecomment s.

Hi nt : posi t i onal par amet er s

11. Wr i t e a Bour neShel l scri pt cal l ed "r ead_i t " t hat doest he same as quest i on 10 but prompts t he user t o ent er

32

Page 33: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 33/193

Page 34: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 34/193

  You can negat e any cr i t er i on by precedi ng i t wi t h an excl amat i onmar k ( ! ) . Par ent heses can be used t o gr oup cri t er i a. I f t her eare no parent heses, t he - a ( l ogi cal AND operator) t akes pr ecedenceover t he - o ( l ogi cal OR oper at or ) . The t est ut i l i t y wi l l eval uat eoper at or s of equal pr ecedence f r om l ef t t o r i ght .

Wi t hi n t he expr essi on i t sel f , you must put speci al char act er s, suchas parent heses, i n quote marks so t he Bour neShel l wi l l not eval uatet hem but wi l l pass them t o t est .

Si nce each el ement ( eval uat i on cri t er i on, st r i ng, or var i abl e) i nan expr essi on i s a separ at e ar gument , each must be separ at ed by aspace.

 The t est ut i l i t y wi l l wor k f r om t he command l i ne but i t i s mor eof t en used i n a scri pt t o t est i nput or ver i f y access t o a f i l e.

Another way t o do t he t est eval uat i on i s t o sur r ound t he expr essi onwi t h l ef t and r i ght br acket s. A space char act er must appear af t er

t he l ef t br acket and bef or e the r i ght br acket .

t est expr essi on = [ expr essi on ]

4. 2 Test on Numer i c Val ues

 Test expressi ons can be i n many di f f er ent f or ms. The expressi onscan appear as a set of eval uat i on cri t er i a. The gener al f or m f ort est i ng numer i c val ues i s:

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º i nt 1 op i nt 2 ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼  Thi s cr i t er i on i s t r ue i f t he i nt eger i nt 1 has t he speci f i edal gebr ai c r el at i onshi p t o i nt eger i nt 2.

 The val i d operat or s ( op) ar e:

- eq equal

- ne not equal

- gt gr eat er t han

- l t l ess than

- ge gr eat er t han or equal

- l e l ess t han or equal

4. 3 Test on Char act er St r i ngs

 The eval uat i on cr i t er i on f or char act er st r i ngs i s si mi l ar t o

34

Page 35: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 35/193

Page 36: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 36/193

f i l e

- d f i l ename t r ue i f f i l ename exi st s and i t i s adi r ectory.

- s f i l ename t r ue i f f i l ename exi t s and i t cont ai nsi nf or mat i on ( has a si ze gr eat er t han 0bytes)

Exampl e:. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. $t est - d new_di r .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

I f new_di r i s a di r ector y, t hi s cr i t er i on wi l l eval uat e t o t r ue.I f i t does not exi st , t hen i t wi l l be f al se.4. 5 i f t hen

 The f or mat f or t hi s const r uct i s:

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command Format : i f expr essi on ºº t hen commands ºº f i ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼  The i f st at ement eval uates t he express i on and t hen r et urns cont r olbased on t hi s st at us. The f i st at ement mar ks t he end of t he i f ,not i ce t hat f i i s i f spel l ed backwar d.

 The i f st at ement execut es t he st at ement s i mmedi at el y f ol l owi ng i ti f t he expr essi on r et ur ns a t r ue st at us. I f t he r et ur n st at us i sf al se, cont r ol wi l l t r ansf er t o t he statement f ol l owi ng t he f i .Sampl e Sessi on:

Ú_______________________________________________________________¿| $cat check_ar gs || i f ( t est $# = 0) || t hen echo ' Pl ease suppl y at l east 1 argument ' || exi t || f i || echo ' Pr ogr am i s r unni ng' || $ |À_______________________________________________________________Ù

 Thi s l i t t l e scr i pt wi l l check t o i nsur e t hat you ar e gi vi ng at

l east one ar gument . I f none ar e gi ven i t wi l l di spl ay t he er r ormessage and exi t . I f one or more ar gument s are gi ven i t wi l ldi spl ay "Progr am i s r unni ng" and r un t he r est of t he scri pt , i f any.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $check_ar gs || Pl ease suppl y at l east 1 argument |

36

Page 37: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 37/193

| $check_args xyz || Pr ogr am i s r unni ng || $ |À_______________________________________________________________Ù4. 6 i f then el se

 The f or mat f or t hi s const r uct i s:

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command Format : i f expr essi on ºº t hen commands ºº el se commands ºº f i ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼  The el se par t of t hi s st r uct ure makes t he si ngl e- branch i f st at ement i nt o a t wo- way br anch. I f t he expr essi on r eturns a t r uest at us, t he commands bet ween t he t hen and t he el se st at ement wi l lbe execut ed. Af t er t hese have been execut ed, cont r ol wi l l st ar t

agai n at t he stat ement af t er t he f i .

I f t he expr essi on r eturns f al se, t he commands f ol l owi ng t he el sest atement wi l l be execut ed.Sampl e Sessi on:

Ú_______________________________________________________________¿| $cat t est _str i ng || number =1 || numer o=0001 || i f t est $number = $numer o || t hen echo "St r i ng val ues of $number and $numero are equal " || el se echo "St r i ng val ues of $number and $numero not equal " || f i || i f t est $number - eq $numero || t hen echo "Numer i c val ues of $number and $numer o are equal " || el se echo "Numer i c val ues of $number and $numero not equal " || f i |À_______________________________________________________________Ù

Let ' s f ol l ow t he execut i on of t hi s scr i pt wi t h t r aci ng.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $sh - x t est_st r i ng |

| number =1 || numer o=0001 || + t est 1 = 0001 || + echo St r i ng val ues of 1 and 0001 are not equal || St r i ng val ues of 1 and 0001 are not equal || + t est 1 - eq 0001 || + echo Numer i c val ues of 1 and 0001 ar e equal || Numer i c val ues of 1 and 0001 are equal || $chmod a+x t est _st r i ng || $t est_st r i ng |

37

Page 38: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 38/193

| St r i ng val ues of 1 and 0001 are not equal || Numer i c val ues of 1 and 0001 are equal || $ |À_______________________________________________________________Ù4. 7 i f then el i f  

 The f or mat f or t hi s const r uct i s:

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command Format : i f expr essi on ºº t hen commands ºº el i f expr essi on ºº t hen commands ºº el se commands ºº f i ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼  The el i f const r uct combi nes t he el se and i f st at ements and al l owsyou t o const r uct a nest ed set of i f t hen el se str uct ur es.

4. 8 f or

 The f or mat f or t hi s const r uct i s:

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command Format : f or l oop- i ndex i n argument - l i st ºº do ºº commands ºº done ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼  Thi s st r uct ure wi l l ass i gn t he val ue of t he f i r st i t em i n t heargument l i st t o t he l oop i ndex and execut es t he commands bet weent he do and done st at ement s. The do and done st at ement s i ndi cat et he begi nni ng and end of t he f or l oop.

Af t er t he st r uct ur e passes cont r ol t o t he done st at ement , i tassi gns t he val ue of t he second i t em i n t he ar gument l i st t o t hel oop i ndex and r epeat s t he commands. The st r uct ure wi l l r epeatt he commands bet ween the do and done st at ement s once f or eachargument i n t he argument l i st . When t he argument l i st has beenexhaust ed, cont r ol passes t o the st atement f ol l owi ng t he done.Sampl e Sessi on:

Ú_______________________________________________________________¿| $cat f i nd_henr y1 |

| f or x i n pr oj ect1 pr oj ect2 pr oj ect3 || do || gr ep henr y $x || done |À_______________________________________________________________Ù

Sampl e Sessi on:

Ú_______________________________________________________________¿| $head pr oj ect ? |

38

Page 39: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 39/193

| ==> pr oj ect 1 <== || henr y || j oe || mi ke || sue || || ==> pr oj ect 2 <== || j oe || mi ke || sue || || ==> pr oj ect 3 <== || j oe || mi ke || sue || henr y || || ==> pr oj ect 4 <== || j oe || mi ke |

| || $f i nd_henr y || henr y || henr y || $ |À_______________________________________________________________Ù

Each f i l e i n t he ar gument l i st was sear ched f or t he st r i ng, henr y.When a mat ch was f ound, t he st r i ng was pr i nted.4. 9 whi l e

 The f or mat f or t hi s const r uct i s:

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command For mat : whi l e expr essi on ºº do ºº commands ºº done ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼As l ong as t he expr essi on r et ur ns a tr ue exi t st at us, t he str uct ur econt i nues t o execute t he commands bet ween t he do and the donest atement . Bef ore each l oop t hr ough t he commands, t he st r uct ur eexecut es t he expr essi on. When t he exi t st atus of t he expr essi oni s f al se ( non- zer o) , cont r ol i s passed t o the st at ement f ol l owi ngt he done st at ement .

 The commands t o be execut ed must change t he expressi on t est or ani nf i ni t e l oop can r esul t .4. 10 unt i l

 The f or mat f or t hi s const r uct i s:

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command Format : unt i l expr essi on º

39

Page 40: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 40/193

º do ºº commands ºº done ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼  The unt i l and whi l e st r uct ures ar e ver y si mi l ar . The onl ydi f f er ence i s that t he t est i s at t he t op of t he l oop. The unt i lst r ucture wi l l cont i nue t o l oop unt i l t he expr essi on r et ur ns t r ueor a noner r or condi t i on. The whi l e l oop wi l l cont i nue as l ong asa tr ue or noner r or condi t i on i s r et ur ned.

 The commands t o be execut ed must change t he expressi on t est or ani nf i ni t e l oop can r esul t .Sampl e Sessi on:

Ú_______________________________________________________________¿| $cat unt i l _ex || secretname=' j enny' || name=' noname' || echo ' Try t o guess t he secret name!' |

| echo || unt i l ( t est "$name" = "$secretname") || do || echo ' Your guess: \ c' || r ead name || done || echo ' You di d i t ! ' || $ |À_______________________________________________________________Ù

 The unt i l l oop wi l l cont i nue unt i l name i s equal t o t he secr etname.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $chmod a+x unt i l _ex || $unt i l _ex || Try t o guess t he secr et name! || || Your guess: gayl an || Your guess: ar t || Your guess: r i char d || Your guess: j enny || You di d i t ! || $ |

À_______________________________________________________________Ù4. 11 case

 The f or mat f or t hi s const r uct i s:

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command Format : case t est - st r i ng i n ºº pat t ern- 1 ) commands- 1 ; ; ºº pat t ern- 2 ) commands- 2 ; ; º

40

Page 41: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 41/193

Page 42: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 42/193

t r ue ( equal t o zer o) or f al se ( not equal t o zer o) ?

2. What are t he operat ors f or charact er st r i ng compari sons?

Cont i nue on t he next pageCOMPUTER EXERCI SES

3. Use t he "i f t hen" const r uct t o wr i t e a Bour neShel l scr i ptt hat wi l l check f or at l east t wo par ameter s bei ng pr esenton t he command l i ne. Output an appropr i at e err or message.

4. Wr i t e a Bour neShel l scri pt usi ng t he "i f t hen el se"const r uct t hat wi l l check f or equal i t y of t wo st r i ngst hat ar e suppl i ed as par amet ers t o the scri pt . Out put amessage st at i ng i f t he st r i ngs are equal or not equal .

5. Wr i t e a Bour neShel l scri pt usi ng t he "i f t hen el i f "const r uct t hat wi l l check t wo numbers , i nput aspar amet er s, and t el l i f t he f i r st par amet er i s gr eat er

t han, equal t o, or l ess t han t he second number . Out putappr opr i at e err or messages.

6. Wr i t e a Bour neShel l scri pt usi ng t he "f or " const r uct t hathas a l oop i ndex cal l ed "f r ui t " and an ar gument l i st as

42

Page 43: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 43/193

f ol l ows: appl es or anges bananas pears. Echo t he name of each argument t o t he moni t or scr een and when t he l astargument i s l i st ed out put an appr opr i at e message.

Cont i nue on t he next page

7. Wr i t e a Bour neShel l scri pt usi ng t he "whi l e" const r uctt hat wi l l add al l t he numbers bet ween 0 and 9 and di spl ayt he r esul t . The sum of t he di gi t s 0 t hr ough 9 i s 45.

8. Wr i t e a Bour neShel l scri pt usi ng t he "unt i l " const r uctsi mi l ar t o the exampl e i n t he manual except compar enumbers i nst ead of st r i ngs.

9. Wr i t e a Bour neShel l scr i pt usi ng t he "case" st atementt hat wi l l ask you t o ent er t he day of t he week and thenecho that day t o the moni t or scr een. Be sur e to i ncl udean appr opr i ate message i f you ent er i n a st r i ng othert han a val i d day of t he week.

EXTRA MI LE

10. Wr i t e a Bour neShel l scri pt cal l ed "di r _num" t hat wi l lt est al l of t he f i l es i n t he cur r ent di r ector y and pr i ntal l t he f i l es t hat pr ove t o be a di r ectory.

NOTESÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ

43

Page 44: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 44/193

5. COMPI LI NG PROGRAMS I N UNI X

 Thi s chapt er wi l l exami ne compi l i ng source code programs i n t hreehi gh l evel l anguages " C" , FORTRAN, and COBOL. The second par t of t he chapt er wi l l l ook at t he ar chi ve and l i br ar y mai nt ai ner . Thearchi ve al l ows you t o creat e a l i br ar y of obj ect modul es. Thesef i l es ar e used by t he l i nk edi t or .

5. 1 "C" : Sampl e Progr am wi t h a Mai n and Two Funct i ons i n OneF i l e

Based on t he command l i ne opt i ons, cc compi l es, assembl es, and l oadC l anguage sour ce code pr ogr ams. I t can al so assembl e and l oadassembl y l anguage sour ce pr ogr ams or merel y l oad obj ect progr 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 ut i l i t y, t he f ol l owi ng convent i ons are observed:

1. A f i l ename wi t h t he extensi on of . c i ndi cat es a Cl anguage source program.

2. A f i l ename wi t h an ext ensi on of . s i ndi cat es an assembl yl anguage source program.

3. A f i l ename wi t h an ext ensi on of . o i ndi cat es an obj ectprogr 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 t wi l lst or e the execut abl e pr ogr am i n a f i l e cal l ed a. out .Sampl e C Language Source Code Pr ogr am:

Ú_______________________________________________________________¿| $cat hel l o. c || mai n ( ) || { || pr i nt f ( "Hel l o f rommai n! \ n\ n") ; || pr i nt f ( "Cal l i ng f unct i on1! \ n\ n") ; || f unct1( ) ; || pr i nt f ( " \ t Back f romf unct i on1! \ n\ n") ; || pr i nt f ( "Cal l i ng f unct i on2! \ n\ n") ; |

| f unct2( ) ; || pr i nt f ( " \ t Back f romf unct 2! \ n\ n") ; || pr i nt f ( "That ' s al l ! \ n\ n" ) ; || } || f unct1( ) || { || pr i nt f ( " \ t \ t Hel l o f rom f unct i on1! \ n\ n) ; || } || f unct2( ) || { |

44

Page 45: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 45/193

| pr i nt f ( " \ t \ t Hel l o f rom f unct i on2! \ n\ n) ; || } |À_______________________________________________________________Ù

5. 2 "C" : Compi l i ng a Progr am 

 To compi l e t he previ ous exampl e program i nt o an execut abl e modul e,enter t he f ol l owi ng command at t he command l i ne.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $cc hel l o. c || $ |À_______________________________________________________________Ù

Wi t hout any opt i ons, cc accept s C source code and assembl y l anguagepr ogr ams t hat f ol l ow t he convent i ons out l i ned above. I t wi l lcompi l e, assembl e, and l oad t hese progr ams t o pr oduce an execut abl e

cal l ed a. out . The cc ut i l i t y put s the obj ect code i n f i l es wi t ht he same base f i l ename ( everyt hi ng bef ore t he per i od) as t he sour cebut wi t h a f i l ename extensi on of . o . The a. out st ands f orassembl y out put . Thi s i s t he def aul t .

Sampl e Sessi on:

Ú_______________________________________________________________¿| $cc hel l o. c || $a. out || Hel l o f r om mai n! || || Cal l i ng f uncti on1! || || Hel l o f r om f uncti on1! || || Back f r om f uncti on1! || || Cal l i ng f uncti on2! || || Hel l o f r om f uncti on2! || || Back f r om f uncti on2! || || That ' s al l ! || $ |À_______________________________________________________________Ù

NOTE: The a. out f i l e t hat was creat ed by t he cc ut i l i t y hast he f ol l owi ng per mi ssi ons:

user - r ead, wr i t e, and execut egroup - 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 he

45

Page 46: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 46/193

chmod command because t he cc ut i l i t y set t he execut e permi ss i onsf or you.5. 3 "C" : 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. Thef 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 Sessi on:

Ú_______________________________________________________________¿| $mv a. out hel l o || $hel l o || Hel l o f r om mai n! || || Cal l i ng f uncti on1! || || Hel l o f r om f uncti on1! || || Back f r om f uncti on1! |

| || Cal l i ng f uncti on2! || || Hel l o f r om f uncti on2! || || Back f r om f uncti on2! || || That ' s al l ! || $ |À_______________________________________________________________Ù5. 4 "C" : 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 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 he speci f i edname f or t he out put i nst ead of t he def aul t a. out .

NOTE: I t i s not necessary f or t he - o opt i on t o appear af t er t hecc command. The f i l ename t hat appears af t er t he - o i st 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 Sessi on:

Ú_______________________________________________________________¿| $cc - o hel l o hel l o. c || $hel l o |

46

Page 47: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 47/193

| Hel l o f r om mai n! || Cal l i ng f uncti on1! || || Hel l o f r om f uncti on1! || || Back f r om f uncti on1! || || Cal l i ng f uncti on2! || || Hel l o f r om f uncti on2! || || Back f r om f uncti on2! || || That ' s al l ! || $ |À_______________________________________________________________Ù5. 5 "C" : Pr oduci ng an Assembl y Li st i ng

 Thi s opt i on causes cc t o compi l e C programs and l eave t he

corr espondi ng assembl y l anguage sour ce pr ogr ams i n a f i l e wi t hf i l ename ext ensi ons of . s.

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command Format : cc - S hel l o. c ºº ºº - S = Compi l e onl y ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼ Sampl e Sessi on:

Ú_______________________________________________________________¿| $cc - S hel l o. c || $l s - C || exampl e. f hel l o hex. c octal . c || hel l o. c hel l o. s mul t i pl y. c || $ |À_______________________________________________________________Ù5. 6 "C" : Mai n and Two Funct i ons i n Thr ee Separat e Sour ce Fi l es

 Thi s i s t he same C program 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 or e. The t hr ee f i l es ar emai n. c, f unct 1. c, and f unct 2. c.

Ú_______________________________________________________________¿

| $cat mai n. c || mai n ( ) || { || pr i nt f ( "Hel l o f rommai n! \ n\ n") ; || pr i nt f ( "Cal l i ng f unct i on1! \ n\ n") ; || f unct1( ) ; || pr i nt f ( " \ t Back f romf unct i on1! \ n\ n") ; || pr i nt f ( "Cal l i ng f unct i on2! \ n\ n") ; || f unct2( ) ; || pr i nt f ( " \ t Back f romf unct 2! \ n\ n") ; |

47

Page 48: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 48/193

Page 49: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 49/193

3. Sever al 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 st di n ( uni t 5) and st dout( uni t 6) .

 Thi s i s t he FORTRAN source code exampl e t o be used i n t he f ol l owi ngdi scussi ons of t he FORTRAN compi l er .

Sampl e Sessi on:

Ú_______________________________________________________________¿| $cat hel l o. f || pr ogr 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 subr out i ne1! ' , / ) || cal l sub1 || wr i t e( 6, 120) |

| 120 f or mat ( 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 or mat ( 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 || 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 || 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 |À_______________________________________________________________Ù5. 9 FORTRAN: Compi l i ng a Progr 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 program i nt o an execut abl e program, use t he

f ol l owi ng command at t he command l i ne.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $f 77 hel l o. f || $ |À_______________________________________________________________Ù

Wi t hout any opt i ons, f 77 accept s FORTRAN source code and assembl y

49

Page 50: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 50/193

l anguage pr ogr ams that f ol l ow t he convent i ons out l i ned above. I twi l l compi l e, assembl e, and l oad t hese pr ogr ams t o pr oduce 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 t h t he same base f i l ename (ever ythi ng bef or e theper i od) as t he sour ce but wi t h a f i l ename extensi on of . o. The a. out st ands f or assembl y output . Thi s i s t he def aul t .

Sampl e Sessi on:

Ú_______________________________________________________________¿| $f 77 hel l o. f || $a. out || Hel l o f r om mai n! || || Cal l i ng f uncti on1! || || Hel l o f r om f uncti on1! || || Back f r om f uncti on1! || |

| Cal l i ng f uncti on2! || || Hel l o f r om f uncti on2! || || Back f r om f uncti on2! || || That ' s al l ! || $ |À_______________________________________________________________Ù

NOTE: The a. out f i l e t hat was creat ed by t he f 77 ut i l i t y hast he f ol l owi ng per mi ssi ons:

user - r ead, wr i t e, and execut egroup - 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 execute permi ss i onsf or you.5. 10 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. Thef 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 Sessi on:

Ú_______________________________________________________________¿| $mv a. out hel l o || $hel l o || Hel l o f r om mai n! || || Cal l i ng f uncti on1! || |

50

Page 51: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 51/193

| Hel l o f r om f uncti on1! || || Back f r om f uncti on1! || || Cal l i ng f uncti on2! || || Hel l o f r om f uncti on2! || || Back f r om f uncti on2! || || That ' s al l ! || $ |À_______________________________________________________________Ù5. 11 FORTRAN: 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 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 o uset he speci 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 necessary 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 appears 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 Sessi on:

Ú_______________________________________________________________¿| $f 77 - o hel l o hel l o. f || $hel l o || Hel l o f r om mai n! || Cal l i ng f uncti on1! || || Hel l o f r om f uncti on1! || || Back f r om f uncti on1! || || Cal l i ng f uncti on2! |

| || Hel l o f r om f uncti on2! || || Back f r om f uncti on2! || || That ' s al l ! || $ |À_______________________________________________________________Ù5. 12 FORTRAN: Produci ng an Assembl y Li st i ng

51

Page 52: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 52/193

 Thi s opt i on causes f 77 t o compi l e For t r an programs and l eave t hecorr espondi ng assembl y l anguage sour ce pr ogr ams i n a f i l e wi t hf i l ename ext ensi ons of . s.

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command Format : f 77 - S hel l o. f ºº ºº - S = Compi l e onl y ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼ Sampl e Sessi on:

Ú_______________________________________________________________¿| $f 77 - S hel l o. f || $l s - C || exampl e. f hel l o hex. c octal . 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.5. 13 FORTRAN: Mai n and Two Subrout i nes i n Three Separ at e SourceFi l es

Sampl e Sessi on:

Ú_______________________________________________________________¿| $cat mai n. f || pr ogr 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 subr out i ne1! ' , / ) || cal l sub1 || wr i t e( 6, 120) || 120 f or mat ( 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 or mat ( t 15' Back f r om subr out i ne2! ' , / ) || wr i t e( 6, 150) || 150 f or mat ( ' 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 |À_______________________________________________________________Ù5. 14 FORTRAN: Compi l i ng But Not Produci ng an Execut abl e Modul e

52

Page 53: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 53/193

 Usi ng t he above progr am, t he f ol l owi ng command wi l l compi l e butnot produce an execut abl e modul e.

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command Format : 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 pr ogr ams ºº i n f i l es wi t h f i l ename ext ensi ons of . o. ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼ Sampl e Sessi on:

Ú_______________________________________________________________¿| $f 77 - c mai n. f sub1. f sub2. f || 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 unct1. o || f unct2. o || hel l o. o || mai n. o || sub1. o || sub2. o || $ |À_______________________________________________________________Ù

 The - c opt i ons causes t he compi l at i on syst em t o suppress 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 s exampl e( mai n. o sub1. o sub2. o) , t hat can be l i nk edi t ed at a l at er t i mewi t h t he f 77 command wi t h no opt i ons.5. 15 FORTRAN: Compi l i ng Obj ect Fi l es t o Produce an Execut abl e

Modul e

 The command t o produce an execut abl e nodul e f r om sever al obj ectf 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 Sessi 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 || $ |

53

Page 54: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 54/193

À_______________________________________________________________Ù5. 16 COBOL: Sampl e Progr am wi t h a Mai n and Two Subrout i nes

Sampl e Sessi on:

Ú_______________________________________________________________¿| $cat t eacher . cob || i dent i f i cat i on di vi s i on. || pr ogr am- i d. t eacher . || envi r onment di vi si on. || conf i gur at i on secti on. || dat a di vi s i on. || wor ki ng- st or age sect i on. || pr ocedur e di vi si on. || begi n secti 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 subr out 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 subr out i ne2. || di spl ay " Back f r om subr out i ne2! ". || di spl ay " That ' s al l , f ol ks! " . || stop r un. || subr out i ne1 secti on. || sub1. || di spl ay " Hel l o f r om subr out i ne1! ". || subr out i ne2 secti on. || sub2. || di spl ay " Hel l o f r om subr out i ne2! ". |À_______________________________________________________________Ù5. 17 COBOL: Compi l i ng a Progr am 

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command For mat : cobol sour ce_f i l ename ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼  Three f i l es ar e cr eat ed by t he compi l er . They ar e i dent i f i ed byt he same f i l ename as t he sour ce code but wi t h a di f f erentext ensi on. They have t he extensi ons . I DY, . I NT, and . LST.

NOTE: The ext ensi ons are uppercase charact ers. UNI X i s casesensi t i ve.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $cobol t eacher. cob || $l s t eacher * || t eacher . I DY || t eacher . I NT || t eacher . LST || t eacher . cob |

54

Page 55: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 55/193

| $ |À_______________________________________________________________Ù5. 18 COBOL: Runni ng a Pr ogr am 

Ú_______________________________________________________________¿| $cbr un t eacher. I NT || Hel l o f r om Mai n! || Cal l i ng subr out i ne1! || Hel l o f r om subr out i ne1! || Back f r om subr out i ne1! || Cal l i ng subr out i ne2! || Hel l o f r om subr out i ne2! || Back f r om subr out i ne2! || That ' s al l , f ol ks ! || $ |À_______________________________________________________________Ù

NOTESÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ

Wor kshop 5

 Thi s wor kshop wi l l r ei nf or ce your underst andi ng of t he i deaspresent ed i n Chapt er 5. Logi n to t he Mul t i max usi ng t he user nameand password gi ven t o you by t he i nst r uct or. Each st udent i s t ocompl et e t he ent i r e workshop.

DESK EXERCI SES

1. "C" : What i s t he command t o compi l e, assembl e, and l oadsour ce code pr ograms?

2. "C": What i s t he f i l ename extensi on t hat i ndi cat es asource code program? An assembl y l anguage pr ogram? 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?

55

Page 56: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 56/193

4. "C" : What command can be used t o r ename t he execut abl ef i l e pr oduced by the cc compi l er ? What ar e the f i l epr ot ect i ons associ at ed wi t h t he execut abl e?

5. "C" : What opt i on wi l l pr oduce an assembl y l i st i ng? Whati s the 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 source code progr 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 the command t o i nvoke t he compi l er ?

8. FORTRAN: What i s t he f i l ename ext ensi on f or sour ce codeprograms?

9. FORTRAN: What i s the name of t he def aul texecut 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 execute i t ?

56

Page 57: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 57/193

 11. FORTRAN: What opt i on on t he cal l t o t he compi l er wi l l

al l ow you to 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 l

pr 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 produce obj ect modul es but notproduce an execut abl e modul e?

14. FORTRAN: What command wi l l produce an execut abl e modul ef r om sever al 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 cr eat ed by t he compi l er?What are t he f i l ename ext ensi ons?

17. COBOL: Whi ch of t he t hr ee f i l es t hat have been cr eat edare used t o r un t he progr am?

57

Page 58: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 58/193

 

Cont i nue on t he next pageCOMPUTER EXERCI SES

18. Copy t he f ol l owi ng f i l es f r om / user 0/ t eacher :

mai n. c f unct 1. c f unct 2. c

Ar e t hese pr ogr ams " C" , FORTRAN, or COBOL? Compi l e t hese t hreef i l es cr eat i ng an execut abl e f i l e cal l ed mai n1. exe and t henexecut e i t . What ar e the f i l e pr ot ect i ons? Why?

19. Now append t he t hr ee f i l es i nt o one f i l e. Useout put r edi r ect i on.

Compi l e the f i l e cr eat i ng t he execut abl e f i l e cal l ed mai n2. exe. Execut e mai n2. exe.

20. Copy t he f ol l owi ng f i l es f r om / user 0/ t eacher :

mai n. f sub1. f sub2. f 

Compi l e t hese t hr ee f i l es creat i ng an execut abl e f i l ecal l ed mai n1. exe . Execut e mai n1. exe

21. Now append t he t hr ee f i l es i nt o one f i l e.

Compi l e t he f i l e creat i ng t he execut abl e f i l e cal l edmai n2. exe . Execut e mai n2. exe.

22. COBOL: Copy t eacher. cob f r om / user0/ t eacher.

Compi l e and r un i t .

NOTESÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜNOTES

ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ6. UNI X TOOLS

6. 1 Processes

A process i s t he execut i on of a command by UNI X. Processes can al sobe execut ed by t he oper at i ng syst em i t sel f . Li ke t he f i l e

58

Page 59: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 59/193

st r uctur e, t he pr ocess str uctur e i s hi er ar chi cal . I t cont ai nsparent s, chi l dr en, and even a root . A parent can f ork ( or spawn)a chi l d pr ocess. That chi l d can, i n t ur n, f or k ot her pr ocesses. The f i r st t hi ng t he operat i ng syst em does t o begi n execut i on i s t ocr eat e a si ngl e pr ocess, PI D number 1. PI D st ands f or Pr ocessI Dent i f i cat i on. Thi s pr ocess wi l l hol d t he same posi t i on as t her oot di r ectory i n t he f i l e st r ucture. Thi s pr ocess i s t he ancest ort o al l pr ocesses t hat each user wor ks wi t h. I t f or ks a pr ocess f oreach termi nal . Each one of t hese pr ocesses becomes a Shel l pr ocesswhen t he user l ogs on.

6. 2 Execut i ng a Command

When you gi ve a command t o the Shel l , i t wi l l f ork a pr ocess t oexecut e the command. Whi l e the chi l d pr ocess i s execut i ng t hecommand, t he parent wi l l go to 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 t i sawakened. When t he chi l d pr ocess has f i ni shed execut i ng t hecommand, i t di es. The parent pr ocess, whi ch i s r unni ng t he Shel l ,wakes up and prompt s you f or anot her command.

When you r equest a pr ocess t o r un i n the background ( by endi ng t hecommand l i ne wi t h an &) , t he Shel l f or ks a chi l d pr ocess t hat i sal l owed t o run t o compl et i on. The par ent pr ocess wi l l r eport t hePI D of t he chi l d process and t hem prompts you f or anot her command. The chi l d and parent ar e now i ndependent processes.

6. 3 Pr ocess I dent i f i cat i on

 The Uni x operat i ng syst em ass i gns a uni que process i dent i f i cat i onnumber ( PI D) t o each pr ocess. I t wi l l keep t he same PI D as l ong ast he pr ocess i s i n exi st ence. Dur i ng one sessi on, t he same pr ocessi s al ways execut i ng t he l ogi n Shel l . When you execut e anot hercommand a new process i s f or ked and a new PI D i s assi gned t o t hatpr ocess. When t hat chi l d pr ocess i s f i ni shed you ar e r et ur ned t ot he l ogi n pr ocess, whi ch i s runni ng t he Shel l , and t hat par entprocess has t he same PI D as when you l ogged on. The Shel l st or es t he PI D i n Shel l var i abl e cal l ed $$. The PI D canal so be shown wi t h t he pr ocess st at us ( ps) command. The f ormat f orps 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 mat i on about pr ocesses associ at ed wi t h t he cont r ol l i ngt er mi 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 the commandname. Ot her wi se, opt i ons wi l l cont r ol t he di spl ay.

Sampl e sessi on:

Ú____________________________________________________________________________¿

59

Page 60: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 60/193

| $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 numbers 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 ut e i t s own PI D number f or $$. The Shel l makes t he subst i t ut i on bef or e i t f or ks a new pr ocess t oexecut e t he echo command. Theref ore, echo wi l l di spl ay the 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 spl ay mor e i nf or mat i on about t he processes.

Sampl e Sessi 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 ksh || 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 ksh || f 0000 O 115 8387 8347 26 73 20 1146000 72 r t 021a0 0: 01 ps

|| $

|À______________________________________________________________________________  ____________Ù

6. 4 gr ep: A Patt er n Mat chi ng Fi l t er

 The grep ut i l i t y can sear ch t hrough a f i l e t o see i f i t cont ai ns

a speci f i ed st r i ng of char acters. The ut i l i t y wi l l not change t hef i l e i t sear ches but di spl ays each l i ne t hat cont ai ns t he st r i ng. The f or mat f or t he st r i ng i s:

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º 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 ºº º

60

Page 61: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 61/193

ÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼  The grep ut i l i t y sear ches f i l es f or a pat t er n and di spl ays al ll i nes t hat cont ai n t he pat t er n. I t uses l i mi t ed- r egul ar -expr essi ons ( t hese ar e 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 ers ) l i ke t hose used wi t h ed t o match t he pat t erns.

Be car ef ul usi ng t he char acter s $, *, [ , , | , ( , ) , and \ i n t her egul ar expr essi 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 equot es. Thi s wi l l pr event t he shel l f r om eval uat i ng t hese speci alcharacters.

 The grep ut i l i t y wi l l assume st andar d i nput i f no f i l es ar e gi 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 ayed t ost andard out put .

Sampl e sessi 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 sc" . I twi l l i ncl ude wor ds l i ke di scover and i ndi screet because t heycont ai n t he charact ers "di sc" . The si ngl e quote marks are notnecessary and f or t hi s exampl e t hey woul dn' t have made anydi f f er ence. They do al l ow you to i ncl ude spaces i n t he searchpattern.6. 4. 1 More on Regul ar Expr essi ons

 The grep command can be best under st ood by a di scussi on of r egul arexpr essi ons. Let ' s cr eat e a database of phone numbers cal l edphone. l i s and t hen use regul ar expr essi ons t o sear ch t hr ough t hedat abase. Her e i s as l i st i ng of t he cont ent s of phone. l i s

Sampl e sessi on:

Ú_______________________________________________________________¿| $cat phone. l i s || Smi t h, J oan 7- 7989 || Adams, Fr an 2- 3876 || St Cl ai r , Fred 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 database i s:

Las t name, Fi r st name <t ab> #- ####

Usi ng t he dat abase ( phone. l i s) above. What grep command woul d weuse t o sear ch t hr ough the database and get al l t he recor ds t hathad a person whose name cont ai ns an "S".

61

Page 62: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 62/193

 An al phabet i c char act er r epr esent s i t sel f .

Sampl e sessi on:

Ú_______________________________________________________________¿| $gr ep S phone. l i s || Smi t h, J oan 7- 7989 || St Cl ai r , Fred 4- 6122 || St ai r , Ri ch 5- 5972 || Benson, Sam 4- 5587 || $ |À_______________________________________________________________Ù

 Thi s grep 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 matched.A si ngl e . ( dot ) i s used t o r epr esent any si ngl e char act er .

Sampl e sessi on:

Ú_______________________________________________________________¿| $gr ep . S phone. l i s || Benson, Sam 4- 5587 || $ |À_______________________________________________________________Ù

A $ r epr esent s t he end of t he l i ne.

Sampl e sessi 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 sessi on:

Ú_______________________________________________________________¿| $gr ep S phone. l i s || Smi t h, J oan 7- 7989 || St Cl ai r , Fred 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 pr oper l y. Let ' s say we want t o get t he recor ds of empl 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 essi on do?

Sampl e sessi on:

Ú_______________________________________________________________¿

62

Page 63: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 63/193

| $gr ep <t ab>4 phone. l i s || St Cl ai r , Fred 4- 6122 || J ones, Ted 1- 3745 || Benson, Sam 4- 5587 || $ |À_______________________________________________________________Ù

Why di d we get t he r ecord of Ted J ones? The tab char act er waseval uat ed by the shel l and so t he search was act ual l y made l ooki ngf or a "4". Thi s i s t he same as i f we had ent ered $gr ep 4 phone. l i s.We must pr event t he shel l f r om eval uat i ng t hese char act er s, t hi si s done wi t h t he \ ( backsl ash) charact er as shown i n t he nextexampl e.

Sampl e sessi on:

Ú_______________________________________________________________¿| $gr ep \ <t ab>4 phone. l i s || St Cl ai r , Fred 4- 6122 || Benson, Sam 4- 5587 |

| $ |À_______________________________________________________________Ù

Now i t worked pr oper l y. I t sear ched f or a <t ab> charact erf ol l owed by the number 4. The [ ] ( l ef t and r i ght br acket s) ar e usedt o i dent i f y a r ange of char act er s.

Sampl e sessi on:

Ú_______________________________________________________________¿| $gr ep \ [ AF] phone. l i s || Adams, Fr an 2- 3876 || St Cl ai r , Fred 4- 6122 || $ |À_______________________________________________________________Ù

Why do [ ] need t o be quot ed? I n the 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 make amat ch on a phone number t hat has a 1, 2, 3, or 4. How can thi s bedone? Here' s an exampl e:

Sampl e Sessi on:

Ú_______________________________________________________________¿| $gr ep \ [ 1- 4] phone. l i s |

| Adams, Fr an 2- 3876 || St Cl ai r , Fred 4- 6122 || J ones, Ted 1- 3745 || St ai r , Ri ch 5- 5972 || Benson, Sam 4- 5587 || $ |À_______________________________________________________________Ù

A ^ char act er l ooks f or al l char act er s NOT i nsi de t he [ ] br ackets .

63

Page 64: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 64/193

For exampl e,

[ 0- 9] mat ches al l non- di gi t s

[ a- zA- Z] mat ches al l non- al phabet i c char act er s

Not e: \ , *, and $ l ose t hei r met a- char act er meani ngs i nsi det he [ ] . Al so t he ^ char acter i s speci al onl 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 Sessi on:

Ú_______________________________________________________________¿| $gr ep ' [ 789] $' phone. l i s || Adams, Fr an 2- 3876 || St Cl ai r , Fred 4- 6122 || J ones, Ted 1- 3745 || St ai r , Ri ch 5- 5972 || $ |

À_______________________________________________________________Ù

6. 4. 2 Cl osur e

 The * ( ast er i sk) r epresent s zer o or mor e of t he char act er spr ecedi ng t he aster i sk.

A* r epr esent s 0 or more As.

AA* r epr esent s 1 or more 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 more of any charact er.

How woul d you wr i t e a gr ep command usi ng r egul ar expr essi ons t of i nd t he l ast name st ar t i ng wi t h an "S" and the f i r st name wi t h an"F"?

S Begi ns wi t h an "S"

. *, F Any number of charact ers bef ore , F

Sampl e sessi on:

Ú_______________________________________________________________¿| $gr ep S. \ *, F phone. l i s || St Cl ai r , Fred 4- 6122 || $ |À_______________________________________________________________Ù

Not e: The * ( ast er i sk) was quot ed so t he shel l di dn' t t r y toeval uat e i t .

64

Page 65: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 65/193

I t i s ver y desi r eabl 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 ncreasesr eadabi l i t y of t he regul ar expr essi on as i n the f ol l owi ng exampl e:

Sampl e sessi on:

Ú_______________________________________________________________¿| $gr ep ' S. *, F' phone. l i s || St Cl ai r , Fred 4- 6122 || $ |À_______________________________________________________________Ù

6. 4. 3 Some Ni ce grep Opt i ons

 The grep provi des sever al opt i ons t hat modi f y how t he sear ch i sperf or med.

- c Repor t count of mat chi ng l i nes onl y

- v Pr i nt t hose l i nes t hat don' t mat ch t he pat t er n.

What wi l l t hese l i nes pr i nt ?

Sampl e sessi on:

Ú_______________________________________________________________¿| $grep - c ' [ J - Z] ' phone. l i s || 5 || $ |À_______________________________________________________________Ù

Why di d we get t hi s r esul t ? Let ' s anal yze the command. I nEngl i sh, t hi s command coul d be i nt erpr et ed t o mean "Tel l me howmany recor ds i n t he f i l e "phone. l i s" cont ai n a l et t er f r om t he set J t hrough and i ncl udi ng Z. " Look at t he phone. l i s f i l e and seet hat f i ve records f i t thi s rest r i ct i on.So t he answer i s 5.

Now l ook at anot her exampl e and see what t hi s one does.

Sampl e sessi on:

Ú_______________________________________________________________¿| $grep - v ' [ J - Z] ' phone. l i s || Adams, Fr an 2- 3876 || $ |À_______________________________________________________________Ù

Why i s t hi s t he onl y record t hat was f ound? The - v opt i on says t osel ect r ecor ds t hat don' t mat ch t he pat t er n. Thi s i s t he samepat t er n as t he pr evi ous exampl e and t heref or e i t sel ect s r ecor dst hat don' t match the pat t ern. The "Adams" r ecor d i s t he onl y onet hat doesn' t make a match. I t doesn' t have a charact er f r om t he set J t hrough and Z.

6. 4. 4 Summary of Regul ar Expr essi on Char act ers

65

Page 66: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 66/193

 ^ Begi nni ng of t he l i ne

$ End of t he l i ne

* 0 or more pr ecedi ng characters

. Any si ngl e char act er

[ . . . ] A r ange of char acters

[ . . . ] Excl usi on r ange of char act er s

6. 5 sed: Edi t a Fi l e t o St andar d Out put

UNI X pr ovi des a met hod of edi t i ng st 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 ext i na f i l e. The sed ut i l i t y edi t s t ext i n a str eam. I n or der t o edi t

a char acter st r eam t wo t hi ngs ar e r equi r ed. Fi r st , t he l i ne t oedi t must be i dent i f i ed ( r egul ar expr essi ons) and second, how t oedi t t he l i ne.

 The f or mal f or m f or t he sed ut i l i t y i s as f ol l ows:

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º ºº Command For mat : 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 ot he standar d out put , edi t ed accor di ng t o a set ( scr i pt ) of commands. The - f opt i ons cause t he scri pt t o be taken f r om f i l e" s f i l e " .

 The gener al f or m i s:

$sed / addr ess/ i nst r ucti on

Not e: I f no addr ess i s speci f i ed, al l l i nes ar e chosen t o edi t .

' sed' addr esses can be l i ne numbers or r egul ar expr essi ons.

Exampl e:

l i ne numbers 2, 42, $ ( $ r epr esent s t he l ast l i ne)

t ext ual addr ess / r egul ar - expr essi on/

66

Page 67: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 67/193

Note: For ward sl ashes encl ose t ext ual addr esses

 The sed i nst r uct i ons i ndi cat e what edi t i ng f unct i on t o per f or m.Here some usef ul sed i nst r uct i ons:

s subst i t ut e

d del et e

Not e: Most sed command l i nes cont ai n spaces or met acharact ers andt hey shoul d be quot ed t o pr ot ect t hem f r om t he shel l . Ther e ar emany more edi t i ng commands pr ovi ded by sed. Here i s a sampl e sedcommand to edi t t he r ecords i n the database f i l e t hat we ar eal r eady f ami l i ar wi t h, namel y, phone. l i s:

Sampl e sessi on:

Ú_______________________________________________________________¿| $sed / s/ Smi t h/ Smythe/ phone. l i s || Smyt he, J oan 7- 7989 |

| Adams, Fr an 2- 3876 || St Cl ai r , Fred 4- 6122 || J ones, Ted 1- 3745 || St ai r , Ri ch 5- 5972 || Benson, 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 or i gi nal f i l e i s not changed.

Her e' s another exampl e of a sed command.

Sampl e sessi 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 , Fred 4- 6123 || J ones, Ted 1- 3745 || St ai r , Ri ch 5- 5972 || Benson, Sam 4- 5587 || $ |À_______________________________________________________________Ù

What does t hi s sed command do? I f you r ead command i n Engl i sh i tr eads 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 endof t he l i ne wi t h a 3. Not i ce t hat t he phone number f orSt Cl ai r , Fr ed 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 range.

 The sed ut i l i t y can al so be use t o del et e par t s of a l i ne of dat a. Thi s i s done by subst i t ut i ng not hi ng f or t he part s you want t odel ete . I t l ooks l i ke thi s :

67

Page 68: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 68/193

Sampl e sessi on:

Ú_______________________________________________________________¿| $sed ' s / . * , / / ' phone. l i s || J oan 7- 7989 || Fr an 2- 3876 || 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 numberof charact er s f ol l owed by a comma wi t h the nul l st r i ng (nothi ng) . Thi s has t he ef f ect of r emovi ng t he t ext .

Here' s a del et e command and how i t ' s used.

Sampl e sessi on:

Ú_______________________________________________________________¿| $sed d phone. l i s || $ |À_______________________________________________________________Ù

Why i s t here no out put ? Wel l , i t r ead st andard i nput and di d t heedi t i ng f uncti on on al l t he sel ected l i nes. Si nce no l i nes wer especi f i ed al l l i nes wer e sel ect ed t o be edi t ed. The edi t i ng was t odel et e t he l i ne.

Quest i on: Has t he ori 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 s appl i edt o each i nput l i ne.

Sampl e sessi on:

Ú_______________________________________________________________¿| $sed ' / St ai r / d || >/ Adams/ d' phone. l i s || Smi t h, J oan 7- 7989 || St Cl ai r , Fred 4- 6122 || J ones, Ted 2- 1136 || Benson, 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 > character i s t he Bour neShel l secondary pr ompt .

6. 6 awk: A Pat t ern Mat chi ng Pr ogr ammi ng Language

68

Page 69: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 69/193

Suppose you want ed t o change t he f ormat of t he dat abase phone. l i st o be the f i r st name f ol l owed by t he l ast name. There i s no easyway t o do t hi s wi t h sed. For t unat el 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 hor s who wr ote i t Al f r ed V. Aho, Peter J . Wei nber ger , and Br i anW. Kerni 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 processi ng l anguage. I twi l l sear ch one or mor e f i l es f or a speci f i ed pat t er n and t henperf orms an act i on, such as wr i t i ng t o st andard out put ori ncr ement i ng a count er when i t f i nds a match. You can use awk t ogener at e r epor t s or f i l t er t ext. I t wor ks equal l y wel l wi t h number sor t ext . The aut hor s desi gned i t t o be easy t o use and sacr i f i cedexecut i on speed t oward t hi s end.

Whi l e the 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 rearr ange, add, or del et e t ext i n a st r eam,easi l y.

 The awk t akes advant age of many const r uct s f r om t he C programmi ngl anguage. I t has t he f ol l owi ng f eat ur es:

f l exi bl e f or matcondi t i onal execut i onl oopi ng st atement 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 andard i nput . The f ol l owi ng i s t he f ormatf or awk:

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º ºº Command f ormat : awk [ - Fc] [ pr og] [ 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 of pat t erns speci f i ed by pr og. Wi t h each pat t ern i n pr og t here can bean associ ated act i on t o be per f ormed when t he l i ne i s f ound. Theset of pat t er ns may appear l i t er al l y as pr og, or i n a f i l e

speci f i ed as - f f i l e. The pr og st r i ng shoul d be encl osed i n si ngl equot es t o pr ot ect i t f r om t he Shel l .

Fi l es are 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 matched agai nst t he pat t er npor t i on of ever y pat t er n- act i on st at ement . The associ at ed act i oni 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 separ at ed by whi t e space. $1, $2. . def i ne t he f i el ds. $0r ef er s t o t he whol e l i ne.

69

Page 70: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 70/193

A pat t ern- act i on st atement has t he f orm:

pat t ern {act i on}

A mi ssi ng act i on means pr i nt t he l i ne; a mi ssi ng pat t ern al waysmakes a mat ch. a st at ement can be one of t he f ol l owi ng:

i f ( condi t i onal ) st at ement [ 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=expr essi onpr i nt [ expr essi on- l i st ] [ >expr essi on]pr i nt f f or mat [ , expr essi on- l i st ] [ >expr essi on]next # ski p r emai ni ng pat t er n on t hi s i nput l i ne

exi t # ski p t he rest of t he i nput

St at ement s ar e t er mi nat ed by semi - col ons, new- l i nes ( CR) , or r i ghtbraces.

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 per f or m 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 express i 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 acti on wi l l be per f or med f orevery l i ne. An act i on can cont ai n sever al commands. There can bemul t i pl e pat t er ns 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 awks commands i s pr i nt . I t put s t he cur r ent l i ne on st andardout put .

Sampl e sessi on:

Ú_______________________________________________________________¿| || $awk ' {pr i nt }' phone. l i s || Smi t h, J oan 7- 7989 || Adams, Fr an 2- 3876 |

70

Page 71: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 71/193

Page 72: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 72/193

i 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 sor t 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 he ent i r el i ne, and or der i ng i s l exi cogr aphi c by byt es i n machi ne col l at i ngsequence.

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º ºº Command f ormat : sor t [ - cmu] [ - oout put ] [ - ykmem] [ - zr ecsz] ºº [ - df i Mnr ] [ - bt x] [ +pos][ - pos2] [ f i l es] ºº ºº See on- l i ne manual f or opt i ons et c. ºº ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼  The easi est 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 sessi on:

Ú_______________________________________________________________¿| || $gr ep ' <t ab>[ 45] ' phone. l i s | sed ' s/ <t ab>/ <t ab>73/ ' | sor t || Benson, Sam 734- 5587 || St Cl ai r , Fred 734- 6122 || St ai r , Ri ch 735- 5972 || $ |À_______________________________________________________________Ù

 The grep command wi l l sel ect onl y t hose r ecor ds t hat have a 4 of a 5 i n t he phone number , t hose r ecor ds are t hen sent t o sed whi chwi l l add "73" j ust af t er t he t ab char act er , t hen t he r ecor ds ar esent t o sor t and put i n al phabet i cal or der . Not i ce t hat t her e i sa pr obl em her e, shoul d St Cl ai r come bef or e St ai r i n an al phabet i call i st i ng? The answer i s NO. Why di d thi s happen? I t occur r ed becauseof t he col l at i ng sequence f or t he def aul t sor t .

 Thi s can be f i xed by speci f yi ng some opt i ons on t he cal l t o t hesor t ut i l i t y. Her e ar e some opt i ons f or sor t . Let ' s see i f we candetermi ne how t o remedy t he pr obl em di scovered i n the def aul t sor t .

Sor t opt i ons:

- f Fol d l ower case i nt o upper case- r Rever se t he sor t f r om hi ghest to l owest

- b I gnore l eadi ng bl ank spaces- d Di ct i onar y sort - i gnor e non al phanumer i c char act er s- m Mer ge t wo sort ed f i l es t oget her- n Sor t t he l i st as number s not di gi t char act er s

Not i ce t he - f opt i ons f ol ds l ower case i nt o upper case. Thi s opt i onwi l l make t he sor t f or our pr obl em wor k cor r ect l y. Her e' s t hesampl e.

Sampl e sessi on:

72

Page 73: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 73/193

 

Ú_______________________________________________________________¿| $gr ep ' <t ab>[ 45] ' phone. l i s| sed ' s/ <t ab>/ <t ab>73/ ' | sor t - f || Benson, Sam 734- 5587 || St ai r , Ri ch 735- 5972 || St Cl ai r , Fred 734- 6122 || $ |À_______________________________________________________________Ù

 The sor t can al so be di r ect ed t o use onl y a port i on of t he l i ne asa sor t 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 br eak each l i ne i nt o f i el ds at whi t espace del i mi t er s. You can use a char act er ot her t han whi t espace by usi ng t he - topt i on. The f i el ds ar e set up l i ke t hi s:

0 1 2/ - - - - | / - - - | / - - - - - - - - - - - - - |

Adams, Fr an 2- 3876

I n order t o sor t by the second f i el d here i s t he sor t commandto enter.

Sampl e sessi on:

Ú_______________________________________________________________¿| $sor t +1 phone. l i s || Adams, Fr an 2- 3876 || St Cl ai r , Fred 4- 6122 || Smi t h, J oan 7- 7989 || St ai r , Ri ch 5- 5972 || Benson, Sam 4- 5587 || J ones, Ted 1- 3745 || $ |À_______________________________________________________________Ù

Her e' s a sampl e of a sor t on t he 3r d f i el d.

Sampl e sessi on:

Ú_______________________________________________________________¿| $sor t +2 phone. l i s || J ones, Ted 1- 3745 || Adams, Fr an 2- 3876 || Benson, Sam 4- 5587 || St Cl ai r , Fred 4- 6122 |

| St ai r , Ri ch 5- 5972 || Smi t h, J oan 7- 7989 || $ |À_______________________________________________________________Ù

A sor t can al so be perf ormed by a charact er posi t i on wi t hi n af i el d. Her e' s t he sampl e.

Sampl e sessi on:

73

Page 74: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 74/193

Ú_______________________________________________________________¿| $sor t +2. 4 phone. l i s || St Cl ai r , Fred 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 || $ |À_______________________________________________________________Ù

Not e: The f i r st char acter of a f i el d i s t he del i mi t er f or t hatf i el d

6. 8 What Ot her Usef ul UNI X Tool s ar e Avai l abl e

As st at ed f r om t he begi nni ng, one of t he maxi ms used t o devel opUNI X was t hat t ool s woul d cont i nue to be devel oped. Here i s al i st of t ool s t hat mi ght be of i nt er est t o you.

Ú_______________________________________________________________¿

| UNI X Tool Descr i pt i on || || comm Compares t wo sor t ed f i l e and r eport s || di f f er ences || || cut Sel ect col umns or f i el ds f r om l i nes i n a || f i el d ( System V onl y) || || di f f Repor t t he di f f er ences bet ween t wo f i l es || || j oi n J oi n l i nes i n t wo f i l es t hat cont ai n a || common f i el d || || pg Show f i l es ( or st andar d i nput ) on a t ermi nal || a scr een at a t i me || || od Pr i nt t he numer i c equi val ent of a f i l e' s || cont ent || || t ai l Li st end of f i l es ( or st andar d i nput ) on || st andard out put || || t ee Sends st andar d i nput t o t wo di f f er ent pl aces|| || t r Tr ansf or ms al l occur r ences of one char act er || i nt o anot her || |

| || wc Count t he char act er s, words, and l i nes i n a || f i l e |À_______________________________________________________________Ù

6. 9 Ar chi ver and Li br ar y Mai nt ai ner

 Thi s command wi l l mai nt ai n groups 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 any

74

Page 75: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 75/193

Page 76: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 76/193

| ar : creat i ng f uncts. a || $ |À_______________________________________________________________Ù

 The name of t he new ar chi ve f i l e i s f unct s. a. The f i l es t hat havebeen added t o t hat archi ve ar e f unct 1. o and f unct 2. o. The f i l epr ot ecti ons f or t he new ar chi ve f i l e ar e r w- r - - r - - .

6. 9. 2 ar : Ver i f yi ng t he Cont ent s of t he Ar chi ve Fi l e

 The key command t o l i st t he t abl e of cont ent s i s t . The t commandwi l l pr i nt a t abl e of cont ent s of t he ar chi ve f i l e. When t he vopt i on i s used wi t h t he t command i t wi l l gi ve a l ong l i st i ng of al l i nf or mat i on about t he f i l es.

Sampl e Sessi on:

Ú_______________________________________________________________¿

| $ar t v f uncts. a || r w- r - - r - - 115/ 200 448 Sep 27 09: 56 1990 f unct 1. o || r w- r - - r - - 115/ 200 448 Sep 27 09: 56 1990 f unct 2. o || $ |À_______________________________________________________________Ù

 Thi s out put shows t hat t here ar e t wo members i n t hi s ar chi ve f i l e,namel y, f unct 1. o and f unct 2. o.

 The prot ect i ons of t hese f i l es i s:

owner - r ead and wr i t egr oup - r eadot her - r ead

 The f i el ds ar e, l ef t t o r i ght , t he f i l e prot ect i ons, owner , group,si ze ( i n bytes) , cr eat i on dat e and t i me, and f i nal l y t he name of t he const i t uent .

6. 9. 3 ar : Removi ng Dupl i cat e Obj ect Fi l es

Once t he archi ve has been created and ver i f i ed, t he obj ect f i l esi n your di r ect ory can be del eted. Thi s can be accompl i shed wi t ht he r m command.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $rm f unct?. o || $ |À_______________________________________________________________Ù

 The quest i on mar k ( ?) i s a wi l dcar d t hat st ands f or any si ngl echar act er . The f i l es f unct 1. o and f unct 2. o no l onger exi st i n yoursubdi r ect or y.

76

Page 77: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 77/193

6. 9. 4 ar : Compi l i ng Mai n and Ar chi ve Fi l es

Now t hat t he obj ect f i l es, f unct 1. o and f unct 2. o, ar e i n t hear chi ve f i l e f uncts. a you, can l i nk them wi t h mai n. o i n t hef ol l owi ng manner .

Sampl e Sessi on:

Ú_______________________________________________________________¿| $cc - o new_hel l o mai n. o f unct s. a || $l s - l a new_hel l o || - r wxr - xr - x 1 t eacher cl ass 17570 Sep 27 12: 58 new_hel l o || $ |À_______________________________________________________________Ù

Wor kshop 6

 Thi s wor kshop wi l l r ei nf or ce your underst andi ng of t he i deaspresent ed i n Chapt er 6. Logi n to t he Mul t i max usi ng t he user nameand password gi ven t o you by t he i nst r uct or. Each st udent i s t ocompl et e t he ent i r e workshop.

DESK EXERCI SES

1. What i s a UNI X process?

2. When a command i s gi ven t o t he Shel l i t wi l l f ork achi l d pr ocess t o execut e t he command.

 Tr ue/ Fal se

3. What i s a pr ocess i dent i f i cat i on number ( PI D) ?

4. What i s t he name of t he Shel l var i abl e t hat cont ai nst he cur r ent PI D?

5. What i s t he UNI X command t o f i nd t he PI Ds associ at edwi t h t he cont r ol l i ng t er mi nal ? What opt i on i s needed t oget det ai l ed i nf or mat i on?

77

Page 78: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 78/193

 

6. What does t he UNI X command gr ep do?

Cont i nue on t he next page7. What do t he f ol l owi ng r egul ar expr essi ons r epr esent ?

Ba

. *

BB*

 J *

[0-9]*$

8. What does the UNI X command sed do?

9. What does t he UNI X command awk do?

10. What does t he UNI X command sor t do?

78

Page 79: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 79/193

11. What i s t he mai n use f or t he UNI X command ar ?

Cont i nue on t he next pageCOMPUTER EXERCI SES

Use t he phone. l i s database f i l e t o answer t he f ol l owi ngquest i ons.

12. " I want t o f i nd al l t he phone numbers t hat begi n wi t h a4 and end wi t h a 2"

13. " I can' t r emember t he name but I bel i eve t he l ast namest art s wi t h an S and t he f i r st name wi t h an F"

14. Fi nd al l t he peopl e wi t h 3 char act er f i r st names.

15. Wr i t e a grep command t hat f i nds al l t he phone number st hat don' t begi n wi t h a 4, 5, or 6.

16. Wr i t e a gr ep command t hat f i nds al l ent r i es begi nni ngwi t h J - Z and endi ng wi t h a 2 or 5.

17. Put a 23 i n f r ont of every phone number . ( Hi nt : sed)

18. Repl ace t he f i r st name wi t h t he per son' s f i r st i ni t i aland a per i od.

79

Page 80: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 80/193

 

Cont i nue on t he next page19. Task: A new phone syst em has been i nst al l ed and peopl e

wi t h phone extensi ons begi nni ng wi t h 4 or 5 now have anew pr ef i x: 73. Cr eat e a f i l e of onl y the peopl e wi t ht he new phone number s.

20. Pr i nt out t he phone l i st showi ng l ast name and f i r stname i n the f ol l owi ng f ormat and sort ed by l ast name.

Fi r st name <t ab> Last name

 That ' s enough, don' t you t hi nk?

7. VAX DCL TO UNI X SHELL SCRI PT CONVERSI ON

 Thi s chapt er wi l l descr i be t he st eps necessar y t o conver t DCLcommand f i l es i nt o Shel l scri pt s. I t i s not a one to one conver si onand many f eat ur es f ound i n one operat i ng syst emare not f ound i nt he ot her . Thi s r equi r es you t o wr i t e shel l scri pt s t hat emul at ef eat ur es of t he ot her. Thi s i s especi al l y t r ue of VAX and UNI X. Thebest way to accompl i sh t hi s i s t o know exact l y what i t i s t hat t hecommand f i l e does i n VMS and t hen wr i t e the equi val ent f unct i on i na shel l scr i pt . Ther e ar e f ew f eat ur es t hat ar e t he si mi l ar andt hose wi l l be exami ned.

 There i s r eal l y no "best " way t o appr oach t hi s subj ect . VMS andUNI X are both uni que operat i ng syst ems. Much of t he mat er i alcover ed i n t hi s cour se up t o t hi s poi nt wi l l be used i n t heconver si on pr ocess. We wi l l st ar t wi t h a l i st of st andar d UNI Xt ool s and t hei r VMS equi val ent s. Thi s wi l l gi ve a you a f l avor of t he ki nds of t ool s each oper at i ng syst em has t o of f er .Ú_______________________________________________________________¿| Maj or Tool VMS UNI X |

Ã_______________________________________________________________´| || Edi t ors EDT ed || TECO ex || TPU vi || || Communi cat i ons MAI L mai l || REPLY wr i t e || PHONE t al k || DECnet f t p |

80

Page 81: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 81/193

| tel net || || Compi l er s FORTRAN f 77 || cc || || Text Processi ng RUNOFF t r of f || nrof f || awk || l ex || || sed || || SORT sor t || MERGE mer ge || || Progr amDevel opment LI NK l i nk || Tool s DEBUG adb/ dbx || LI BRARI AN ar / r anl i b || DEC MMS make || yacc |

| DEC CMS scs || || Mi scel l aneous DECal c bc/ dc || DECspel l spel l || |À_______________________________________________________________Ù

As you can see f r om t he above l i st s, both syst ems of f er edi t or scapabl e of screen or l i ne edi t i ng capabi l i t i es. Bot h havei nt er acti ve communi cat i ons, el ectr oni c mai l , net wor ki ng, f i l et r ansf er , r emot e command execut i on, and remot e l ogi ns. Theseut i l i t i es and t ool s ar e st andar d t o UNI X but r equi r e a l i cense t or un under VMS. The Di gi t al Command Language (DCL) and t he UNI XShel l ( Bour ne, C, or Kor n) ar e command i nt er pr et er s. That i s, t heyare bot h pr ogr ams t hat par se t he command l i ne and t hen pass cont r olt o ot her pr ogr ams t hat are the kernel of t he operat i ng syst em.7. 1 Processes

When you l ogi n to ei t her syst em t he oper at i ng syst em wi l l creat ea uni que pr ocess . Thi s pr ocess i s gi ven access t o memory and CPUr esour ces. The di f f erences between the t wo operat i ng syst ems wi t hr espect t o mul t i - t aski ng needs expl anat i on. Mul t i - t aski ng i sconcur r ent pr ocesses i ni t i ated by a si ngl e user . When t he userst ar t s a t er mi nal sessi on t he syst em i ni t i at es a si ngl e pr ocesscal l ed t he PARENT pr ocess. I t i s possi bl e to st ar t mul t i pl epr ocesses f r om t he par ent pr ocess. Thi s i s cal l ed spawni ng i n VMSand f orki ng i n UNI X. The new pr ocess i s cal l ed a sub- pr ocess i n

VMS and a chi l d pr ocess i n UNI X. Thi s i dea of "f orki ng" a chi l dpr ocess occur s f r equent l y i n UNI X.

When UNI X cr eat es a chi l d pr ocess or VMS creates a subprocessdi f f er ent t hi ngs occur . Fi r st , l et ' s l ook at VMS. When t hesubpr ocess i s spawned t he parent goes t o "sl eep" unt i l t he userl ogs of f f r om t he subpr ocess. When t he l ogof f occur s cont r olr et urns t o t he par ent . The VMS ATTACH command gi ves cont r ol backt o t he parent process and t he subprocess goes t o sl eep. The poi nti s t hat onl y one pr ocess i s act i ve at a ti me. The except i on i s t he

81

Page 82: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 82/193

VMS RUN/ PROCESS=name whi ch wi l l r un a user - def i ned process at t hesame t i me commands can be i ssued at t he parent process .

I n UNI X, you can r un parent and chi l dr en pr ocesses at t he samet i me. A chi l d pr ocess can f ork another pr ocess and t hus a pr ocesscan be bot h a par ent and chi l d. Chi l d pr ocesses ar e not r est r i ct edt o user - def i ned i mages but can be any val i d UNI X operat i on. UNI Xpr ocesses t hat ar e runni ng or st opped but not get t i ng i nput f r oma t ermi nal are sai d to be i n background. When you begi n a UNI Xsessi on t he kernel gi ves you a copy of t he shel l . When you ent era command t he Shel l f orks a chi l d. That chi l d t hen pr ocesses t hecommand you ent er ed.

Note: some commands ar e executed by t he Shel l i t sel f and no chi l di s f or ked.

 Thi s i s a di f f er ent concept f r om VMS, i n whi ch al l commands ar eexecut ed by t he parent process . Once a subprocess i s cr eat ed t hepar ent r emai ns dor mant unt i l t he subpr ocess compl etes.

Al l t he above parent and chi l d pr ocesses use t he st andard def aul tf or i nput and out put devi ces, t he t er mi nal . I nput and out putst r eams i n UNI X ar e cal l ed st andar d i nput ( st di n) and st andar dout put ( st dout ) . St andar d er r or ( st der r ) al so uses t he termi nal asi t ' s def aul t out put . I n or der t o r edi r ect t hese st r eams f r om at er mi nal i n VMS i t i s r equi r es t he assi gnment of t he l ogi cal namesSYS$OUTPUT, SYS$I NPUT, and SYS$ERROR t o a f i l e or devi ce. UNI X hasa much ni cer means of r edi r ect i ng the i nput or out put .

7. 2 Pi pes

A ver t i cal bar ( | ) i s used t o redi r ect t he out put of a command t ot he i nput of another command. Thi s i s t he power of UNI X. Forexampl e, we want t o get a l i st of al l cur r ent l y act i ve user s,sor t ed i n al phabet i cal or der , and sent t o the pr i nt er , how coul dt hat be done i n VMS? Here' s one sol ut i on:

VMS Sampl e Sess i on:

Ú_______________________________________________________________¿| $SHOW USERS/ OUTPUT=A. TEMP || $SORT/ KEY=POSI TI ON: 40, SI ZE: 6) - || A. TEMP SYS$PRI NT |À_______________________________________________________________Ù

Not i ce t he need f or an i nt er medi at e f i l e cal l ed A. TEMP.

Now how woul d thi s same requi r ement be met usi ng UNI X. Ther e i s acommand t hat wi l l l i st t he users t hat are cur r ent l y l ogged on t ot he syst em. The command i s who. Pi pes al l ow t he out put of onecommand t o be t he i nput i nto anot her command. The output of whocan be put i nt o anot her UNI X command sort by a pi pe. I n a si mi l armanner , t he out put of sor t can be r edi r ect ed t o anot her commandcal l ed l p. Thus t he same pr obl emcan be sol ved usi ng UNI X Shel l i nt hi s way:

UNI X Bour ne Shel l Sampl e Sessi on:

82

Page 83: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 83/193

Page 84: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 84/193

| || < Redi r ect st andar d i nput ( st di n) || || | Redi r ect st andar d out put ( st dout ) t o anot her || command || |À_______________________________________________________________Ù

 These ar e t he most commonl y used r edi r ect i on met achar act er s.

Not es:

UNI X r edi r ect i on onl y af f ect s t he command l i ne on whi ch ther edi r ect i on char act er occur s.

Er r or messages ar e not r edi r ected and wi l l appear on t het er mi nal .

I f a f i l e al r eady exi st s, VMS wi l l creat e a new ver si on of t he f i l e wi t h a hi gher ver si on number . By def aul t , UNI X wi l l

over wr i t e t he exi st i ng f i l e.7. 4 Command St r uct ure and Fi l e Nami ng Convent i ons

VMS i s not case- sensi t i ve i n i t s i nt er pr et at i on of commands. I tdoesn' t di st i ngui sh between upper and l ower case charact ers. Thi si s not t r ue i n UNI X, however . Commands must be ent ered i n l owercase char act er s onl y. The Shel l wi l l not under st and char act er s t hatare i n upper case.

Fi l enames ar e al so case sensi t i ve i n UNI X. The f i l e namedMYFI LE. DAT and t he f i l e myf i l e. dat r ef er t o di f f er ent f i l es. Oneadvant age of t hi s i s t hat you can have a much l arger var i ety of f i l enames wi t h f ewer char act er s. Especi al l y good i f you don' t l i ket o t ype.

 Thi s al so has advant age over t he f i l ename convent i ons used by VMS.Di r ectori es and subdi r ectori es ar e poi nt er f i l es i n bot h oper at i ngsyst ems. When a subdi r ect ory i s cr eat ed i n VMS i t i s gi ven t heext ensi on name . DI R aut omat i cal l y. UNI X f i l es on the ot her hand donot di st i ngui sh bet ween ordi nary f i l es and di r ect or i es. Many VMSuser s have adopt ed a pr act i ce of nami ng new subdi r ector i es i n UNI Xwi t h al l capi t al l et t ers or t he f i rst l et t er bei ng capi t al i zed. Thi s i s not , however , st andar d UNI X pract i ce.

VMS Sampl e Sess i on:

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. $CREATE/ DI RECTORY [ . TEST] .

. $DI RECTORY .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

UNI X Shel l Sampl e Sessi on:

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. $mkdi r Test .

. $l s Test .

84

Page 85: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 85/193

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

I t i s possi bl e i n UNI X t o mai nt ai n t he same f i l ename convent i onst hat you used i n VMS. The per i od ( . ) i s a l egal char act er i n a UNI Xf i l ename. Some VMS user s l i ke t o cont i nue t he pr act i ce of nami ngf i l es usi ng t he same . ext ensi ons f r om VMS. Probl ems occur whendef aul t ext ensi ons are di f f erent between t he two syst ems. Forexampl e, obj ect f i l es use t he extensi on . OBJ i n VMS but . o i n UNI X.Anot her exampl e i s FORTRAN source code i n UNI X t he extensi on i s . f and VMS uses . FOR. Note t hat . o and . f are UNI X convent i ons t of aci l i t ate f i l e recogni t i on and t hat UNI X commands do not assumef i l e extensi ons as VMS does.Here i s a l i st of commonl y used ext ensi ons f or bot h operat i ngsyst ems.

Ú_______________________________________________________________¿| VMS UNI X Def i ni t i on || |Ã_______________________________________________________________´| . OLB . a Li br ar y |

| || . BAS . bas BASI C Source Code || || . C . c C Sour ce Code || || . FOR . f FORTRAN Sour ce Code || . h C header f i l es || . l l ex program || || . OBJ . o Obj ect Code || || . PAS . p PASCAL Source Code || . s Symbol i c Assembl y Code || . y yacc pr ogr am || || . EXE a. out Executabl e I mage || || . ADA ADA Source Code || . B32 BLI SS- 32 Source Code || . CLD Command descr i pt i on f i l e || . COB Cobol Source Code || . COM Commands f or t he l anguage i nt erpr et er || . DAT Dat a f i l e || . DI S Di st r i but i on l i s t f i l e f or MAI L || . DI R Di rect ory f i l e || . EDT St art up command f i l e f or EDT edi t or || . DOC Document at i on |

| . HLP I nput sour ce f i l es f or HELP Li br ar y || . J OU J our nal f i l e creat ed by EDT || . LI S Li s t i ng of text || . LOG Bat ch j ob out put f i l e || . MAI MAI L message f i l e || . MAR VAX Macr o source code || . SYS Syst em I mage || . TMP Temporary f i l e |À_______________________________________________________________Ù7. 5 Fi l e Management Commands

85

Page 86: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 86/193

  Thi s i s a t abl e of t he mor e commonl y used f i l e management commands.Some of t hese commands have been covered el sewher e i n t hi s manual . The on- l i ne manual command ( man) can be used t o get det ai l edi nf or mat i on about any UNI X command.

UNI X VMS Pur poseCommand equi val ent

ar LI BRARY Ar chi ve f i l es

awk TPU Pat t ern matchi ng ut i l i t y

cat TYPE/ APPEND Cat enat es and pr i nt s t ot ermi nal

cd SET DEFAULT Change wor ki ng di r ect or y

chgrp SET FI LE Change group owner shi p

chmod SET PROTECTI ON Changes prot ect i on

cmp DI FFERENCE Compares t wo f i l es and r epor t sf i r st di f f er ence f ound

cp COPY Cr eat e a new copy

f i nd DI RECTORY Locates wi t hi n a di r ect orystructure

f t p COPY Transf er t o/ f r omr emote node

grep SEARCH Fi nds a st r i ng

l n ASSI GN Cr eat e a symbol i c l i nk

l s DI RECTORY Li st cont ent s of a di r ect or y

mer ge MERGE Mer ge f i l es

mkdi r CREATE/ DI R Make a di r ect ory

mv RENAME Moves or r enames f i l es

od DUMP Oct al , deci mal , hex, ASCI Idump

pr PRI NT/ HEAD Pr i nt s f i l es

pwd SHOW DEFAULT Wor ki ng di r ect or y name

r m DELETE Removes or del etes f i l es

r mdi r DELETE Removes a di r ect ory f i l e

sor t SORT Sor t s by a key

86

Page 87: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 87/193

t ai l EDI T/ READ Out put s l ast par t of f i l e

t ar BACKUP Tape ar chi ve

t ouch CREATE Updates f i l e char act er i st i csor c reates nul l f i l e

t r EDI T Tr ansl at es char act er s

7. 6 Metacharact ers

Charact ers t hat have speci al meani ngs t o the Shel l are known asmetacharact ers . User s shoul d avoi d usi ng t hese charact ers i nf i l enames as r esul t s mi ght be unpr edi ct abl e. We have al r eady seensever al met achar act er s, f or exampl e, ver t i cal bar ( | ) , or t hegr eat er t han ( >) or l ess t han (<) . The f unct i on of met acharacterscan be di f f erent dependi ng on whet her t he Shel l or a UNI X t ooli nt er pr et s t he char acter. The f ol l owi ng i s a l i st of UNI X speci alcharact ers and thei r VMS equi val ent .

UNI X Funct i on VMS equi val entChar

& Per f or m command i n Backgr ound= Ass i gnment oper at or =; Command separ at or\ Cont i nuat i on of command l i ne -\ m Li t er al t r ansl at i on of met achar acter m "m' Tur n of f speci al meani ng "` Process i mmedi at el y @ & r un" Gr oup charact ers i nt o a si ngl e argument "# Comment f ol l ows !* Wi l dcar d f i l ename subst i t ut i on *? Wi l dcar d f i l ename subst i t ut i on si ngl e char act er %$ Ar gument subst i t ut i on f ol l ows '$# Ar gument count$$ Process i d F$GETJ PI ( "PI D")$? Exi t st atus $STATUS$< Read one l i ne f r om st andard i nput I NQUI RE, READ. Cur r ent di r ectory [ ][ ] Sel ecti ve f i l ename subst i t ut i on

Note: Metacharact ers uni que t o t he C- Shel l have not been i ncl udedi n t hi s l i st t o r educe conf usi on. Check C- Shel l document at i on f ora compl et e l i st .

7. 7 Wi l dcards: Ar e They Real l y Wi l d?

UNI X wi l dcards extend the f eat ur es f ound wi t h wi l dcar ds i n VMS. The f ol l owi ng t abl e shows how UNI X expands wi l dcar d def i ni t i ons:

Ú_______________________________________________________________¿| UNI X exampl e Meani ng |Ã_______________________________________________________________´| || * # Al l f i l es i n t he cur r ent di r ectory and one || l evel bel ow |

87

Page 88: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 88/193

| || . # Fi l es i n t he cur r ent di r ector y || || || *. * # Al l f i l es t hat cont ai n a per i od i n t he || f i l ename || || *. com # Al l f i l es i n t he cur r ent di r ector y t hat || end i n . com || || ?. com # Al l f i l es i n t he cur r ent di r ector y t hat || end i n . com and have one charact er || pr ecedi ng t he peri od || || name[ xyz] # Al l f i l es i n t he cur r ent di r ect or y, namex, || namey, or namez || || name[a- z] # Al l f i l es i n t he cur r ent di r ect or y namea || t hrough namez || |

| name[ a- z4] # Al l f i l es i n t he cur r ent di r ect or y, namea || t hr ough namez and name4 || |À_______________________________________________________________Ù

 There ar e no absol ut e r ul es concer ni ng t he use of wi l dcar ds. Theout put produced by t he wi l dcar d expansi on process i s commanddependent . For i nst ance, t he l s * command wi l l l i st f i l es i n t hecur r ent di r ect or y and t he f i l es i n t he di r ect or y i n t he next l owerl evel as wel l . The wc * ( word count ) command wi l l pr oduce out putf or t he f i l es i n t he cur r ent di r ect or y onl y. Thus as you can seecommands var y i n how wi l dcar ds ar e expanded.

User s and pr ogr ammers can use wi l dcar ds i n a si mi l ar way t o t hemet hod of usi ng wi l dcar ds on t he VAX. UNI X wi l l i nt er pr et *. com t omean any f i l e i n t he cur r ent di r ect or y that ends i n . com event hough t he ext ensi on has no meani ng i n UNI X.7. 8 Summar y

 The VMS and UNI X operat i ng syst ems ar e si mi l ar i n some r espect s. The VMS user must r ecogni ze t hat t here ar e some f undamentaldi f f er ences.

UNI X al l ows mul t i pl e pr ocesses and t he user must l ear n t o managet hese pr ocesses. VMS usual l y manages a si ngl e pr ocess whi chprocesses commands i n sequence.

UNI X has a di f f erent command synt ax. UNI X commands ar e al r eadyshort and cannot be abbrevi at ed l i ke VMS commands. UNI X commandsdo not l end t hemsel ves easi l y t o descr i be t hei r f unct i on. Forexampl e, TYPE seems t o descr i be the f unct i on bet t er t han cat i nUNI X. Si ngl e l et t er opt i ons modi f y UNI X commands i n a mannersi mi l ar t o VMS command qual i f i ers.

UNI X has a di f f er ent f i l e and di r ect or y st r uct ur e. You can addr essany f i l e i r r espect i ve of t he physi cal devi ce usi ng t he absol ut e orr el at i ve pat hname.

88

Page 89: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 89/193

 UNI X has metacharact ers whi ch have speci al f unct i ons wheni nt er pr et ed by the Shel l .

NOTESÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜWor kshop 7

 Thi s wor kshop wi l l r ei nf or ce your underst andi ng of t he i deaspr esent ed i n al l t he pr evi ous chapt er s. Logi n t o the Mul t i max usi ngt he user name and password gi ven t o you by t he i nst r uct or . Eachst udent i s t o compl ete the ent i r e workshop.

DESK EXERCI SES

1. Conver t t he DCL command f i l e f ound on t he next paget o Bour neShel l and r un i t .

Hel pf ul ?? Hi nt s:

What does t he VMS DCL command f i l e do?

Does UNI X Shel l have a si mi l ar f unct i on?

What does t he UNI X command cut do?

What does t he UNI X command dat e do?

Can t hi s scr i pt be conver t ed one l i ne at a t i me?

Cont i nue on t he next page

$ Today = F$cvt i me( " t oday", , "weekday")$ i f t oday . eqs. "Monday" t hen got o monday$ i f t oday . eqs. "Tuesday" t hen goto tuesday$ i f t oday . eqs. "Wednesday" t hen got o wednesday$ i f t oday . eqs. "Thur sday" t hen got o t hur sday$ i f t oday . eqs. "Fr i day" t hen got o f r i day$ go t o weekend$ Monday:$ Wr i t e sys$out put " "$ Wr i t e sys$out put "Today i s ' ' t oday' and t here are 5 days

89

Page 90: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 90/193

unt i l t hi s weekend"$ Wr i t e sys$out put " "$ exi t$ Tuesday:$ Wr i t e sys$out put " "$ Wr i t e sys$out put "Today i s ' ' t oday' and t here are 4 daysunt i l t hi s weekend"

$ Wr i t e sys$out put " "$ exi t$ Wednesday:$ Wr i t e sys$out put " "$ Wr i t e sys$out put "Today i s ' ' t oday' and t here are 3 daysunt i l t hi s weekend"

$ Wr i t e sys$out put " "$ exi t$ Thursday:$ Wr i t e sys$out put " "$ Wr i t e sys$out put "Today i s ' ' t oday' and t here are 2 daysunt i l t hi s weekend"

$ Wr i t e sys$out put " "

$ exi t$ Fr i day:$ Wr i t e sys$out put " "$ Wr i t e sys$out put "Today i s ' ' t oday' and t her e i s 1 dayunt i l t hi s weekend"

$ Wr i t e sys$out put " "$ exi t$ Weekend:$ Wr i t e sys$out put " "$ Wr i t e sys$out put "Today i s ' ' t oday' and why are you worki ngon a weekend"

$ Wr i t e sys$out put " "$ exi t

Cont i nue on t he next page2. Not al l f unct i ons, espec i al l y cal l s t o l i brary

f unct i ons, t hat exi st i n VMS have an equi val ent cal l i nUNI X Shel l . An exampl e of t hi s i s F$ELEMENT i n VMS.Wr i t e a Bour neShel l scr i pt t hat wi l l do the same j ob asF$ELEMENT and t est i t .

I f t i me remai ns and you want a f ur t her chal l enge:

90

Page 91: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 91/193

 

3. Wr i t e a user i nt er f ace t o add and del et e r ecor ds to t hephone. l i s dat abase. Quer y t he user i n a user f r i endl ymanner and check to see i f t he name i s al r eady i n thedat abase pr i or t o addi ng i t .

 That ' s al l f or t hi s wor kshop. I f you di dn' t f i ni shbecause of t i me, t hat ' s OK, as l ong as you underst andt he pr ocess needed t o do conver si ons.

NOTES

ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ8. ADVANCED FEATURES OF FTP

 Thi s chapt er wi l l di scuss some advanced f eat ures of t he FTP ser veras i mpl ement ed on t he Mul t i max. The i nt r oduct i on of FTP i n UNI Xf or Begi nni ng User s gave an el ement ary i nt r oduct i on t o some of t hef eat ur es. I f you ar e not f ami l i ar wi t h t he basi cs, pl ease r ef ert o t hat manual . I t i s not t he pur pose t o r evi ew t hose basi cs her e.

 The FTP ( I nt er net f i l e t r ansf er program) i s t he user i nt er f ace t ot he DARPA Fi l e Tr ansf er Pr ot ocol . Thi s ut i l i t y pr ogr am wi l lt r ansf er f i l es to and f r om a r emot e comput er . I n or der f or f i l est o be t r ansf err ed f r omt he l ocal comput er t o a r emote comput er, aconnect i on must be est abl i shed. Thi s can be done f r om t he FTPcommand l i ne. The connect i on t o t he r emot e comput er wi l l r emai nacti ve unt i l i t i s t er mi nat ed by the user .

 The r emot e computer wi t h whom t he connect i on i s t o be made can bespeci f i ed on t he FTP command. I n t hi s case, FTP wi l l i mmedi atel yt r y t o est abl i sh a connect i on. I f t he r emot e comput er i s notspeci f i ed, FTP wi l l ent er i t s command i nt erpr eter mode and wai tf or i nst r uct i ons; a pr ompt wi l l be di spl ayed.

FTP does have a hel p f eat ure, and al l 58 commands can be l i st ed.I t wi l l al so gi ve a t er se descr i pt i on of each command. I naddi t i on, t here ar e on- l i ne manual pages whi ch can be accessed by

usi ng t he man command i n UMAX.8. 1 I ni t i al i zi ng FTP on UMAX

 The t er m, " l ocal comput er , " wi l l r ef er t o t he Mul t i max. The" r emote comput er" wi l l r ef er t o t he ot her comput er wi t h whi ch youar e t r yi ng t o send/ r ecei ve f i l es. For pur poses of t hi s cour se, wewi l l be r ef err i ng t o t he VAX mi ni comput er as t he remot e comput er .Pl ease be aware t hat t hese pr ocedures wi l l work f or any comput erconnect ed t o Et hernet and havi ng an FTP ser ver .

91

Page 92: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 92/193

 FTP can be i nvoked on the l ocal comput er usi ng the f ol l owi ngsyntax:

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command For mat : f t p [ - v] [ - d] [ - i ] [ - n] [ - g] [ host ] ºº ºº - v = ver bose on, f or ces f t p t o show al l r esponses ºº f r om t he r emot e server ºº ºº - d = enabl es debuggi ng ºº ºº - i = t ur n of f i nt er act i ve pr ompt i ng dur i ng ºº mul t i pl e f i l e t ransfers . ºº ºº - n = di sabl es t he "aut o- l ogi n" f eat ur e ºº ºº - g = di sabl e f i l ename gl obbi ng ºº ºº host = t he name of t he r emot e computer º

ÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼ NOTE: UMAX ( UNI X) i s case sensi t i ve. The commands and opt i ons

must be ent er ed as shown.8. 2 Mul t i pl e Fi l e Tr ansf er s

 The synt ax f or t he mul t i pl e get command i s:

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command For mat : mget r emot e- f i l es ºº ºº r emot e- f i l es = r emot e comput er wi l dcar d speci f i cat i on ºº or ºº f i l e1 f i l e2 . . . f i l en ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼  The r emot e computer wi l dcar d speci f i cat i on i s expanded i n a processcal l ed gl obbi ng. Once t he gl obbi ng i s compl et e, a get i s per f ormedon each f i l ename; and i t i s t r ansf er r ed t o t he l ocal comput er . The f i l ename i s t he same on bot h comput er s. You can speci f y t hef i l enames t o be t r ansf er r ed separ ati ng t hem wi t h spaces.

Exampl e:. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. f t p>mget *. dat; * .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

 Thi s command wi l l t r ansf er al l ver si ons of t he r emot e- f i l es t hathave t he f i l ename extensi on of . dat . I f t he opt i on - i wasspeci f i ed on t he cal l t o FTP, t hen t he f i l es wi l l be t r ansf er r edaut omat i cal l y. I f t he opt i on was not speci f i ed, FTP wi l l pr omptyou bef or e t r ansf er r i ng each f i l e.

Sampl e Sessi on:

92

Page 93: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 93/193

Ú_______________________________________________________________¿| f t p>mget *. dat || mget change_pass. dat ; 1? |À_______________________________________________________________Ù

 The def aul t i s ' yes' , pressi ng ( Ret ) wi l l cause t he f i l e t o be sentt o t he l ocal di r ector y. I f you don' t want t hi s f i l e t r ansf er r ed,ent er n( Ret ) ; you wi l l t hen be pr ompt ed f or t he next f i l e, i f oneexi sts.8. 3 Aut o Logi n Featur e

I t i s possi bl e t o have the l ogi n pr ocedur e occur aut omat i cal l y. To do t hi s r equi r es a f i l e i n your home di r ect or y cal l ed . net r c. The . net r c f i l e cont ai ns l ogi n and i ni t i al i zat i on i nf or mat i on t obe used by t he aut o- l ogi n pr ocess. The f ol l owi ng var i abl es ar eused and can be separated by spaces, t abs, or new l i nes.

machi ne name

 Thi s i s t he name of t he r emot e comput er . The aut o- l ogi n processwi l l sear ch t he . net r c f i l e f or a machi ne var i abl e that mat chest he name of t he r emot e comput er on t he f t p command or as an opencommand argument . Once a mat ch i s f ound, t he next var i abl es areal so pr ocessed unt i l t he end of f i l e or anot her machi ne var i abl ei s encount ered.

l ogi n name

 Thi s i s t he user name on t he r emot e syst em. I f t hi s var i abl e i spr esent , t he aut o- l ogi n pr ocess wi l l l ogi n t o the r emote comput erwi t h t he gi ven username.

password st r i ng

 Thi s i s t he passwor d t o be used when l oggi ng i n t o t he r emot esyst em.

NOTE: I f t hi s var i abl e i s pr esent i n t he . net r c f i l e, f t p wi l labor t t he aut o- l ogi n pr ocess i f t he . net r c f i l e i sr eadabl e by anyone but t he user .

account st r i ng

 Thi s suppl i es an addi t i onal account passwor d. I f present , t heaut o- l ogi n pr ocess wi l l suppl y t he str i ng as an addi t i onal passwor di f r equi r ed by t he remot e ser ver .

macdef name

 Thi s def i nes a macr o. Thi s var i abl e wi l l f unct i on l i ke t he f t p

93

Page 94: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 94/193

macdef command. A macro i s def i ned wi t h t he speci f i ed name, i t scont ent s begi n wi t h t he next . net r c l i ne and cont i nue unt i l a nul ll i ne ( 2 new l i ne char act er s) . I f a macro named i ni t i s def i ned,i t wi l l be execut ed as t he l ast st ep of t he aut o- l ogi n pr ocess.Sampl e Sessi on:

Ú_______________________________________________________________¿| $cat . net r c || machi ne er c830 || l ogi n t eacher || password secret1 || machi ne er c780 || l ogi n r har di ng || password secret2 || $ |À_______________________________________________________________Ù

 To i nvoke t he aut o- l ogi n f eat ure, t ype t he f t p command and entert he name of t he remot e comput er as an argument .

Sampl e Sessi on:

Ú_______________________________________________________________¿| $f t p er c830 || Connect ed t o erc830. || 220 erc830 Wol l ongong FTP Ser ver ( Ver 5. 0) at Tue Oct 23 || 331 Passwor d r equi r ed f or r har di ng. || 230 User l ogged i n, def aul t di r ectory D_1131: [ RHARDI NG] || f t p> |À_______________________________________________________________Ù

I f t he . net r c f i l e i s r eadabl e by anyone ot her t han t he user , t hef ol l owi ng er r or message wi l l appear; and t he connect i on wi l l notbe made t o t he r emot e comput er .

Sampl e Sessi on:

Ú_______________________________________________________________¿| $l s - l . net r c || $f t p er c830 || Connect ed t o erc830. || 220 erc830 Wol l ongong FTP Ser ver ( Ver 5. 0) at Tue Oct 23 || Er r or - . net r c f i l e not cor r ect mode. || Remove password or cor r ect code. || 221 Goodbye. || f t p> |À_______________________________________________________________Ù

 To cor r ect t hi s er r or , change t he mode of t he . net r c f i l e so i t i snot r eadabl e by ot her users or r emove t he password f r om t he f i l e. Thi s i s t o prevent your passwor d f r om bei ng r ead by an unaut hor i zeduser.8. 4 Macros

Macros ar e a si ngl e i nst r uct i on t hat a pr ogr am r epl aces by several ,usual l y, more compl ex i nst r uct i ons. The f t p command t o cr eat e a

94

Page 95: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 95/193

macro def i ni t i on i s:

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command For mat : macdef macr o- name ºº ºº macr o- name - t he name of t he macr o ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼ Af t er t he macdef command i s gi ven, al l subsequent l i nes ar e st oredas a macro wi t h t he name macro_def . Consecut i ve newl i ne char act ersor car r i age r etur ns t ermi nate t he i nput mode i nt o t he macr o. Therei s a l i mi t of 16 def i ned macros and a l i mi t of 4096 char act er s i nal l def i ned macr os.

Sampl e Sessi on:

Ú_______________________________________________________________¿| f t p>macdef get _f i l es || open erc780 || get f i l e_1 |

| put r esul t _2 || cl ose || f t p> |À_______________________________________________________________Ù

I n t hi s exampl e, t he f our l i nes of t he macr o can be execut ed si mpl ybe ent er i ng get _f i l es at t he f t p pr ompt . The macro wi l l onl y exi stunt i l t he cur r ent f t p sessi on i s cl osed.8. 5 Fi l ename Transl at i on

Fi l ename convent i ons di f f er f r omone comput er t o another, and FTPwi l l al l ow you t o t r ansl at e t he name as i t i s tr ansf er r ed. Oneway i s t o speci f y t he name of t he f i l e as i t i s t o exi st on t hel ocal comput er . Thi s i s done by t he argument on t he put or getcommand.

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command For mat : put l ocal - f i l e [ r emot e- f i l e] ºº ºº get remote- f i l e [ l ocal - f i l e] ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼ 

I f you don' t speci f y the remot e- f i l e ( f or t he put command) or t hel ocal - f i l e ( f or t he get command) , t he name wi l l be the same on botht he l ocal and r emote comput er. Thi s can cause a pr obl emi f you are

not aware of i t . There i s an FTP command t hat wi l l al l owt he name t o be t r ansl ated aut omat i cal l y.

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command Format : nmap [ i npat t ern out pat t ern] ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼ 

I f no ar gument s are suppl i ed, i t wi l l set or unset t he f i l enamemappi ng mechani sm. I f argument s are speci f i ed, r emote f i l enames

95

Page 96: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 96/193

are mapped dur i ng mput ( mul t i pl e f i l e put s) and put ( si ngl e f i l e)commands t hat are i ssued wi t hout a speci f i ed r emot e f i l ename.

I f argument s ar e speci f i ed, l ocal f i l enames are mapped dur i ng mget( mul t i pl e f i l e get s) and get ( si ngl e f i l e) commands t hat ar e i ssuedwi t hout a speci f i ed l ocal f i l ename.

 The mappi ng f ol l ows t he pat t er n set by i npat t er n and out pat t er n.Vari abl e templ at i ng i s done by i ncl udi ng t he sequences " $1" ,"$2", . . . . "$9 "i n i npat t er n. Al l ot her char acter s ar e t r eat edl i t er al l y and are used t o det ermi ne the nmap i npatt er n var i abl eval ues.

For exampl e, say t he i npat t ern was $1. $2 and t he r emot e f i l enamei s mydat a. dat a, $1 woul d have the val ue mydat a and $2 woul d havet he val ue data. The out pat t ern det ermi nes t he r esul t i ng mappedf i l ename. The sequences "$1", "$2", . . . "$9", ar e r epl aced by t heval ue r esul t i ng f r om t he i npat t er n t empl at e. "$0" i s repl aced byt he or i gi nal f i l ename.8. 6 Abor t i ng Transf er s

Pr ess the t er mi nal i nt er r upt key ( usual l y Ct r l - C) t o abor t a f i l et r ansf er . The sendi ng t r ansf er wi l l st op i mmedi at el y. Recei vi ngt r ansf ers wi l l be hal t ed by FTP sendi ng an ABOR command to t her emot e ser ver and di scar di ng any f ur t her dat a t hat i s r ecei ved.

I f t he r emot e server doesn' t suppor t t he ABOR prot ocol command t hef t p> pr ompt wi l l not appear unt i l t he request ed f i l e has been sent .8. 7 Mor e Remot e Comput er Commands

 These commands can be usef ul when wor ki ng wi t h t he di r ect or i es ont he r emot e comput er .

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command For mat : cdup ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼  Thi s FTP command wi l l change t he r emot e machi ne cur r ent wor ki ngdi r ect or y t o the par ent of t he cur r ent wor ki ng di r ect or y.

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command Format : del et e r emot e- f i l e ºº ºº r emot e- f i l e name of t he f i l e t o del et e º

ÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼  Thi s FTP command wi l l del et e t he speci f i ed f i l e.

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command Format : mdel et e [ r emot e- f i l es] ºº ºº r emot e- f i l es names of t he f i l es t o del et e ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼

  96

Page 97: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 97/193

  Thi s FTP command act s as a mul t i pl e del et e. I t wi l l del et e al lt he speci f i ed f i l es.

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command For mat : mkdi r di r ect or y- name ºº ºº di r ect ory- name t he name of t he di r ectory t o be cr eat ed ºº on t he r emot e comput er . ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼  Thi s FTP command wi l l cr eat e a di r ect or y on t he r emot e comput er .

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command For mat : r mdi r di r ect or y- name ºº ºº di r ectory- name t he name of t he di r ect ory on t he r emote ºº comput er t hat wi l l be r emoved. º

ÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼  Thi s FTP command wi l l r emove t he speci f i ed di r ect or y.

NOTE: Thi s command wi l l not wor k wi t h some r emot e ser ver s.NOTES

ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜWor kshop 8

 Thi s wor kshop wi l l r ei nf or ce your underst andi ng of t he i deaspresent ed i n Chapt er 9. Logi n to t he Mul t i max usi ng t he user nameand password gi ven t o you by t he i nst r uct or. Each st udent i s t ocompl et e t he ent i r e workshop.

DESK EXERCI SES ( 10 mi nutes)

1. What FTP command i s used t o t r ansf er more t han one f i l eat a t i me? What FTP command wi l l gi ve a pr ompt t o youbef or e each f i l e i s r et r i eved? Suggest i on: t her e ar e twoways

2. What i s t he name of t he f i l e where t he aut o- l ogi n

var i abl es ar e f ound? Extr a credi t : Why does thi s f i l ebegi n wi t h a dot ( . ) ?

3. How can t he f i l e i n quest i on t he aut o- l ogi n f i l e bepr ot ect ed f r om unaut hor i zed r eadi ng?

97

Page 98: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 98/193

 

4. What do t he f ol l owi ng FTP commands do?

cdup

del et e ( t ough quest i on)

mdel et e ( di t t o)

mkdi r

r mdi r

Cont i nue on t he next pageCOMPUTER EXERCI SES ( 30 mi nut es)

5. Tr ansf er al l t he f i l es f r om on t he VAX ( er c830) t o t hedomax1. Use onl y one command and use wi l dcar ds. Theusername and password f or t he VAX wi l l be gi ven t o youby t he i nst r uct or .

6. Tr ansf er t he f i l es f r om t he VAX and t hi s t i me t r ansl at et he names of t he f i l es as t hey ar e tr ansf er r ed.

7. Cr eat e an aut o- l ogi n f i l e f or t he er c830 andt hen do an aut o- l ogi n t o t he VAX.

8. Logout .9. OPTI ONAL CHAPTER - KORNSHELL PROGRAMMI NG

 The KornShel l i s a command- programmi ng l anguage t hat execut escommands read f r om a t er mi nal or f i l e. The Kor nShel l i s cl ose t obei ng upward compat i bl e wi t h t he st andard Bour neShel l . Forexampl e, scr i pt s wr i t t en f or t he Bour neShel l ( sh) wi l l al so wor ki n t he KornShel l ( ksh) . The maj or enhancement s are command- l i ne

98

Page 99: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 99/193

r eent r y, i n- l i ne command edi t i ng, and al i asi ng.

 Thi s chapt er wi l l deal wi t h some, not al l , of t he f eat ures t hatdi f f er ent i at e i t f r om t he Bour neShel l . For your i nf or mat i on, her ei s a l i st of f eat ur es t hat have been enhanced f r omBour ne.I mprovement s have been made as a command l anguage, i ncl udi ngcommand- l i ne edi t i ng, a command hi st or y mechani sm, command- nameal i asi ng, j ob cont r ol , new capabi l i t i es f or cd, and t i l deexpansi on.

I mprovements as a Pr ogr ammi ng Language i ncl ude a more gener al I / Omechani sm, Menu sel ect i on pr i mi t i ve, Bui l t - i n i nt eger ar i t hmet i c,Subst r i ng oper ator s, Ar r ay var i abl es and at t r i but es, Mor e- gener alf uncti on f aci l i t y, Co- pr ocess f aci l i t y, Easi er t o debug, Bet t erperf ormance, and Bet t er secur i t y.

 The KornShel l i s a hi gh l evel programmi ng l anguage and a commandl i ne i nt er preter.

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »

º Command For mat : ksh [ acef hi kmnorst uvx] [ - o opt i on] . . . [ args] ºº ºº See t he appendi x f or a compl et e l i st of opt i ons ºº ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼ 

9. 1 Kor nShel l Var i abl es

 The KornShel l has var i abl es t hat ar e i ni t i al i zed f or you on l ogi nsuch as:

PATH Determi ne whi ch di r ector i es are sear ched, sequent i al l yf or shel l commands.

CDPATH The di r ect or i es t hat are searched f or argument s t o t he"cd" command.

SHELL The cur r ent i nt er act i ve shel l .

PWD The present worki ng di r ect ory.

I FS Val i d i nt er - f i el d separat ors used t o separat e commandsf r om argument s, argument s f r om each ot her and commandsf r om each ot her .

EDI TOR Command l i ne edi t mode.

FCEDI T Edi t or ent ered when usi ng f c

 TERM Def i nes t er mi nal capabi l i t i es

PS1 Pr i mary shel l prompt

PS2 Secondary shel l prompt

99

Page 100: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 100/193

PS3 Sel ect command pr ompt

HOME Home di r ect or y

ENV Fi l e( s) t o execut e when ent er i ng t hi s shel l

LOGNAME Logi n name of t he user

 The command l i ne ar gument var i abl es ar e al so avai l abl e:$$, $?, $*, $#, and t he posi t i onal s ( $0, $1, $2. . . et c)9. 2 User Def i ned Vari abl es

 These var i abl es ar e si mi l ar t o t he BourneShel l . The gener al f or mi s VARI ABLE=val ue.

No spaces ar e al l owed around t he =. You can encl ose t hem i n doubl equot es "=" or si ngl e quot es ' =' f or cl ar i t y. No spaces ar e al l owedi n "val ue" . These can al so be encl osed i n doubl e or si ngl e quotes.

"val ue" can be a st r i ng or an expr essi on. The val ue of a var i abl ecan be accessed by pr ecedi ng t he name of t he var i abl e wi t h a dol l arsi gn ( $) .

Exampl es:. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. $MySt r i ng' =' "Thi s i s a st r i ng" .. $MySt at i c=47 .. $r eadonl y Myst at i c .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

 The r eadonl y command makes MySt at i c r ead onl y ( can' t change t hecontents).

Exampl e:. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. $t ypeset - i BI GI NT=1492 .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

 Thi s wi l l make t he var i abl e i nt eger f or f ast er ar i t hmet i cs :

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. $t ypeset - i 8 OCTI NT=9 .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

 The out put of OCTI NT wi l l be an oct al i nt eger ; t he ass i gnment i sdeci mal .

Sampl e Sessi on:

Ú_______________________________________________________________¿| $echo $OCTI NT || 8#11 || $ |À_______________________________________________________________Ù

100

Page 101: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 101/193

9. 3 Val ues of Vari abl es Between Chi l d and Par ent Processes

Val ues i n one shel l ar e l ocal onl y t o t hat shel l . I f a chi l dpr ocess or a subshel l needs t o have access t o a val ue est abl i shedi n i t s par ent , t he val ue must be expor t ed f r om t he par ent .

$typeset - i 8 - x OCTI NT=19

 The - x opt i on export s t he var i abl e OCTI NT; subshel l s can r ead i t ,but can' t change the val ue i n the parent .

$MySt r i ng=' Hi t here'

At t hi s poi nt , t he var i abl e MySt r i ng i s l ocal t o t he cur r entpr ocess. I t i s not avai l abl e t o a chi l d pr ocess.

$export MySt r i ng

Now t he var i abl e MySt r i ng i s avai l abl e t o subshel l s.

 To al l ow a subshel l t o change t he cont ent of an export ed var i abl eand have t hat change be know t o t he parent , execut e t he chi l d wi t h" . program_name"

Sampl e Sessi on:

Ú_______________________________________________________________¿| $cat my. var s || echo Var i abl e comi ng i nt o scr i pt : $PARENT || PARENT=' chi l d val ue' || echo Var i abl e comi ng out of t he scr i pt : $PARENT || $PARENT=' parent val ue' || $echo $PARENT || par ent val ue || $my. var s || Var i abl e comi ng i nt o scri pt : || Var i abl e comi ng out of t he scr i pt : chi l d val ue || $echo $PARENT || par ent val ue || $ |À_______________________________________________________________Ù

Sampl e sessi on:

Ú_______________________________________________________________¿| $PARENT=' parent val ue' || $echo $PARENT || par ent val ue || $. my. var s || Var i abl e comi ng i nt o t he scr i pt : par ent val ue || Var i abl e comi ng out of t he scr i pt : chi l d val ue || $echo $PARENT |

101

Page 102: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 102/193

| chi l d val ue || $ |À_______________________________________________________________Ù

9. 4 ksh: Al i ases

 The f i r st wor d of each command can be r epl aced by t he t ext of anal i as i f an al i as f or t hi s wor d has been def i ned. The f i r stcharact er of an al i as name can be any pr i nt abl e charact er, but t her emai ni ng charact ers must be the same as any val i d i dent i f i er.Al i ases can be cr eat ed, l i st ed, and expor t ed wi t h t he al i ascommand. They can be r emoved wi t h t he unal i as command.

Al i asi ng i s per f or med when scr i pt s are read but not whi l e t hey ar eexecut ed. Ther ef or e, f or an al i as t o t ake ef f ect t he al i as commandhas t o be execut ed bef ore t he command whi ch ref erences t he al i asi s r ead.

 The f or mat f or t he al i as command i s as shown bel ow.

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command Format : al i as [ - t x] [ name=val ue] ºº ºº - t = t o set and l i st t r acked al i ases ºº - x = t o set or di spl ay expor t ed al i ases ºº ºº name = i f speci f i ed, i t must be a val i d al i as name ºº ºº val ue = val ue t o equat e wi t h name ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼ Exampl e:. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. $al i as .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

 The command al one wi l l di spl ay al l al i ases.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $al i as || cd=c || echo=pr i nt - || f al se=l et 0 || f unct i ons=t ypeset - f || hash=al i as - t |

| hi story=f c - l || i nt eger =t ypeset - i || moni t or =/ usr / l bi n/ t op || nohup=nohup || pwd=pr i nt - $PWD || r=f c - e - || t r ue=: || t ype=whence - v || $ |À_______________________________________________________________Ù

102

Page 103: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 103/193

Page 104: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 104/193

. $f c - e - ct=cat c .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

 The f i r st l i ne i s a del i berat e mi st ake; not i ce t he er r or message. The f c command execut es t he most r ecent command t hat st ar t s wi t ha "c" and changes t he f i r st occur r ence of "ct " t o "cat "; i t doesn' tent er t he edi t or .

Exampl e:. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. $l s / .. $f c - e - l s=cd .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

 The f i r st command wi l l l i st t he cont ent s of t he r oot di r ect or y. The f c command changes t hat command f r om " l s" t o "cd" ; t he " - "i ndi cat es t hat t he l i ne i s t o be edi t ed i nst ead of t aken i nt o t heedi t or bef or e execut i on.9. 6 ksh: I nt eract i ve Command Li ne Edi t i ng

I n t hi s met hod of command- l i ne edi t i ng, t he EDI TOR KornShel lvar i abl e cont r ol s t he edi t i ng.

Exampl e:. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. $EDI TOR=/ usr / bi n/ vi .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

 Thi s command wi l l put t he KornShel l i n t he vi edi t i ng mode.

 To ent er t he edi t hi st or y press Esc.

NOTE: Thi s exampl e i s f or t he vi edi t mode onl y; emacs orgmacs edi t modes use di f f erent key st okes.

 To move t hough t he . hi st or y f i l e use f ol l owi ng keys:

<k> Sel ect t he previ ous command

<j > Sel ect t he next command

<h> Next l et t er t o t he l ef t

<l > Next l et t er t o t he r i ght

When the command l i ne t hat you desi r e t o change i s di spl ayed ont he scr een, you can use t he f ol l owi ng commands t o make changes:

<i > i nsert char s Esc I nsert char act er s bef or e t hecursor

<A> append chars Esc Append charact er s at t he end of t he l i ne

104

Page 105: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 105/193

 <r > r epl acement char Esc Change si ngl e char act er

<cw> r epl acement wor d Esc Change si ngl e wor d

<x> Del et e si ngl e charact er

NOTE: A number can precede t he command as a count , f orexampl e, "3x" del etes 3 charact ers .

<dw> Del et e si ngl e word

Funct i ons execut e i n the same pr ocess as t he cal l er and shar e al l

º compound l i st º

NOTE: A number can precede t he command as a count , f orexampl e, "2dw" del et es 2 words.

( Ret ) Execut e t he al t ered command

When t he ( Ret ) key i s pressed, t he edi t ed command l i ne wi l l beexecut ed. Hi st ory i s not changed, but t he new command i s ent ered

i nt o t he . hi story f i l e at t he end.

 These ar e not t he onl y commands avai l abl e wi t h t he i nt er act i vecommand l i ne edi t i ng. You wi l l f i nd t hat t he ar r ow keys wi l l notwork whi l e edi t i ng t he command l i ne. Most of t he edi t or f eat ur eswi l l be avai l abl e f or you t o use, dependi ng upon whi ch edi t or wassel ect ed by t he EDI TOR shel l var i abl e.9. 7 ksh: Funct i ons

Funct i ons are si mi l ar t o al i ases, t hey run i n t he cur r ent Kor nShel lpr ocess and def i ne a set of act i ons. Posi t i onal par amet er s can ber ef er enced; ot her pr evi ousl y def i ned f unct i ons can be ref erenced;nested f uncti on def i ni t i ons ar e al l owed wi t h f ul l vi si bi l i t y t o al ldef i ni t i ons.

 The f unct i on keywor d i s used t o def i ne KornShel l f unct i ons. Thef unct i ons ar e r ead i n and st or ed i nt er nal l y. Any al i as names ar er esol ved when t he f unct i on i s r ead. Funct i ons ar e execut ed l i kecommands wi t h argument s passed as posi t i onal par amet er s.

f i l es, t r aps, and pr esent wor ki ng di r ectory wi t h t he cal l er .

 The f or mat f or f unct i ons i s:

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »

º Command For mat : ºº ºº f unct i on i dent i f i er ºº { º

º } ºº ºº i dent i f i er - name by whi ch t he f unct i on i s cal l ed ºº ºº compound l i st - The body of t he f unct i on. º

105

Page 106: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 106/193

ÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼  The l ef t bracket { and t he r i ght bracket } ar e consi dered t o ber eserved words. The body of t he f unct i on must exi st bet ween t het wo br acket s.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $f unct i on k || { || cd / ; l s - C; || } || $ |À_______________________________________________________________Ù

 To execut e t he f unct i on, si mpl y t ype t he name of t he f unct i on ont he command l i ne.

Sampl e Sessi on:

Ú_______________________________________________________________¿| $k || bck l i b t mp user12 user5 || bi n l ost t mp. j a user13 user6 || bsd nbox uni x user14 user7 |. .. .. .9. 8 ksh: The Sel ect Const r uct

 The "sel ect " const r uct i s uni que t o t he KornShel l . I t al l ows t heuser t o det ermi ne the act i on based on i nput f r om ei t her t he commandl i ne ( wi t hout an i n l i st ) or f r om an aut omat i cal l y pr ompt ed i nput .PS3 cont r ol s t he "sel ect " pr ompt .

 The command f or mat i s as f ol l ows:

ÉÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í »º Command For mat : sel ect i dent i f i er [ i n l i st ] ºº do ºº commands ºº done ºº ºÈÍ Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í ¼ 

I n the sel ect command:

ksh wi l l di spl ay t he i t ems i n one or more col umns on st andarder r or , each preceded by a number . The PS3 prompt f ol l ows. Thenumber of col umns i s det er mi ned by t he val ues of COLUMNS and LI NES.

ksh wi l l t hen r ead a sel ecti on l i ne f r om st andar d i nput . I f t hel i ne i s t he number of one of t he di spl ayed i t ems, ksh sets t heval ue of "i dent i f i er " t o t he i t em cor r espondi ng t o t hi s number . I f t he l i ne i s empt y, ksh agai n di spl ays t he l i st of i t ems; and t he

106

Page 107: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 107/193

prompt i s r edi spl ayed. The "commands" are not executed.

ksh saves t he cont ent s of t he sel ect i on l i ne read f r om st andar di nput i n t he KornShel l var i abl e REPLY.

ksh r uns " commands" f or each sel ect i on unt i l ksh encount ers a

| 1) f red |

| # Ct r l - C |

A Kor nShel l scr i pt t hat i s not execut abl e can be r un i mpl i ci t l y

break, r et ur n, or exi t command i n t he "commands" l i st .Sampl e Sessi on:

Ú_______________________________________________________________¿| $cat sel ect. ksh || stt y er ase || sel ect mysel ect i on i n f r ed wi l ma pebbl es bar ney bet t y || do || case $mysel ect i on i n || f red) || echo Fred was t he sel ect i on || ; ; || wi l ma) || echo Wi l ma was t he sel ect i on |

| ; ; || pebbl es) || echo Pebbl es was t he sel ect i on || ; ; || bar ney) || echo Barney was t he sel ect i on || ; ; || bet t y) || echo Bet t y was t he sel ect i on || ; ; || esac || done || $chmod 755 sel ect . ksh || $sel ect. ksh |

| 2) wi l ma || 3) pebbl es || 4) barney || 5) bet t y || #? 3 || Pebbl es was t he sel ect i on || #? 5 || Bet t y was t he sel ect i on || #? 6 || #? 4 || Barney was t he sel ect i on |

| $ |À_______________________________________________________________Ù9. 9 ksh: Tr aci ng and Condi t i onal Execut i on

wi t h t he ksh command. Traci ng can be accompl i shed usi ng t he- v or t he - x opt i on.

Sampl e Sessi on:

107

Page 108: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 108/193

 Ú_______________________________________________________________¿| $l s - l sel ect . ksh || - r w- r - - r - - 1 t eacher cl ass 390 Oct 16 09: 21 sel ect . ksh || $ksh - x sel ect. ksh || + st t y erase || 1) f red || 2) wi l ma || 3) pebbl es || 4) barney || 5) bet t y || #? 3 || + pr i nt - Pebbl es was t he sel ect i on || Pebbl es was t he sel ect i on || #? 5 || + pr i nt - Bet t y was t he sel ect i on |

| #? 4 |

| Barney was t he sel ect i on |

| Bet t y was t he sel ect i on || #? 6 |

| + pr i nt - Bar ney was t he sel ect i on |

| #? Ct r l - C || $ |À_______________________________________________________________Ù

 The - n opt i on wi l l t r ace execut i on of t he scr i pt wi t hout execut i on.Wor kshop 9

 Thi s wor kshop wi l l r ei nf or ce your underst andi ng of t he i deaspresent ed i n Chapt er 10. Logi n to t he Mul t i max usi ng the user nameand password gi ven t o you by t he i nst r uct or. Each st udent i s t ocompl et e t he ent i r e workshop.

DESK EXERCI SES

1. What command wi l l i nvoke t he KornShel l ?

2. What opt i on wi l l t r ace execut i on of a Kor nShel l scri pt ?

Cont i nue on t he next page3. What do t he f ol l owi ng shel l var i abl es i ndi cat e:

PATH

108

Page 109: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 109/193

 CDPATH

SHELL

PWD

I FS

EDI TOR

HOME

COMPUTER EXERCI SES ( 30 mi nut es)

t hat a l i st i ng wi l l f ol l ow, sl eep f or t hr ee seconds, and

FCEDI T

 TERM

 

PS1

PS2

PS3

ENV

LOGNAME

Cont i nue on t he next page4. What i s an al i as?

5. Def i ne a f uncti on k t hat wi l l :

Di spl ay t he pr esent worki ng di r ect ory, di spl ay a message

t hen l i st t he cont ent s.

109

Page 110: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 110/193

 

8. Logout

Cont i nue on t he next pageCompl et e t he Summar y Wor kshop

and

APPENDI X A - sh

NAME sh, r sh - shel l , t he st andard/ r est r i ct ed command pr ogr ammi ng

DESCRI PTI ON

6. Set up an al i as t o do t he l s - C f unct i on. Use a name of your own choi ce.

7. Wr i t e a KornShel l scr i pt usi ng t he sel ect command t odi spl ay t he f ol l owi ng choi ces:

Appl es

BananasPears J ack Dani el s

Af t er a choi ce has been made pr i nt t he f ol l owi ng:

"Thanks, your choi ce was" ( di spl ay t he choi ce)

Cour se Eval uat i onNOTES

ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ

l anguage

SYNOPSI Ssh [ - acef hi knr st uvx ] [ ar gs ]r sh [ - acef hi knr st uvx ] [ ar gs ]

sh i s a command pr ogr ammi ng l anguage t hat execut es commands

110

Page 111: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 111/193

r ead f r om a t er mi nal or a f i l e. r sh i s a r estr i cted ver si on

A bl ank i s a t ab or a space. A name i s a sequence of 

A pi pel i ne i s a sequence of one or more commands separat ed

shel l wai t s f or t he l ast command t o t ermi nate. The exi t

symbol && ( | | ) causes t he l i st f ol l owi ng i t t o be execut ed

f ol l owi ng. Unl ess other wi se st at ed, t he val ue r eturned by a

f or name [ i n wor d . . . ] do l i st done

next wor d t aken f r om t he i n wor d l i st . I f i n wor d . . .

of t he st andard command i nt erpr eter sh; i t i s used t o set upl ogi n names and execut i on envi r onment s whose capabi l i t i esare mor e cont r ol l ed t han t hose of t he st andard shel l . SeeI nvocat i on bel ow f or t he meani ng of argument s t o t he shel l .

Def i ni t i ons

l et t er s, di gi t s, or under scor es begi nni ng wi t h a l et t er orunderscore. A parameter i s a name, a di gi t , or any of t hechar act er s *, @, #, ?, - , $, and ! .

CommandsA si mpl e- command i s a sequence of non- bl ank words separat edby bl anks. The f i r st word speci f i es the name of t he commandt o be execut ed. Except as speci f i ed bel ow, t he r emai ni ngwords are passed as ar gument s t o t he i nvoked command. Thecommand name i s passed as argument 0 (see exec( 2) ) . Theval ue of a si mpl e- command i s i t s exi t st at us i f i t

t er mi nat es nor mal l y, or ( oct al ) 200+st at us i f i t t er mi nat esabnor mal l y ( see si gnal ( 2) f or a l i st of st at us val ues) .

by | . The st andard out put of each command but t he l ast i sconnected by a pi pe(2) t o t he st andard i nput of t he nextcommand. Each command i s r un as a separ at e pr ocess; t he

st at us of a pi pel i ne i s t he exi t st at us of t he l ast command.

A l i st i s a sequence of one or more pi pel i nes separat ed by; , &, &&, or | | , and opt i onal l y t er mi nat ed by ; or &. Of t hese f our symbol s, ; and & have equal pr ecedence, whi ch i sl ower t han t hat of && and | | . The symbol s && and | | al sohave equal pr ecedence. A semi col on ( ; ) causes sequent i alexecut i on of t he pr ecedi ng pi pel i ne; an ampersand ( &) causesasynchr onous execut i on of t he pr ecedi ng pi pel i ne ( i . e. , t heshel l does not wai t f or t hat pi pel i ne t o f i ni sh) . The

onl y i f t he pr ecedi ng pi pel i ne ret ur ns a zer o (non- zer o)exi t s t atus. An arbi t r ary number of new- l i nes may appear i na l i st , i nst ead of semi col ons, t o del i mi t commands.

A command i s ei t her a si mpl e- command or one of t he

command i s t hat of t he l ast si mpl e- command execut ed i n thecommand.

Each t i me a f or command i s execut ed, name i s set t o t he

i s omi t t ed, t hen t he f or command execut es t he do l i stonce f or each posi t i onal par amet er t hat i s set ( seePar ameter Subst i t ut i on bel ow) . Execut i on ends whent here are no more words i n t he l i st .

case word i n [ pat t ern [ | pat tern ] . . . ) l i s t ; ; ] . . . esacA case command executes t he l i st associ ated wi t h the

111

Page 112: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 112/193

f i r st pat t er n t hat mat ches wor d. The f or m of t hepat t erns i s t he same as t hat used f or f i l e- namegenerat i on ( see Fi l e Name Generat i on) except t hat asl ash, a l eadi ng dot , or a dot i mmedi at el y f ol l owi ng asl ash need not be matched expl i ci t l y.

i f l i s t t h e n l i s t [ e l i f l i s t t h e n l i s t ] . . . [ e l s e l i s t ] f i The l i st f ol l owi ng i f i s execut ed and, i f i t r et urns a

t hen t he i f command retur ns a zero exi t st atus.

and }. Execut i on of f unct i ons i s descr i bed bel ow ( see

t he st andar d output are r emoved.

No i nt er pr et at i on i s done on t he st r i ng bef or e t he st r i ng i s

zero exi t stat us, t he l i st f ol l owi ng t he f i r st t hen i sexecut ed. Ot her wi se, t he l i st f ol l owi ng el i f i sexecut ed and, i f i t s val ue i s zer o, t he l i st f ol l owi ngt he next t hen i s execut ed. Fai l i ng t hat , t he el se l i si s execut ed. I f no el se l i st or t hen l i st i s execut ed

whi l e l i st do l i st doneA whi l e command repeat edl y execut es t he whi l e l i st andi f t he exi t st at us of t he l ast command i n t he l i st i szer o, execut es t he do l i st ; ot her wi se t he l oopt ermi nates. I f no commands i n t he do l i st areexecut ed, t hen t he whi l e command r et urns a zer o exi tst at us; unt i l may be used i n pl ace of whi l e t o negat e

t he l oop t er mi nat i on test .( l i s t )Execut e l i st i n a sub- shel l .

{ l i st ; }l i st i s execut ed i n t he cur r ent ( t hat i s, par ent )shel l .

name ( ) { l i st ; }Def i ne a f unct i on whi ch i s ref erenced by name. Thebody of t he f unct i on i s t he l i st of commands bet ween {

Execut i on) .

 The f ol l owi ng wor ds ar e onl y r ecogni zed as t he f i r st wor d of a command and when not quot ed:

i f t hen el se el i f f i case esac f or whi l e unt i ldo done {}

Comment sA word begi nni ng wi t h # causes t hat word and al l t hef ol l owi ng char act er s up t o a new- l i ne to be i gnor ed.

Command Subst i t ut i on The shel l r eads commands f r om t he st r i ng bet ween t wo graveaccent s ( ``) and the st andar d output f r om t hese commands maybe used as al l or par t of a wor d. Tr ai l i ng new- l i nes f r om

r ead, except t o remove backsl ashes ( \ ) used t o escape ot herchar act ers. Backsl ashes may be used t o escape a graveaccent ( `) or another backsl ash ( \ ) and ar e removed bef oret he command st r i ng i s read. Escapi ng grave accents al l owsnest ed command subst i t ut i on. I f t he command subst i t ut i onl i es wi thi n a pai r of doubl e quotes ( " . . . ` . . . ` . . . " ) , abacksl ash used t o escape a doubl e quote ( \ " ) wi l l ber emoved; ot her wi se, i t wi l l be l ef t i nt act.

112

Page 113: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 113/193

 I f a backsl ash i s used t o escape a new- l i ne char act er( \ new- l i ne) , both t he backsl ash and t he new- l i ne ar e removed( see t he l at er sect i on on Quot i ng) . I n addi t i on,backsl ashes used t o escape dol l ar si gns ( \ $) are r emoved.Si nce no i nt erpr etat i on i s done on the command st r i ng bef orei t i s r ead, i nser t i ng a backsl ash t o escape a dol l ar si gnhas no ef f ect . Backsl ashes t hat pr ecede charact ers othert han \ , `, ", new- l i ne, and $ ar e l ef t i nt act when t hecommand st r i ng i s r ead.

Par amet er Subst i t ut i on The char act er $ i s used t o i nt r oduce subst i t ut abl eparameters . There are t wo t ypes of par ameters, posi t i onaland keywor d. I f par amet er i s a di gi t , i t i s a posi t i onalpar ameter . Posi t i onal paramet ers may be assi gned val ues byset . Keyword par amet ers ( al so known as var i abl es) may beassi gned val ues by wr i t i ng:

name=val ue [ name=val ue ] . . .

Pat t ern- mat chi ng i s not per f ormed on val ue. There cannot bea f unct i on and a var i abl e wi t h t he same name.

${par amet er } The val ue, i f any, of t he paramet er i s subst i t ut ed. The braces ar e r equi r ed onl y when paramet er i s f ol l owedby a l et t er , di gi t , or under scor e t hat i s not t o bei nt er pr et ed as par t of i t s name. I f par amet er i s * or@, al l t he posi t i onal par amet er s, st ar t i ng wi t h $1, ar esubst i t ut ed ( separat ed by spaces) . Par ameter $0 i s setf r om argument zero when t he shel l i s i nvoked.

${par amet er : - word}I f par amet er i s set and i s non- nul l , subst i t ut e i t sval ue; ot her wi se subst i t ut e wor d.

${par amet er : =wor d}I f par amet er i s not set or i s nul l set i t t o wor d; t heval ue of t he par amet er i s subst i t ut ed. Posi t i onalpar amet ers may not be assi gned t o i n t hi s way.

${par amet er : ?wor d}I f par amet er i s set and i s non- nul l , subst i t ut e i t sval ue; ot her wi se, pr i nt wor d and exi t f r om t he shel l .I f word i s omi t t ed, t he message "par ameter nul l or notset " i s pr i nt ed.

${par amet er : +wor d}I f par amet er i s set and i s non- nul l , subst i t ut e wor d;ot her wi se subst i t ut e not hi ng.

I n t he above, wor d i s not eval uat ed unl ess i t i s t o be usedas the subst i t ut ed st r i ng, so t hat , i n t he f ol l owi ngexampl e, pwd i s execut ed onl y i f d i s not set or i s nul l :

echo ${d: - `pwd`}

I f t he col on ( : ) i s omi t t ed f r om t he above expr essi ons, t heshel l onl y checks whether par ameter i s set or not .

113

Page 114: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 114/193

The f ol l owi ng par ameter s are aut omat i cal l y set by t he shel l :# The number of posi t i onal parameters i n deci mal .- Fl ags suppl i ed t o t he shel l on i nvocat i on or by

t he set command.? The deci mal val ue r etur ned by t he l ast

synchronousl y execut ed command.$ The pr ocess number of t hi s shel l .! The process number of t he l ast background command

i nvoked.

 The f ol l owi ng paramet er s ar e used by t he shel l :HOME The def aul t argument ( home di r ect ory) f or t he cd

command.PATH The sear ch pat h f or commands ( see Execut i on

bel ow) . The user may not change PATH i f 

mi nut es). I f set t o 0, t he shel l wi l l checkbef or e each prompt .

SHACCT

execut i ng under r sh.CDPATH

 The sear ch pat h f or t he cd command.MAI L I f t hi s parameter i s set t o t he name of a mai l

f i l e and t he MAI LPATH parameter i s not set , t he

shel l i nf or ms t he user of t he ar r i val of mai l i nt he speci f i ed f i l e.MAI LCHECK 

 Thi s paramet er speci f i es how of t en ( i n seconds)t he shel l wi l l check f or t he ar r i val of mai l i nt he f i l es speci f i ed by the MAI LPATH or MAI Lparameters. The def aul t val ue i s 600 seconds ( 10

MAI LPATHA col on ( : ) separ at ed l i st of f i l e names. I f  t hi s par amet er i s set, t he shel l i nf or ms t he userof t he ar r i val of mai l i n any of t he speci f i edf i l es. Each f i l e name can be f ol l owed by % and amessage t hat wi l l be pr i nt ed when t hemodi f i cat i on t i me changes. The def aul t messagei s you have mai l .

PS1 Pri mary pr ompt st r i ng, by def aul t "$ " .PS2 Secondary pr ompt st r i ng, by def aul t "> " .I FS I nt er nal f i el d separ at or s, nor mal l y space, t ab,

and new- l i ne.

I f t hi s par amet er i s set t o t he name of a f i l ewr i t abl e by t he user , t he shel l wi l l wr i t e anaccount i ng r ecor d i n t he f i l e f or each shel lpr ocedur e execut ed. Account i ng r out i nes such asacct com( 1) and acct cms( 1M) can be used to anal yze

t he dat a col l ect ed.SHELL When t he shel l i s i nvoked, i t scans t heenvi r onment ( see Envi r onment bel ow) f or t hi sname. I f i t i s f ound and ' r sh' i s the f i l e namepar t of i t s val ue, t he shel l becomes a restr i ctedshel l .

 The shel l gi ves def aul t val ues t o PATH, PS1, PS2, MAI LCHECK and I FS. HOME and MAI L are set by l ogi n(1) .

114

Page 115: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 115/193

Bl ank I nt er pr et at i onAf t er parameter and command subst i t ut i on, t he r esul t s of subst i t ut i on ar e scanned f or i nt er nal f i el d separ at orchar acters ( t hose f ound i n I FS) and spl i t i nt o di st i nctargument s where such charact ers ar e f ound. Expl i ci t nul lar gument s ( "" or ' ' ) are r et ai ned. I mpl i ci t nul l ar gument s( t hose resul t i ng f r ompar amet er s t hat have no val ues) ar er emoved.

I nput / Out putA command' s i nput and out put may be redi r ect ed usi ng aspeci al not at i on i nt er pr et ed by t he shel l . The f ol l owi ngmay appear anywhere i n a si mpl e- command or may pr ecede orf ol l ow a command and ar e not passed on t o t he i nvokedcommand; subst i t ut i on occur s bef ore word or di gi t i s used:

<wor d Use f i l e word as st andar d i nput ( f i l edescri pt or 0) .

>wor d Use f i l e word as st andar d out put ( f i l edescr i pt or 1) . I f t he f i l e does not exi st i t

i s cr eat ed; ot her wi se, i t i s t r uncat ed t o zer ol engt h.>>wor d Use f i l e word as st andar d out put . I f t he f i l e

exi st s out put i s appended t o i t ( by f i r stseeki ng t o t he end- of - f i l e) ; ot her wi se, t hef i l e i s created.

<<[ - ] word Af t er par amet er and command subst i t ut i on aredone on wor d, t he shel l i nput i s r ead up t ot he f i rst l i ne t hat l i t er al l y mat ches t her esul t i ng wor d, or t o an end- of - f i l e. I f ,however , - i s appended t o <<:1) l eadi ng t abs ar e st r i pped f r om wor d bef or e

t he shel l i nput i s r ead ( but af t erpar amet er and command subst i t ut i on i s doneon word) ,

2) l eadi ng t abs ar e st r i pped f r om t he shel li nput as i t i s r ead and bef or e each l i nei s compar ed wi t h word, and

3) shel l i nput i s r ead up t o t he f i r st l i net hat l i t er al l y mat ches t he r esul t i ng wor d,or t o an end- of - f i l e.

I f any char act er of wor d i s quot ed ( seeQuot i ng, l at er ) , no addi t i onal pr ocessi ng i sdone t o t he shel l i nput . I f no char act er s of word are quoted:1) par amet er and command subst i t ut i on occur s,2) ( escaped) \ newl i ne i s i gnored, and

3) \ must be used t o quote t he charact ers \ ,$, and `. The r esul t i ng document becomes t he st andar di nput .

<&di gi t Use t he f i l e associ at ed wi t h f i l e descri pt ordi gi t as st andar d i nput . Si mi l ar l y f or t hest andard out put usi ng >&di gi t .

<&- The st andar d i nput i s cl osed. Si mi l ar l y f ort he st andard out put usi ng >&- - .

115

Page 116: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 116/193

I f any of t he above i s pr eceded by a di gi t , t he f i l edescri pt or whi ch wi l l be associ at ed wi t h t he f i l e i s t hatspeci f i ed by t he di gi t ( i nst ead of t he def aul t 0 or 1). Forexampl e:

. . . 2>&1

associ at es f i l e descr i pt or 2 wi t h t he f i l e cur r ent l yassoci at ed wi t h f i l e descri pt or 1.

 The or der i n whi ch r edi r ect i ons ar e speci f i ed i ssi gni f i cant . The shel l eval uat es r edi r ecti ons l ef t - t o-r i ght . For exampl e:

. . . 1>xxx 2>&1

f i rst associ at es f i l e descr i pt or 1 wi t h f i l e xxx. I tassoci at es f i l e descri pt or 2 wi t h t he f i l e associ at ed wi t hf i l e descr i pt or 1 ( i . e. xxx) . I t di rect s bot h standardout put and st andar d er r or out put ( st dout , st der r ) t o xxx.

I f t he or der of r edi r ecti ons wer e r ever sed, f i l e descri pt or2 woul d be associ at ed wi t h the ter mi nal ( assumi ng f i l edescr i pt or 1 had been) and f i l e descr i pt or 1 woul d beassoci at ed wi t h f i l e xxx.

Usi ng t he termi nol ogy i nt r oduced on t he f i r st page, underCommands, i f a command i s composed of several si mpl ecommands, r edi r ect i on wi l l be eval uat ed f or t he ent i r ecommand bef ore i t i s eval uat ed f or each si mpl e command. That i s, t he shel l eval uat es r edi r ect i on f or t he ent i r el i st , t hen each pi pel i ne wi t hi n t he l i st , t he each commandwi t hi n each pi pel i ne, t hen each l i st wi t hi n each command.

I f a command i s f ol l owed by & t he def aul t st andard i nput f ort he command i s the empt y f i l e / dev/ nul l . Ot herwi se, t heenvi r onment f or t he execut i on of a command cont ai ns t he f i l edescr i pt or s of t he i nvoki ng shel l as modi f i ed byi nput / out put speci f i cat i ons.

Redi r ecti on of out put i s not al l owed i n t he rest r i ctedshel l .

Fi l e Name Generat i onBef or e a command i s execut ed, each command wor d i s scannedf or t he char act er s *, ?, and [ . I f one of t hese char act er sappear s, t he word i s r egarded as a pat t ern. The word i sr epl aced wi t h al phabet i cal l y sor t ed f i l e names t hat mat ch

t he pat t er n. I f no f i l e name i s f ound t hat mat ches t hepat t ern, t he word i s l ef t unchanged. The char act er . at t hest ar t of a f i l e name or i mmedi at el y f ol l owi ng a / , as wel las t he char act er / i t sel f , must be mat ched expl i ci t l y.

* Mat ches any st r i ng, i ncl udi ng t he nul l st r i ng.? Matches any si ngl e charact er.[ . . . ] Matches any one of the encl osed char act ers. A

pai r of charact ers separat ed by - matches anychar act er l exi cal l y bet ween t he pai r ,

116

Page 117: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 117/193

i ncl usi ve. I f the f i rst charact er f ol l owi ngt he openi ng "[ " i s a "! " any char act er notencl osed i s mat ched.

Quot i ng The f ol l owi ng char act er s have a speci al meani ng t o t he shel land cause t ermi nat i on of a word unl ess quoted:

; & ( ) | ^ < > new- l i ne space t ab

A character may be quot ed ( i . e. , made to stand f or i t sel f )by pr ecedi ng i t wi t h a backsl ash ( \ ) or i nser t i ng i t bet weena pai r of quot e mar ks ( ' ' or " ") . Dur i ng pr ocessi ng, t heshel l may quot e cer t ai n char act er s t o pr event t hemf r omt aki ng on a speci al meani ng. Backsl ashes used t o quot e asi ngl e charact er ar e removed f r om t he word bef ore t hecommand i s execut ed. The pai r \ newl i ne i s r emoved f r om aword bef or e command and paramet er subst i t ut i on.

Al l charact ers encl osed between a pai r of si ngl e quote marks

( ' ' ) , except a si ngl e quot e, ar e quot ed by t he shel l .Backsl ash has no speci al meani ng i nsi de a pai r of si ngl equotes. A si ngl e quot e may be quot ed i nsi de a pai r of doubl e quote marks ( f or exampl e, " ' " ) .

I nsi de a pai r of doubl e quote marks ( " " ) , parameter andcommand subst i t ut i on occur s and t he shel l quotes t he resul t st o avoi d bl ank i nt er pr et at i on and f i l e name gener at i on. I f $* i s wi t hi n a pai r of doubl e quot es, t he posi t i onalpar ameters ar e subst i t ut ed and quot ed, separated by quotedspaces ( "$1 $2 . . . ") ; however , i f $@ i s wi t hi n a pai r of doubl e quotes, t he posi t i onal par amet er s are subst i t ut ed andquot ed, separ at ed by unquot ed spaces ( "$1" " $2" . . . ) . \quot es the char act er s \ , `, ", and $. The pai r \ newl i ne i sr emoved bef ore par amet er and command subst i t ut i on. I f abacksl ash pr ecedes char act er s ot her t han \ , `, ", $, andnew- l i ne, t he backsl ash i t sel f i s quot ed by t he shel l .

Prompt i ngWhen used i nt eract i vel y, t he shel l pr ompt s wi t h t he val ue of PS1 bef ore r eadi ng a command. I f at any t i me a new- l i ne i st yped and f urt her i nput i s needed to compl et e a command, t hesecondar y pr ompt ( i . e. , t he val ue of PS2) i s i ssued.

envi r onment i n several ways. On i nvocat i on, t he shel l scans

Envi r onment The envi r onment ( see envi r on( 5) ) i s a l i st of name- val uepai r s t hat i s passed t o an execut ed pr ogr am i n t he same way

as a nor mal ar gument l i st . The shel l i nt er act s wi t h t he

t he envi r onment and creat es a paramet er f or each name f ound,gi vi ng i t t he cor r espondi ng val ue. I f t he user modi f i es theval ue of any of t hese paramet ers or cr eat es new par ameters,none of t hese af f ect s t he envi r onment unl ess t he exportcommand i s used t o bi nd t he shel l ' s par ameter t o t heenvi r onment ( see al so set - a) . A par amet er may be r emovedf r om t he envi r onment wi t h t he unset command. Theenvi r onment seen by any execut ed command i s t hus composed of 

117

Page 118: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 118/193

any unmodi f i ed name- val ue pai r s or i gi nal l y i nher i t ed by theshel l , mi nus any pai r s r emoved by unset , pl us anymodi f i cat i ons or addi t i ons, al l of whi ch must be not ed i nexpor t commands.

 The envi r onment f or any si mpl e- command may be augment ed bypr ef i xi ng i t wi t h one or more assi gnment s t o parameters. Thus:

 TERM=450 cmdand

( expor t TERM; TERM=450; cmd)

are equi val ent ( as f ar as t he execut i on of cmd i sconcerned) .

I f t he - k f l ag i s set , al l keywor d ar gument s ar e pl aced i nt he envi r onment , even i f t hey occur af t er t he command name. The f ol l owi ng f i r st pr i nt s a=b c and c:

echo a=b cset -kecho a=b c

Si gnal s The I NTERRUPT and QUI T si gnal s f or an i nvoked command ar ei gnored i f t he command i s f ol l owed by &; otherwi se si gnal shave the val ues i nher i t ed by the shel l f r om i t s par ent , wi t ht he except i on of si gnal 11 ( SI GSEGV) ( but see al so t he t r apcommand bel ow) . See nohup(1) f or more si gnal handl i ng.

Execut i onEach t i me a command i s execut ed, t he above subst i t ut i ons ar ecar r i ed out . I f t he command name mat ches one of t he Speci alCommands l i st ed bel ow, i t i s execut ed i n t he shel l pr ocess.I f t he command name does not mat ch a Speci al Command, butmatches t he name of a def i ned f unct i on, t he f unct i on i sexecut ed i n t he shel l pr ocess ( not e how t hi s di f f er s f r omt he execut i on of shel l pr ocedur es) . The posi t i onalpar amet er s $1, $2, . . . . ar e set t o t he ar gument s of t hef unct i on. I f t he command name mat ches nei t her a Speci alCommand nor t he name of a def i ned f unct i on, a new process i scr eat ed and an at t empt i s made t o execut e t he command vi aexec( 2) .

 The shel l paramet er PATH def i nes t he sear ch path f or t hedi r ect or y cont ai ni ng t he command. Al t ernat i ve di r ect or y

names are separated by a col on ( : ) . The def aul t path i s: / bi n: / usr/ bi n ( speci f yi ng t he cur r ent di r ector y, / bi n, and/ usr / bi n, i n t hat or der ) . Not e t hat t he cur r ent di r ectoryi s speci f i ed by a nul l pat h name, whi ch can appeari mmedi atel y af t er t he equal si gn or between the col ondel i mi t er s anywher e el se i n t he path l i st . I f t he commandname cont ai ns a / t he sear ch pat h i s not used; such commandswi l l not be execut ed by t he r est r i ct ed shel l . Ot her wi se,each di r ect or y i n t he pat h i s sear ched f or an execut abl ef i l e. I f t he f i l e has execut e per mi ssi on but i s not an

118

Page 119: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 119/193

a. out f i l e, i t i s assumed t o be a f i l e cont ai ni ng shel lcommands. A sub- shel l i s spawned t o r ead i t . Apar ent hesi zed command i s al so executed i n a sub- shel l .

 The l ocat i on i n t he sear ch pat h where a command was f ound i sr emember ed by t he shel l ( t o hel p avoi d unnecessary execsl at er ) . I f t he command was f ound i n a r el at i ve di r ect or y,i t s l ocat i on must be r e- determi ned whenever t he cur r entdi r ect or y changes. The shel l f or get s al l r ememberedl ocat i ons whenever t he PATH var i abl e i s changed or t he hash- r command i s execut ed ( see bel ow) .

Speci al CommandsI nput / out put r edi r ect i on i s now per mi t t ed f or t hesecommands. Fi l e descr i pt or 1 i s t he def aul t out put l ocat i on.

:No ef f ect ; t he command does nothi ng. A zero exi t codei s ret ur ned.

. f i l e

Read and execut e commands f r om f i l e and r et urn. Thesear ch path speci f i ed by PATH i s used t o f i nd t he

Change t he cur r ent di r ectory t o arg. The shel l

cur r ent di r ect ory i s speci f i ed by a nul l pat h name,

echo [ ar g . . . ]

I nput / out put argument s may appear and, i f no other

di r ector y cont ai ni ng f i l e.break [ n ]

Exi t f r om t he encl osi ng f or or whi l e l oop, i f any. I f  n i s speci f i ed br eak n l evel s.

cont i nue [ n ]Resume t he next i t er at i on of t he encl osi ng f or or whi l el oop. I f n i s speci f i ed r esume at t he nt h encl osi ngl oop.

cd [ arg ]

parameter HOME i s t he def aul t arg. The shel l paramet erCDPATH def i nes t he sear ch path f or t he di r ectorycont ai ni ng ar g. Al t er nati ve di r ect or y names ar esepar at ed by a col on ( : ) . The def aul t pat h i s <nul l >( speci f yi ng t he cur r ent di r ectory) . Not e t hat t he

whi ch can appear i mmedi at el y af t er t he equal si gn orbetween the col on del i mi t ers anywhere el se i n the pathl i st . I f ar g begi ns wi t h a / t he sear ch pat h i s notused. Ot her wi se, each di r ect or y i n t he pat h i ssear ched f or ar g. The cd command may not be execut edby rsh.

Echo ar gument s. See echo( 1) f or usage and descr i pt i on.eval [ arg . . . ]

 The ar gument s ar e r ead as i nput t o t he shel l and t her esul t i ng command(s) executed.exec [ ar g . . . ]

 The command speci f i ed by t he ar guments i s execut ed i npl ace of t hi s shel l wi t hout cr eat i ng a new pr ocess.

argument s are gi ven, cause the shel l i nput / out put t o bemodi f i ed.

exi t [ n ]Causes a shel l t o exi t wi t h t he exi t st at us speci f i ed

119

Page 120: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 120/193

by n. I f n i s omi t t ed t he exi t st at us i s that of t hel ast command execut ed ( an end- of - f i l e wi l l al so causet he shel l t o exi t . )

expor t [ name . . . ]

expor t ed f r om a par ent shel l ar e l i st ed onl y i f t hey

For each name, t he l ocat i on i n t he search path of t he

al l r emembered l ocat i ons. I f no argument s ar e gi ven,

sear ch pat h, af t er changi ng t o t hat di r ect or y, t he

wi l l be i ncr ement ed when t he recal cul at i on i s done.

r ead [ name . . . ]

other t han new- l i ne can be quot ed by pr ecedi ng t hem

t hese names may not be changed by subsequent

Causes a f unct i on t o exi t wi t h t he ret ur n val ue

 The gi ven names ar e mar ked f or aut omat i c export t o t heenvi r onment of subsequent l y- execut ed commands. I f noar gument s are gi ven, a l i st of al l names that ar eexpor t ed i n t hi s shel l i s pr i nt ed. ( Var i abl e names

have been export ed agai n dur i ng t he cur r ent shel l ' sexecut i on. ) Funct i on names may not be expor t ed.

getopt sUse i n shel l scr i pt t o suppor t command synt ax st andards( see i nt r o( 1) ) ; i t par ses posi t i onal par amet er s andchecks f or l egal opt i ons. See get opt s( 1) f or usage anddescri pt i on.

hash [ - r ] [ name . . . ]

command speci f i ed by name i s deter mi ned and r ememberedby t he shel l . The - r opt i on causes the shel l t o f or get

i nf or mat i on about r emembered commands i s present ed.hi t s i s t he number of t i mes a command has been i nvokedby t he shel l pr ocess. cost i s a measure of t he workr equi r ed t o l ocat e a command i n t he sear ch path. I f acommand i s f ound i n a "r el at i ve" di r ect or y i n t he

st ored l ocat i on of t hat command i s r ecal cul ated.Commands f or whi ch t hi s wi l l be done ar e i ndi cat ed byan ast er i sk ( *) adj acent t o t he hi t s i nf or mat i on. cost

newgr p [ ar g . . . ]Equi val ent t o exec newgr p arg . . . . See newgr p( 1M) f orusage and descr i pt i on.

pwdPr i nt t he cur r ent worki ng di r ect or y. See pwd( 1) f orusage and descr i pt i on.

One l i ne i s r ead f r omt he st andar d i nput and, usi ng t hei nt er nal f i el d separ at or , I FS ( nor mal l y space or t ab) ,t o del i mi t wor d boundar i es, t he f i r st wor d i s assi gnedt o the f i r st name, t he second word to t he second name,et c. , wi t h l ef t over wor ds assi gned t o t he l ast name.Li nes can be cont i nued usi ng \ new- l i ne. Charact er s

wi t h a backsl ash. These backsl ashes are r emoved bef orewords are assi gned t o names, and no i nt erpretat i on i s

done on t he charact er t hat f ol l ows the backsl ash. Ther et ur n code i s 0 unl ess an end- of - f i l e i s encount er ed.r eadonl y [ name . . . ]

 The gi ven names ar e mar ked r eadonl y and t he val ues of 

assi gnment . I f no ar gument s ar e gi ven, a l i st of al lr eadonl y names i s pr i nt ed.

return [ n ]

speci f i ed by n. I f n i s omi t t ed, t he r et ur n st at us i s

120

Page 121: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 121/193

t hat of t he l ast command execut ed.

- k Al l keyword argument s are pl aced i n t he

- v Pr i nt shel l i nput l i nes as t hey ar e r ead.

shi f t [ n ] The posi t i onal paramet er s f r om $n+1 . . . ar e r enamed $1

shel l r ecei ves si gnal ( s) n. ( Not e t hat ar g i s scanned

f r om t he shel l . The t r ap command wi t h no argument s

set [ - - aef hknt uvx [ ar g . . . ] ]- a

Mark var i abl es whi ch are modi f i ed or cr eat ed f orexport .

- e Exi t i mmedi atel y i f a command exi t s wi t h a non-zer o exi t st at us.

- f Di sabl e f i l e name gener ati on.- h Locat e and r emember f unct i on commands as f unct i ons

are def i ned ( f unct i on commands are normal l yl ocated when t he f unct i on i s execut ed) .

envi r onment f or a command, not j ust t hose t hatpr ecede t he command name.

- n Read commands but do not execut e t hem.- t Exi t af t er r eadi ng and execut i ng one command.- u Treat unset var i abl es as an err or when

subst i t ut i ng.

- x Pr i nt commands and t hei r argument s as t hey are

execut ed.- - Do not change any of t he f l ags; usef ul i n set t i ng$1 t o - .

Usi ng + r ather t han - causes t hese f l ags t o be tur nedof f . These f l ags can al so be used upon i nvocat i on of t he shel l . The cur r ent set of f l ags may be f ound i n$- . The r emai ni ng argument s ar e posi t i onal parametersand ar e assi gned, i n or der , t o $1, $2, . . . . I f noargument s ar e gi ven the val ues of al l names arepr i nt ed.

. . . . I f n i s not gi ven, i t i s assumed t o be 1.test

Eval uat e condi t i onal expr essi ons. See t est ( 1) f or usageand descr i pt i on.

t i mesPri nt t he accumul ated user and syst emt i mes f orpr ocesses r un f r om t he shel l .

t rap [ arg ] [ n ] . . . The command ar g i s t o be r ead and execut ed when t he

once when the t r ap i s set and once when the t r ap i st aken. ) Trap commands are execut ed i n or der of si gnalnumber . Any at t empt t o set a t r ap on a si gnal t hat wasi gnor ed on ent r y to t he cur r ent shel l i s i nef f ecti ve.An at t empt t o t r ap on si gnal 11 (memory f aul t ) produces

an er r or . I f ar g i s absent al l t r ap( s) n ar e r eset t ot hei r or i gi nal val ues. I f ar g i s t he nul l str i ng t hi ssi gnal i s i gnored by t he shel l and by t he commands i ti nvokes. I f n i s 0 t he command arg i s execut ed on exi t

pr i nt s a l i st of commands associ ated wi t h each si gnalnumber .

t ype [ name . . . ]For each name, i ndi cate how i t woul d be i nt erpr eted i f used as a command name.

121

Page 122: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 122/193

ul i mi t [ n ]I mpose a si ze l i mi t of n bl ocks on f i l es wr i t t en by t heshel l and i t s chi l d pr ocesses ( f i l es of any si ze may ber ead) . I f n i s omi t t ed, t he cur r ent l i mi t i s pr i nt ed.Each user may l ower t he ul i mi t , but onl y a super - user( see su( 1M) ) can r ai se a ul i mi t .

umask [ nnn ]

mask i s pr i nt ed.

f rom/ et c/ prof i l e and f rom$HOME/ . prof i l e, i f such f i l es

cont ai ni ng commands, and t he r emai ni ng argument s ar e passed

str i ng.

par amet er s. Shel l out put ( except f or Speci al

- i I f the - i f l ag i s present or i f the shel l i nput

t hat wai t i s i nt err upt i bl e) . I n al l cases, QUI T

- r I f the - r f l ag i s present the shel l i s a

r sh Onl y

 The user f i l e- cr eat i on mask i s set t o nnn ( seeumask(1)) . I f nnn i s omi t t ed, t he cur r ent val ue of t he

unset [ name . . . ]For each name, r emove t he cor r espondi ng var i abl e orf unct i on. The var i abl es PATH, PS1, PS2, MAI LCHECK andI FS cannot be unset .

wai t [ n ]Wai t f or a backgr ound pr ocess whose pr ocess I D i s n andr epor t i t s t er mi nat i on st at us. I f n i s omi t t ed, al lt he shel l ' s cur r ent l y act i ve backgr ound pr ocesses ar ewai t ed f or and the r et ur n code wi l l be zer o.

I nvocat i onI f t he shel l i s i nvoked t hr ough exec( 2) and t he f i r stchar act er of ar gument zer o i s - , commands are i ni t i al l y read

exi st . Thereaf t er , commands ar e r ead as descr i bed bel ow,whi ch i s al so the case when t he shel l i s i nvoked as / bi n/ sh. The f l ags bel ow ar e i nt er pret ed by t he shel l on i nvocat i ononl y. Not e t hat unl ess t he - c or - s f l ag i s speci f i ed, t hef i r st argument i s assumed t o be the name of a f i l e

as posi t i onal parameters t o that command f i l e:

- c st r i ng I f t he - c f l ag i s pr esent commands are r ead f r om

- s I f t he - s f l ag i s pr esent or i f no ar gument sr emai n commands are read f r om t he st andar d i nput .Any r emai ni ng argument s speci f y t he posi t i onal

Commands) i s wr i t t en t o f i l e descr i pt or 2.

and out put ar e at t ached t o a ter mi nal , t hi s shel li s i nt er act i ve. I n t hi s case TERMI NATE i s i gnor ed( so t hat ki l l 0 does not ki l l an i nt er acti veshel l ) and I NTERRUPT i s caught and i gnored ( so

i s i gnor ed by t he shel l .

rest r i ct ed shel l .

 The r emai ni ng f l ags and ar guments ar e descr i bed under t heset command above.

r sh i s used t o set up l ogi n names and execut i on envi r onment swhose capabi l i t i es are more cont r ol l ed t han t hose of t hest andar d shel l . The act i ons of r sh ar e i dent i cal t o t hoseof sh, except t hat t he f ol l owi ng ar e di sal l owed:

122

Page 123: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 123/193

 changi ng di r ect or y ( see cd( 1) ) ,set t i ng the val ue of $PATH,speci f yi ng pat h or command names contai ni ng / ,r edi r ect i ng out put ( > and >>) .

 The r est r i ct i ons above ar e enf or ced af t er . prof i l e i si nt er pr et ed.

A r est r i ct ed shel l can be i nvoked i n one of t he f ol l owi ngways: ( 1) r sh i s t he f i l e name par t of t he l ast ent r y i n t he/ et c/ passwd f i l e (see passwd( 4) ) ; ( 2) t he envi r onmentvar i abl e SHELL exi st s and r sh i s t he f i l e name par t of i t sval ue; ( 3) t he shel l i s i nvoked and r sh i s t he f i l e namepar t of ar gument 0; ( 4) t he shel l i s i nvoked wi t h t he - ropt i on.

When a command t o be execut ed i s f ound t o be a shel lpr ocedur e, r sh i nvokes sh t o execut e i t . Thus, i t i spossi bl e t o pr ovi de to t he end- user shel l pr ocedur es t hat

have access t o t he f ul l power of t he st andar d shel l , whi l ei mposi ng a l i mi t ed menu of commands; t hi s scheme assumest hat t he end- user does not have wr i t e and execut epermi ssi ons i n t he same di r ectory.

 The net ef f ect of t hese r ul es i s t hat t he wr i t er of t he. pr of i l e has compl et e cont r ol over user act i ons, byperf ormi ng guarant eed set up act i ons and l eavi ng t he user i nan appr opr i at e di r ect or y (probabl y not t he l ogi n di r ect or y).

Er r or s det ect ed by t he shel l , such as synt ax er r ors, cause

bei ng used non- i nt er acti vel y execut i on of t he shel l f i l e i s

/ et c/ prof i l e

pwd( 1) , t est ( 1) , umask( 1) .acct cms( 1M) , newgrp( 1M) , su( 1M) i n the UMAX V

 The syst em admi ni st r at or of t en set s up a di r ect or y of commands ( i . e. , / usr / r bi n) t hat can be saf el y i nvoked byr sh. Some syst ems al so pr ovi de a r est r i ct ed edi t or r ed.

EXI T STATUS

t he shel l t o r et ur n a non- zer o exi t st at us. I f t he shel l i s

abandoned. Ot her wi se, t he shel l r et ur ns the exi t s t atus of t he l ast command execut ed ( see al so t he exi t command above) .

FI LES

$HOME/ pr of i l e/ t mp/ sh*/ dev/ nul l

SEE ALSOacct com( 1) , cd( 1) , echo( 1) , env( 1) , ksh( 1) , l ogi n( 1) ,

Admi ni st r ator' s Ref erence Manual .dup( 2) , exec(2) , f or k( 2) , pi pe( 2) , si gnal ( 2) , ul i mi t ( 2) ,wai t ( 2) , a. out ( 4) , passwd( 4) , pr of i l e( 4) , envi r on( 5) i n t heUMAX V Pr ogr ammer ' s Ref er ence Manual .

CAVEATS

123

Page 124: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 124/193

Page 125: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 125/193

i n t he second SYNOPSI S l i ne) must be separ at e ar gument s t ot he t est command; nor mal l y these i t ems ar e separat ed byspaces.

 The f ol l owi ng pr i mi t i ves ar e used t o const r uct expr :

- r f i l e t r ue i f f i l e exi s ts and i s readabl e.

- w f i l e t r ue i f f i l e exi s ts and i s wr i tabl e.

- x f i l e t rue i f f i l e exi sts and i s execut abl e.

- f f i l e t r ue i f f i l e exi s t s and i s a regul ar f i l e.

- d f i l e t r ue i f f i l e exi s ts and i s a di rectory .

- c f i l e t rue i f f i l e exi sts and i s a charact er speci alf i l e.

- b f i l e t r ue i f f i l e exi s ts and i s a bl ock speci al f i l e.

- p f i l e t r ue i f f i l e exi st s and i s a named pi pe ( f i f o) .

- u f i l e t r ue i f f i l e exi s ts and i t s set - user - I D bi t i sset.

- g f i l e t r ue i f f i l e exi s ts and i t s set - group- I D bi t i sset.

- k f i l e t r ue i f f i l e exi s t s and i t s s t i cky bi t i s set .

- s f i l e t r ue i f f i l e exi st s and has a si ze gr eat er t hanzero.

- t [ f i l d e s ]t r ue i f t he open f i l e whose f i l e descri pt ornumber i s f i l des ( 1 by def aul t ) i s associ at edwi t h a t er mi nal devi ce.

- z s1 t r ue i f t he l engt h of st r i ng s1 i s zer o.

- n s1 t r ue i f t he l engt h of t he st r i ng s1 i s non- zer o.

s1 = s2 t r ue i f str i ngs s1 and s2 ar e i dent i cal .

s1 ! = s2 t r ue i f st r i ngs s1 and s2 ar e not i dent i cal .

s1 t r ue i f s1 i s not t he nul l str i ng.

n1 - eq n2 t r ue i f t he i nt eger s n1 and n2 are al gebr ai cal l yequal . Any of t he compar i sons - ne, - gt , - ge,- l t , and - l e may be used i n pl ace of - eq.

 These pr i mar i es may be combi ned wi t h t he f ol l owi ngoperators:

! unary negat i on operat or .

125

Page 126: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 126/193

 - a

bi nary and operat or.

- o bi nary or operator ( - a has hi gher pr ecedencet han - o) .

( expr ) par ent heses f or gr oupi ng. Not i ce t hatparent heses ar e meani ngf ul t o t he shel l and,t heref ore, must be escaped.

SEE ALSOcsh( 1) , f i nd( 1) , ksh( 1) , sh( 1) .

WARNI NGI f a user t ests a f i l e ( usi ng - r , - w, or - x) , but t heper mi ssi on t est ed does not have t he owner bi t set , a non-zer o (f al se) exi t st at us wi l l be r et ur ned even t hough t hef i l e may have t he gr oup or ot her bi t set f or t hatper mi ssi on. The cor r ect exi t st at us wi l l be set i f t he user

i s super - user .

oper ator s, onl y t he f i r st ar gument i s exami ned; t he ot her s

ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ

 The = and ! = oper at or s have a hi gher precedence t han t he - rt hrough - n oper at ors, and = and != al ways expect argument s;t heref ore, = and ! = cannot be used wi t h t he - r t hrough - noperators.

I f more than one ar gument f ol l ows t he - r t hr ough - n

are i gnored, unl ess a - a or a - o i s t he second argument .

I n t he second f orm of t he command ( i . e. , t he one that uses[ ] , r at her t han t he wor d t est ) , t he squar e br ackets must bedel i mi t ed by bl anks.

NOTES

APPENDI X C - expr

$man exprNAME

expr - eval uate ar gument s as an expr essi on

SYNOPSI Sexpr ar guments

DESCRI PTI ON The ar gument s ar e t aken as an express i on. Af t er eval uat i on,t he r esul t i s wr i t t en on t he st andar d out put . Terms of t heexpr essi on must be separat ed by bl anks. Characters speci alt o t he shel l must be escaped. Note t hat 0 i s retur ned t oi ndi cat e a zer o val ue, r at her t han t he nul l st r i ng. St r i ngscont ai ni ng bl anks or ot her speci al char act er s shoul d bequot ed. I nteger - val ued argument s may be preceded by a unarymi nus si gn. I nt er nal l y, i nt eger s ar e t r eat ed as 32- bi t , 2scompl ement number s.

126

Page 127: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 127/193

  The operat or s and keywor ds ar e l i st ed bel ow. Charact er st hat need t o be escaped are pr eceded by \ . The l i st i s i norder of i ncr easi ng pr ecedence, wi t h equal pr ecedenceoperat ors gr ouped wi t hi n {} symbol s.

expr \ | expr r et ur ns t he f i r st expr i f i t i s nei t her nul lnor 0, otherwi se retur ns t he second expr .

expr \ & expr r et ur ns t he f i r st expr i f nei t her expr i snul l nor 0, ot her wi se r et ur ns 0.

expr { =, \ >, \ , \ <, \ , ! = } exprr et ur ns t he r esul t of an i nt eger compari soni f both ar gument s are i nt eger s, ot her wi ser et ur ns t he r esul t of a l exi cal compar i son.

expr { +, - } expraddi t i on or subt r acti on of i nt eger - val uedar guments.

expr { \ *, / , % } exprmul t i pl i cat i on, di vi si on, or r emai nder of t hei nt eger - val ued argument s.

expr : expr The matchi ng operat or : compares t he f i r star gument wi t h t he second argument whi ch mustbe a r egul ar expr essi on. Regul ar expr essi onsynt ax i s t he same as t hat of ed( 1) , exceptmat chi ng operat or r et urns t he number of char act er s mat ched (0 on f ai l ur e) .Al t er nat i vel y, t he \ ( . . . \ ) pat t er n symbol scan be used t o r et ur n a por t i on of t he f i r star gument .

EXAMPLES1. a= expr $a + 1`

adds 1 t o t he shel l var i abl e

2. # For $a equal t o ei t her "/ usr / abc/ f i l e" or j ust" f i l e "expr $a : ' . * / \ ( . * \ ) ' \ | $a

|r et ur ns t he l ast segment of a pat h name ( i . e. , f i l e) .Watch out f or / al one as an argument : expr wi l l t akei t as t he di vi si on oper ator ( see BUGS bel ow) .

3. # A bet t er r epr esent at i on of t he pr evi ous exampl e.expr / / $a : ' . * / \ ( . * \ ) ' The addi t i on of t he / / char act er s el i mi nates anyambi gui t y about t he di vi si on oper ator and si mpl i f i est he whol e expr essi on.

4. expr $VAR : ' . *'r etur ns t he number of char act ers i n $VAR.

SEE ALSO

127

Page 128: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 128/193

Page 129: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 129/193

Suppl y a suppl ement al password r equi r ed by ar emot e syst em f or access t o r esour ces once a l ogi nhas been successf ul l y compl et ed. I f no argumenti s i ncl uded, t he user wi l l be pr ompt ed f or anaccount password i n a non- echoi ng i nput mode.

append l ocal - f i l e [ r emot e- f i l e ]Append a l ocal f i l e to a f i l e on t he remot emachi ne. I f r emot e- f i l e i s l ef t unspeci f i ed, t hel ocal f i l e name i s used to name the remot e f i l eaf t er bei ng al t ered by any nt r ans or nmap set t i ng.Fi l e t r ansf er uses t he cur r ent set t i ngs f or t ype,f ormat , mode, and st r uct ur e.

asci i Set t he f i l e t r ansf er t ype t o net wor k ASCI I . Thi si s t he def aul t t ype.

bel l Sound a bel l af t er each f i l e t r ansf er command i scompl et ed.

bi nar y Set t he f i l e t r ansf er type t o suppor t bi nar y i magetransfer.

bye Ter mi nate t he FTP sessi on wi t h t he r emot e ser verand exi t f t p.

case Toggl e r emot e comput er f i l e name case mappi ngdur i ng mget commands. When case i s on ( def aul t i sof f ) , r emot e comput er f i l e names wi t h al l l et t er si n upper case ar e wr i t t en i n t he l ocal di r ect or ywi t h the l et t ers mapped t o l ower case.

cd r emot e- di r ect or yChange t he worki ng di r ect ory on t he r emot e machi net o r emot e- di r ect or y.

cdup Change t he r emot e machi ne worki ng di r ect ory t o t heparent of t he cur r ent r emote machi ne worki ngdi r ectory.

cl ose Ter mi nate t he FTP sessi on wi t h t he r emote server ,and r etur n t o t he command i nt erpr et er . Anydef i ned macr os are er ased.

cr Toggl e car r i age r et ur n st r i ppi ng dur i ng ASCI I t ypef i l e r et r i eval . Recor ds ar e denot ed by a car r i ager et ur n/ l i nef eed sequence dur i ng ASCI I t ype f i l e

t r ansf er . When cr i s on ( t he def aul t ) , car r i ager et ur ns are st r i pped f r om t hi s sequence t o conf or mwi t h t he UNI X si ngl e l i nef eed r ecor d del i mi t er .Recor ds on non- UNI X remot e sys t ems may cont ai nsi ngl e l i nef eeds; when an ASCI I t ype t r ansf er i smade, t hese l i nef eeds may be di st i ngui shed f r om ar ecor d del i mi t er onl y when cr i s of f .

del et e r emot e- f i l eDel ete t he f i l e r emote- f i l e on t he r emote machi ne.

129

Page 130: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 130/193

 

Pr i nt t he cont ent s of di r ect or y, r emot e- di r ect or y,

I f no di r ectory i s speci f i ed, t he cur r ent wor ki ng

f ormat i s f i l e.

get remote- f i l e [ l ocal - f i l e ]

machi ne, subj ect t o al t erat i on by the cur r ent

mput . I f gl obbi ng i s t ur ned of f wi t h gl ob, t he

expanded. Gl obbi ng f or mput i s done as i n csh( 1) .

l i st s ar e not mer ged. Expansi on of a di r ect or y

not meant t o t r ansf er ent i r e di r ect or y subt r ees of 

archi ve of t he subt r ee ( i n bi nary mode) .

hash Toggl e number- si gn ( #) pr i nt i ng f or each data

1024 byt es.

hel p [ command ]

debug [ debug- val ue ] Toggl e debuggi ng mode. I f an opt i onal debug- val uei s speci f i ed, i t i s used t o set t he debuggi ngl evel . When debuggi ng i s on, f t p pr i nt s eachcommand sent t o t he remot e machi ne, preceded byt he st r i ng - - > .

di r [ remot e- di rect ory ] [ l ocal - f i l e ]

and, opt i onal l y, pl ace t he out put i n l ocal - f i l e.

di r ectory on t he r emote machi ne i s used. I f nol ocal f i l e i s speci f i ed, or l ocal - f i l e i s - ,out put comes t o t he t ermi nal .

di sconnectA synonym f or cl ose.

f or m f or mat

Set t he f i l e t r ansf er f or m t o f or mat . The def aul t

Ret r i eve t he r emot e- f i l e and st or e i t on t he l ocalmachi ne. I f t he l ocal f i l e name i s not speci f i ed,i t i s gi ven the same name i t has on t he r emote

case, nt r ans, and nmap set t i ngs. The cur r entsett i ngs f or t ype, f or m, mode, and st r uct ur e ar eused whi l e t r ansf er r i ng t he f i l e.

gl ob Toggl e f i l ename expansi on f or mdel ete, mget and

f i l e name argument s are t aken l i t eral l y and not

For mdel et e and mget , each r emot e f i l e name i sexpanded separat el y on t he r emot e machi ne and t he

name i s l i kel y to be di f f er ent f r om expansi on of t he name of an or di nar y f i l e: t he exact r esul tdepends on t he f orei gn operat i ng syst em and FTPser ver , and can be previ ewed by doi ng"ml s r emote- f i l es - " . Note: mget and mput are

f i l es. That can be done by t r ansf er r i ng a t ar ( 1)

bl ock tr ansf er r ed. The si ze of a dat a bl ock i

Pr i nt a descr i pt i on of command. Wi t h no argument ,f t p pr i nt s a l i st of t he known commands.

l cd [ di r ector y ]

130

Page 131: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 131/193

Change t he worki ng di r ect ory on t he l ocal machi ne.I f no di r ectory i s speci f i ed, changes t o t heuser ' s home di r ect ory.

newl i ne char acters i n a f i l e or car r i age retur ns

mget r emote- f i l es

l s [ remot e- di r ect or y ] [ l ocal - f i l e ]Pr i nt an abbr evi at ed l i st i ng of t he cont ent s of adi r ectory on the r emote machi ne. I f r emote-di r ectory i s l ef t unspeci f i ed, t he cur r ent wor ki ngdi rect ory i s used. I f no l ocal f i l e i s spec i f i ed,t he out put i s sent t o t he t er mi nal .

macdef macr o- nameDef i ne a macro. Subsequent l i nes are st ored ast he macr o- name; a nul l l i ne ( consecut i ve

f r om t he t ermi nal ) t ermi nates macr o i nput mode. There i s a l i mi t of 16 macr os and 4096 t ot alcharact ers i n al l def i ned macros. Macr os r emai ndef i ned unt i l a cl ose command i s execut ed. Themacro pr ocessor i nt er pr ets "$" and "\ " as speci al

charact ers. A "$" f ol l owed by a number ( ornumber s) i s r epl aced by t he cor r espondi ng argumenton t he macr o i nvocat i on command l i ne. A "$"f ol l owed by an "i " si gnal s t hat macr o pr ocessort hat t he execut i ng macro i s t o be l ooped. On t hef i r st pass "$i " i s r epl aced by t he f i r st ar gumenton t he macr o i nvocat i on command l i ne, on t hesecond pass i t i s r epl aced by t he second argument ,and so on. A " \ " f ol l owed by any char act er i sr epl aced by t hat char act er . Use t he "\ " t opr event speci al t r eat ment of t he "$".

mdel et e [ r emot e- f i l es ]Del ete t he speci f i ed f i l es on t he r emote machi ne.

mdi r r emot e- f i l es l ocal - f i l eLi ke di r , except mul t i pl e r emot e f i l es may bespeci f i ed. I f i nt er acti ve pr ompt i ng i s on, f t pwi l l pr ompt t he user t o ver i f y t hat t he l astar gument i s i ndeed t he t ar get l ocal f i l e f orr ecei vi ng mdi r out put .

Expand t he r emot e- f i l es on t he r emot e machi ne anddo a get f or each f i l e name t hus pr oduced. Seegl ob f or det ai l s on t he f i l ename expansi on.Resul t i ng f i l e names wi l l t hen be pr ocessed

accor di ng to case, nt r ans, and nmap set t i ngs.Fi l es ar e t r ansf er r ed i nt o t he l ocal wor ki ngdi r ect ory, whi ch can be changed wi t h"l cd di r ectory"; new l ocal di r ectori es can becreat ed wi t h "! mkdi r di r ect or y".

mkdi r di r ect ory- nameMake a di r ect or y on t he r emot e machi ne.

ml s r emot e- f i l es l ocal - f i l e

131

Page 132: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 132/193

Page 133: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 133/193

Page 134: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 134/193

cont r ol connect i on t o t he host on t he pr i marycont r ol connecti on. Thi r d par t y f i l e t r ansf er sdepend upon suppor t of t he FTP pr ot ocol PASVcommand by t he ser ver on the secondary cont r olconnect i on.

put l ocal - f i l e [ remote- f i l e ]St or e a l ocal f i l e on t he r emot e machi ne. I f remot e- f i l e i s l ef t unspeci f i ed, t he l ocal f i l ename i s used i n nami ng t he remote f i l e, af t erpr ocessi ng accordi ng t o any nt r ans or nmapset t i ngs. Fi l e t r ansf er uses t he cur r ent set t i ngsf or t ype, f or mat, mode, and st r uct ur e.

pwd Pri nt t he name of t he cur r ent worki ng di r ectory ont he remot e machi ne.

qui t A synonym f or bye.

quote arg1 arg2 . . .

 The ar gument s speci f i ed ar e sent , ver bat i m, t o t her emot e FTP server .

recv remot e- f i l e [ l ocal - f i l e ]A synonym f or get .

r emot ehel p [ command- name ]Request hel p f r om t he r emot e FTP server . I f acommand- name i s speci f i ed, i t i s suppl i ed t o theser ver as wel l .

r ename [ f r om ] [ t o ]Rename, on t he r emote machi ne, t he f i l e f r om t othe f i l e t o.

r eset Cl ear r epl y queue. Thi s command r e- synchr oni zescommand/ r epl y sequenci ng wi t h the r emot e FTPser ver . Resynchroni zat i on may be necessaryf ol l owi ng a vi ol at i on of t he FTP pr ot ocol by t her emot e ser ver .

r mdi r di r ectory- nameDel et e a di r ect ory on t he r emote machi ne.

r uni que Toggl e st or i ng of f i l es on t he l ocal syst em wi t huni que f i l enames. I f a f i l e al r eady exi st s wi t h aname equal t o the target l ocal f i l ename f or a get

or mget command, a " . 1" i s appended t o t he name.I f t he r esul t i ng name matches anot her exi st i ngf i l e, a ". 2" i s appended t o t he or i gi nal name. I f t hi s pr ocess cont i nues up t o ". 99" , an er r ormessage i s pri nt ed, and t he t r ansf er does not t akepl ace. The generat ed uni que f i l ename wi l l ber epor t ed. Not e t hat r uni que wi l l not af f ect l ocalf i l es generated f r om a shel l command ( see bel ow) . The def aul t val ue i s of f .

134

Page 135: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 135/193

send l ocal - f i l e [ remot e- f i l e ]A synonym f or put .

sendpor t Toggl e t he use of PORT commands. By def aul t , f t pat t empts t o use a PORT command when est abl i shi ng aconnect i on f or each data t r ansf er. The use of PORTcommands can pr event del ays when per f or mi ngmul t i pl e f i l e t r ansf er s. I f t he PORT commandf ai l s, f t p uses t he def aul t dat a por t . When t heuse of PORT commands i s di sabl ed, no at t empt i smade to use themf or each dat a t r ansf er . Thi s i susef ul f or cer t ai n FTP i mpl ement at i ons t hat doi gnor e PORT commands but wr ongl y i ndi cat e t heyhave been accept ed.

st at us Show t he cur r ent st at us of f t p.

st r uct [ st r uct - name ]Set t he f i l e tr ansf er st r ucture to st r uct- name. The def aul t st r uct ure i s st r eam.

suni que Toggl e st or i ng of f i l es on r emote machi ne underuni que f i l e names. Remot e FTP ser ver must suppor tt he FTP pr ot ocol STOU command f or successf ulcompl et i on. The r emot e server wi l l r epor t auni que name. Def aul t val ue i s of f .

t enex Set t he f i l e t r ansf er t ype t o t hat needed t o t al kt o TENEX machi nes.

t r ace Toggl e packet t r aci ng.

t ype [ t ype- name ]Set t he f i l e t r ansf er t ype t o t ype- name. I f not ype- name i s speci f i ed, t he cur r ent t ype i spr i nt ed. The def aul t t ype i s net wor k asci i .

user user - name [ password ] [ account ] The user i dent i f i es hi m/ hersel f t o t he r emot e FTPser ver . I f t he passwor d i s not speci f i ed and t heser ver r equi r es i t , f t p pr ompt s t he user f or i t( af t er di sabl i ng l ocal echo) . I f an account f i el di s not speci f i ed, and t he FTP ser ver r equi r es i t ,t he user i s pr ompt ed f or i t . I f an account f i el di s speci f i ed, an account command wi l l be rel ayedt o the remot e ser ver af t er t he l ogi n sequence i scompl et ed i f t he remot e ser ver di d not r equi r e i t

f or l oggi ng i n. Unl ess f t p i s i nvoked wi t h"aut o- l ogi n" di sabl ed, t hi s pr ocess i s doneaut omat i cal l y on i ni t i al connect i on t o t he FTPserver.

verbose Toggl e verbose mode. I n verbose mode, al lr esponses f r om t he FTP ser ver ar e di spl ayed t o theuser . I n addi t i on, i f ver bose i s on, when a f i l et r ansf er compl et es, st at i st i cs regar di ng t heef f i ci ency of t he t r ansf er ar e r epor t ed. By

135

Page 136: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 136/193

Page 137: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 137/193

r emote f i l e names, t he remote f i l ename i s t he l ocalf i l ename, whi ch may be al t ered by a nt r ans or nmapset t i ng. The r esul t i ng f i l ename may t hen be al t ered byt he remot e ser ver i f suni que i s on.

FI LE TRANSFER PARAMETERS The FTP speci f i cat i on i dent i f i es many paramet er s t hat canaf f ect a f i l e t r ansf er . The t ype can be one of asci i , i mage( bi nar y) , ebcdi c, and l ocal byt e si ze ( f or PDP- 10' s andPDP- 20' s most l y) . f t p support s the asci i and i mage t ypes of f i l e tr ansf er , pl us l ocal byt e si ze 8 f or t enex modetransfers.

f t p suppor t s onl y t he def aul t val ues f or t he r emai ni ng f i l et r ansf er parameters : mode, f orm, and st r uct .

OPTI ONSOpt i ons can be speci f i ed at t he command l i ne, or t o t hecommand i nterpret er .

 The - v ( ver bose on) opt i on f or ces f t p t o show al l r esponsesf r om t he r emot e server , as wel l as r eport on dat a t r ansf ers t at i st i cs .

 The - n opt i on r est r ai ns f t p f r om at t empt i ng "auto- l ogi n"upon i ni t i al connecti on. I f aut o- l ogi n i s enabl ed, f t pchecks t he net r c f i l e i n t he user ' s home di r ect or y f or anent r y descr i bi ng an account on t he remot e machi ne. I f noent r y exi st s, f t p wi l l pr ompt f or t he r emot e machi ne l ogi nname (def aul t i s t he user i dent i t y on t he l ocal machi ne) ,and, i f necessary, prompt f or a password and an account wi t hwhi ch t o l ogi n.

 The - i opt i on t urns of f i nt er act i ve prompt i ng dur i ngmul t i pl e f i l e t ransf ers.

 The - d opt i on enabl es debuggi ng.

 The - g opt i on di sabl es f i l e name gl obbi ng.

 THE . net r c FI LE The . net r c f i l e cont ai ns l ogi n and i ni t i al i zat i oni nf or mat i on used by t he "aut o- l ogi n" pr ocess. I t r esi des i nt he user' s home di r ect or y. The f ol l owi ng t okens ar er ecogni zed; t hey may be separated by spaces, t abs, or new-l i nes:

machi ne nameI dent i f y a r emot e machi ne name. The aut o- l ogi n pr ocesssear ches t he . net r c f i l e f or a machi ne token t hatmat ches t he remot e machi ne speci f i ed on the f t p commandl i ne or as an open command ar gument . Once a mat ch i smade, t he subsequent . net r c t okens are pr ocessed,st oppi ng when t he end of f i l e i s r eached or anothermachi ne t oken i s encount ered.

l ogi n name

137

Page 138: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 138/193

I dent i f y a user on t he r emot e machi ne. I f t hi s t okeni s pr esent , t he "aut o- l ogi n" pr ocess wi l l i ni t i at e al ogi n usi ng t he speci f i ed name.

passwor d st r i ngSuppl y a passwor d. I f t hi s t oken i s pr esent , t he"aut o- l ogi n" pr ocess wi l l suppl y t he speci f i ed st r i ngi f t he remot e ser ver r equi r es a passwor d as par t of t hel ogi n pr ocess. Not e t hat i f t hi s token i s pr esent i nt he . net rc f i l e, f t p wi l l abort t he "aut o- l ogi n"pr ocess i f t he . net r c i s r eadabl e by anyone besi des t heuser.

account st r i ngSuppl y an addi t i onal account passwor d. I f t hi s tokeni s pr esent , t he "aut o- l ogi n" pr ocess wi l l suppl y thespeci f i ed st r i ng i f t he r emot e ser ver r equi r es anaddi t i onal account passwor d, or t he "aut o- l ogi n"pr ocess wi l l i ni t i ate an ACCT command i f i t does not .

macdef nameDef i ne a macro. Thi s t oken f unct i ons l i ke t he f t pmacdef command f unct i ons. A macro i s def i ned wi t h t hespeci f i ed name; i t s cont ent s begi n wi t h t he next . net r cl i ne and cont i nue unt i l a nul l l i ne ( consecut i ve new-l i ne char act er s) i s encount er ed. I f a macro named i ni ti s def i ned, i t i s aut omat i cal l y execut ed as t he l astst ep i n t he "aut o- l ogi n" pr ocess.

SEE ALSOcsh(1).f t pd( 1M) i n the UMAX V Admi ni st r at or ' s Ref erence Manual .

BUGSCor r ect execut i on of many commands depends upon properbehavi or by t he r emot e server .

An er r or i n t he tr eatment of car r i age r et ur ns i n t he 4. 2BSDUNI X ASCI I - mode t r ansf er code has been corr ect ed. Thi scor r ecti on may r esul t i n i ncor r ect t r ansf er s of bi nar y f i l est o and f r om 4. 2BSD ser ver s usi ng t he asci i t ype. Avoi d t hi spr obl emby usi ng the bi nary i mage t ype.

NOTESÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ

NOTESÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ

APPENDI X E - cc

$man cc

NAMEcc - C compi l er

SYNOPSI Scc [ opt i on ] . . . f i l e . . .

138

Page 139: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 139/193

DESCRI PTI ON The cc command i nvokes t he C l anguage compi l er . Thi s Ccompi l er i s an advanced, opt i mi zi ng compi l er t hat accept s acompl et e i mpl ement at i on of t he C progr ammi ng l anguage. Fora more compl ete descr i pt i on of t he compi l er, see "CLanguage" and "Compi l er and C Language" i n t he UMAX VPr ogr ammer ' s Gui de.

Fi l es wi t h a . c suf f i x ar e t aken t o be C l anguage sour ceprogr ams. The compi l er processes every C l anguage sourcef i l e t o pr oduce a cor r espondi ng obj ect f i l e wi t h t he samef i l e name and a . o suf f i x. Fi l es wi t h a . s suf f i x ar e t akent o be assembl y l anguage source progr ams. These areassembl ed t o pr oduce a corr espondi ng obj ect f i l e wi t h t hesame f i l e name and a . o suf f i x. Fi l es wi t h a suf f i x ot hert han . c and . s are assumed t o be obj ect f i l es (usual l ypr oduced by an earl i er compi l at i on or assembl y) or C-compat i bl e l i br ar i es. These f i l es, t oget her wi t h any obj ectcode pr oduced by the compi l er , are l i nked i n t he or der t heywere speci f i ed t o pr oduce an execut abl e pr ogr amf i l e named

a. out .

I f onl y one i nput f i l e wi t h a . c or . s suf f i x i s suppl i ed,t he compi l er aut omat i cal l y del et es t he obj ect f i l e out putpr oduced f r om t hat i nput f i l e af t er t he execut abl e pr ogr amf i l e a. out i s creat ed.

 The cc opt i ons t hat modi f y t he behavi or descr i bed above ar e:

- A Cause ASCI I assembl er out put t o be gener at ed andaut omat i cal l y pi ped t o t he assembl er . The def aul ti s f or di r ect gener at i on of obj ect code. The - Aopt i on i s t he same as t he - q nodi r ect_code opt i on.

- Bpath Run t he compi l er pr ogr am cont ai ned i n pathccom. I f - B i s speci f i ed wi t h no pat h, t hen t he def aul t pat hi s assumed t o be / l i b/ o and t he compi l er pr ogr am i n/ l i b/ occom i s run. I f no - B opt i on i s speci f i ed,t hen t he compi l er pr ogr am i n / l i b/ ccom i s r un.

- c Compi l e onl y. Pr oduce obj ect f i l e out put , even i f t her e was onl y one sour ce f i l e.

- C Ret ai n comment s dur i ng t he macr o prepr ocessor pass .

- Dname=def Def i ne symbol name t o be st r i ng def , as i f by a

#def i ne st atement . I f =def i s omi t t ed, def i ne namet o be 1.

- E Run onl y t he macro pr epr ocessor , pr ocess onl y i nputf i l es wi t h t he . c suf f i x; send t he resul t of t hi spass t o t he st andard out put .

- g Generat e speci al symbol t abl e data f or sdb( 1) orcdb( 1) and pass t he - g f l ag t o the l i nk edi t or .

139

Page 140: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 140/193

- G Cause obj ect code t o be di r ect l y generat ed by t hecompi l er, bypassi ng t he i nt ermedi ate st eps of produci ng assembl y code and assembl i ng i t t opr oduce obj ect code. Thi s i s t he def aul t . The - Gopt i on i s t he same as t he - q di r ect _code opt i on.

- I di r di r i s a di r ectory name. Sear ch f or #i ncl ude f i l eswhose names do not begi n wi t h / f i r st i n t hedi r ectory cont ai ni ng t he sour ce f i l e, t hen i n di r ,and t hen i n a l i st of st andar d def aul t s. Mul t i pl e- I opt i ons can est abl i sh a hi er ar chy of #i ncl udef i l e di rector i es .

- o out putName the f i nal , execut abl e out put f i l e out puti nst ead of a. out . Note t he space between t he - oand the f i l e name.

- O Per f orm opt i mi zat i ons whi ch speed up t he generat edcode. Al so, per f or m any space opt i mi zat i ons whi ch

do not i mpact code speed. See al so the - q opt i on.

- p Prepar e t o gener at e an execut i on pr of i l e usi ngpr of ( 1) . I ncl ude speci al pr of i l i ng code t hatcount s how many t i mes each r out i ne i s cal l ed. I f l i nki ng occur s, use a speci al st ar t up r out i ne t hatcal l s moni t or( 3C) and pr oduces a mon. out f i l e upont er mi nat i on. Uses speci al pr of i l i ng ver si ons of standard l i brar i es f ound i n / usr/ l i b/ l i bp/ l i b* . a.

NOTE: use of t he MARK macro ( see pr of ( 5) ) r equi r est he - A opt i on of cc.

- pg Prepar e t o gener at e an execut i on pr of i l e usi nggpr of ( 1) . I ncl ude speci al pr of i l i ng code t hatcount s how many t i mes each f unct i on i s cal l ed andhow much t i me i s spent i n each. I f l i nki ng occur s,use a speci al st ar t up f uncti on t hat cal l smonst ar t up and pr oduces a gmon. out f i l e upont er mi nat i on. Uses speci al pr of i l i ng ver si ons of standard l i brar i es f ound i n / usr/ l i b/ l i bp/ l i b* . a.Note: Use of t he MARK macro (see pr of ( 5) ) r equi r est he - A opt i on of cc.

- P Run al l . c f i l es t hr ough t he pr epr ocessi ng st ep,put t i ng t he r esul t i n t he cor r espondi ng out put f i l ewi th a . i suf f i x.

- R Make i ni t i al i zed var i abl es shar ed and r ead- onl y ( bypassi ng t he - r opt i on t o the assembl er ) .

- S Generat e onl y assembl y l anguage out put , put t i ng i ti n one or more f i l es t hat have the sour ce f i l e nameand an . s suf f i x.

- Uname Undef i ne symbol name t o r emove i t s def aul tdef i ni t i on.

140

Page 141: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 141/193

- v Report t he names of al l subpr ocesses i nvoked i n t hecompi l ed pr ogr am, and t hei r ar gument s. Thi s opt i onshows any f i l es t hat ar e l i nked aut omat i cal l y andt he cur r ent compi l er , assembl er, and l i nk edi t oropt i ons.

- w Suppr ess warni ng di agnost i cs.

- Wc, arg- Wa, arg- Wl , arg Pass opt i on arg t o t he compi l er ( see "C Compi l er

I nt ernal Opt i ons" i n t he "Compi l er and C Language"chapt er i n the UMAX V Programmer ' s Gui de) ,assembl er ( see as( 1) ) , or l i nker ( see l d( 1) ) ,r especti vel y.

 The f ol l owi ng opt i ons ar e i nt ended t o provi de mor e det ai l edcont r ol over t he generat ed code and act i on of t he compi l er .I n gener al , t hey shoul d onl y be used f or speci al si t uat i ons.

- q qual i f i er- q qual i f i er =ar gModi f y t he generated code of t he compi l er t or ef l ect var i ous speci al r equi r ement s of a pr ogr am.Qual i f i er s i ncl ude t he f ol l owi ng:

al i gn_t ext , noal i gn_t extEnabl e al i gnment of t ext segment s on boundar i est hat al l ows t he bur st mode of syst ems equi ppedwi t h APCs ( Advanced Dual Processor Cards,ut i l i zi ng t he NS32332 CPU chi p) t o be mostef f ecti vel y used. The def aul t opt i on i s- q noal i gn_t ext, unl ess the - q opt i mi ze=t i meopt i on i s speci f i ed.

xpc, apc, dpcGenerat e code opt i mi zed f or a syst em equi ppedwi t h XPCs ( Ext ended Per f ormance Dual ProcessorCards, ut i l i zi ng t he NS32532 CPU chi p) , APCs( Advanced Dual Processor Car ds, ut i l i zi ng t heNS32332 CPU chi p) , or DPCs ( Dual ProcessorCar ds, ut i l i zi ng t he NS32032 CPU chi p) . I f t he- q xpc opt i on i s speci f i ed, t hen t heprepr ocessor symbol ns32532 i s def i ned and codeopt i mal f or t he NS32532 i s generat ed. I f t he- q apc opt i on i s speci f i ed, t hen t hepr epr ocessor symbol ns32332 i s def i ned and the

- q al i gn_t ext opt i on i s enabl ed. I f t he - q dpcopt i on i s speci f i ed, t hen t he pr epr ocessorsymbol ns32032 i s def i ned and t he- q noal i gn_t ext opt i on i s enabl ed. I f nei t her- q xpc nor - q apc nor - q dpc i s speci f i ed, t hent he def aul t opt i on i s ei t her - q xpc or - q apcor - q dpc , dependi ng upon whet her t he syst emupon whi ch t he compi l er i s r unni ng i s equi ppedwi t h XPCs, APCs, or DPCs, r espect i vel y. Codegener at ed wi t h t hese opt i ons wi l l work on al l

141

Page 142: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 142/193

Page 143: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 143/193

Language i n t he UMAX V Pr ogrammer ' s Gui de. The- q extensi on=mi cr ot aski ng opt i on speci f i es t hatext ensi ons whi ch support mi cr otaski ng arer ecogni zed. Thi s i ncl udes t he- q ext ensi on=par al l el ext ensi ons, and al sospeci f i es t hat t he mi crot aski ng l i br ar y and anal t er nate versi on of cr t 0. o ar e t o be used byt he l oad st ep. The - q extensi ons opt i on i sequi val ent t o - q ext ensi on=mi cr otaski ng. Thedef aul t opt i on i s - q noext ensi ons.

l i mi t f regs, nol i mi t f regsUse or don' t use t he new NS32532 doubl epr eci s i on f l oat i ng poi nt r egi sters f 1, f 3, f 5,f 7. Thi s f l ag i s val i d onl y i n conj uncti on wi t ht he - q xpc f l ag. The def aul t val ue f or t hi sf l ag i s - q l i mi t f r egs ( t he new r egi st er s ar enot used) . The doubl e pr eci si on regi st er s f 1,f 3, f 5, f 7 do not exi st on APCs and DPCs, andcode t hat uses t hese regi st er s wi l l not wor k on

APCs and DPCs.

l ong_j ump, nol ong_j ump

- q nol ong_j ump, al l ows branches t o span up t o

i ncl udes, noi ncl udesLook or don' t l ook f or C l anguage i ncl ude f i l esi n the standar d di r ectory / usr / i ncl ude.- q noi ncl udes speci f i es t her e i s no standar dl ocat i on f or t he i ncl ude f i l es. The def aul tval ue i s - q i ncl udes.

l ong_case, nol ong_caseEnabl e or di sabl e the generat i on of casest at ement s usi ng a f ul l f our byte di spl acement . The - q nol ong_case opt i on i s t he def aul t ,al l owi ng case st atement s t o span 8 Ki l obytes. The - q l ong_case opt i on al l ows case st at ement st o span 16 Megabyt es. Thi s shoul d onl y beneeded i n unusual ci r cumst ances.

Enabl e or di sabl e t he generat i on of j umps wi t hf our byt e di spl acement s when t he assembl er i sunabl e t o r esol ve t hem i n 1 byt e. Thi s opt i ononl y has ef f ect when di r ect code gener at i on i snot enabl ed. The def aul t opt i on,

 _8 Ki l obyt es. The - q l ong_j ump opt i on wi l lal l ow branches t o span up t o _16 Megabyt es.

l oops, nol oopsEnabl e or di sabl e l oop opt i mi zati ons. Theseopt i mi zat i ons i ncl ude l oop- i nvar i ant hoi st i ngand st r engt h r educt i on. The def aul t opt i on i s- q nol oops.

opt i mi ze, noopt i mi zeopt i mi ze=none, opt i mi ze=st andar d, opt i mi ze=t i me, opt i mi ze=space

Speci f y t he l evel of opt i mi zat i on. The

143

Page 144: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 144/193

- q opt i mi ze opt i on i s equi val ent t o t he- q opt i mi ze=st andard. The - q noopt i mi ze opt i oni s equi val ent t o - q opt i mi ze=none. The - Oopt i on i s equi val ent t o - q opt i mi ze=st andar d. The - q opt i mi ze=st andar d opt i on enabl es a setof opt i mi zat i ons t hat do not t ake an excessi vet i me to generate and do not over l y f avor spaceover t i me or vi ce ver sa. The - q opt i mi ze=t i meopt i on enabl es opt i mi zat i ons whi ch may t ake

obj ect f i l es bui l t wi t h t he - q r eg_par ams

t ype as t he smal l est pr edef i ned t ype that can

l onger t o recogni ze but shoul d yi el d a pr ogr amt hat t akes mi ni mal t i me. Thi s opt i on enabl es- q al i gn_t ext , - q l oops, and - q novol at i l e. I f  any of t hese opt i ons are i nappr opr i ate, t heymay be over r i dden by t he appropr i at e - q noxxxopt i on. The - q opt i mi ze=space opt i on enabl esopt i mi zat i ons whi ch may t ake l onger t o generat ebut shoul d yi el d a pr ogr am whi ch t akes mi ni malspace. Thi s opt i on enabl es- q pr el oad_const ant s and - q t ai l _mer ge. Thedef aul t opt i on i s - q opt i mi ze=none.

pr el oad_const ant s, nopr el oad_const ant sEnabl e or di sabl e the l i nki ng of const antval ues and addr esses that ar e f r equent l yr ef er enced i n t he sour ce code at t he start of apr ogr am. Thi s opt i on saves space; i t may saveexecut i on t i me i f t he const ant s and addr essesar e al so r ef er enced f r equent l y dur i ngexecut i on. The - q nopr el oad_const ant s opt i on i st he def aul t ; t he - q pr el oad_const ant s opt i on i senabl ed by t he - O opt i on.

r eg_params, nor eg_paramsPass t he f i r st t wo paramet er s t o a subr out i nei n r egi st er s rat her t han on t he st ack. The- q nor eg_par ams opt i on i s t he def aul t . Thest andar d l i br ar i es pr ovi ded wi t h t he syst emassume - q nor eg_par ams and wi l l not work wi t h

opt i on.

sbf i xed, nosbf i xedEnabl e or di sabl e t he use of t he NS32000 sbr egi st er when generat i ng i mmedi ate addr esses. The - q sbf i xed opt i on i s t he def aul t .

si gned_bi t _f i el ds, nosi gned_bi t _f i el ds

Enabl e or di sabl e maki ng bi t f i el ds i nst r uct ur es of t ype i nt , shor t , and char t o besi gned. The def aul t opt i on,- q nosi gned_bi t _f i el ds, i s t o make al l f i el dsunsi gned.

smal l _enums, nosmal l _enumsEnabl e or di sabl e the al l ocat i on of each enum

r epr esent al l of t he val ues t hat ar e l i st ed

144

Page 145: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 145/193

( t hat i s val ues of t ype char , shor t , i nt ,unsi gned char , unsi gned shor t , or unsi gned t hatare used i n the enum st atement ) . The def aul topt i on, - q nosmal l _enums, al l ocat es an enumt ype as an i nt .

st andar d_l i br ar y, nost andar d_l i br ar yAl l ows t he compi l er t o r epl ace cal l s t ost andar d l i bc rout i nes wi t h equi val ent i n- l i necode. The def aul t opt i on i s- q nost andar d_l i br ar y, unl ess t he- q opt i mi ze=t i me opt i on i s speci f i ed.

t ai l _mer ge, not ai l _mer geEnabl e or di sabl e br anch- t ai l mer gi ng, anopt i mi zat i on whi ch r educes code si ze by shari ng

f i l e. o obj ect f i l e

common port i ons of t hen and el se cl auses or of case swi t ches. The - q t ai l _merge opt i on i senabl ed by def aul t , and di sabl ed when - O i sspeci f i ed.

vol at i l e, novol at i l eDi sabl e or enabl e addi t i onal opt i mi zat i on ont he assumpt i on t hat memory never changes exceptas t he r esul t of expl i ci t st or e oper at i ons. Thedef aul t opt i on, - q vol at i l e, di sabl es theseopt i mi zat i ons. The - q novol at i l e opt i on shoul dbe used when al l var i abl es t hat can be modi f i edasynchr onousl y ( e. g. , by si gnal handl er s) havet ype vol at i l e. Asynchr onous modi f i cat i on coul dhappen, f or exampl e, wi t h si gnal s, devi cedr i ver s, and par al l el pr ocesses accessi ngshar ed memory. The curr ent def aul t i s- q novol at i l e. I n t he f ut ur e, t he goal i s t ohave - q vol at i l e t he def aul t val ue.

FI LESf i l e. c i nput f i l e

a. out l i nked out put/ l i b/ ccom compi l er/ l i b/ occom backup compi l er/ l i b/ crt 0. o runt i me start of f  / l i b/ mcrt 0. o star t of f f or prof i l i ng/ l i b/ l i bc. a standard l i brary, see i nt ro(3)/ us r / l i bp/ l i b* . a prof i l i ng l i brar i es , see i nt r o(3)/ usr / i ncl ude st andar d di r ectory f or #i ncl ude f i l es

mon. out f i l e pr oduced f or anal ysi s by pr of ( 1)

SEE ALSOadb( 1) , as( 1) , cdb( 1) , gpr of ( 1) , l d( 1) , pr of ( 1) , sdb( 1) ,a. out ( 4) , moni t or ( 3C) .cf l ow( 1) i n t he UMAX V User ' s Ref erence Manual ."C Language" and "Compi l er and C Language" i n t he UMAX Vcf l ow( 1) i n t he UMAX V User ' s Ref erence Manual ."C Language" and "Compi l er and C Language" i n t he UMAX VPr ogr ammer ' s Gui de.

145

Page 146: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 146/193

B. W. Kerni ghan and D. M. Ri t chi e, The C Progr ammi ngLanguage. Pr ent i ce- Hal l , 1978.

DI AGNOSTI CS The di agnost i cs produced by C i t sel f ar e i nt ended t o besel f - expl anatory. Occasi onal messages may be pr oduced byt he assembl er or l i nk edi t or .

NOTESÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ

NOTESÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜAPPENDI X F - f 77

$man f 77

NAMEf 77 - For t r an- 77 compi l er

SYNOPSI Sf 77 [ opt i ons ] f i l e [ opt i ons ] [ f i l es ] . . .

DESCRI PTI ON The f 77 compi l er i s an advanced, opt i mi zi ng For t r an- 77compi l er t hat accept s a compl ete i mpl ement at i on of t hest andar d Fort r an l anguage def i ned by ANSI st andard X3. 9-1978. I t al so has ext ensi ons to support VAX For t r anf unct i onal i t y and paral l el pr ogr ammi ng. The For t r an- 77compi l er accept s any or none of t he opt i ons descr i bedf ol l owi ng, and one or mor e i nput f i l e names. Fi l es andopt i ons can be mi xed i n any order . Any di f f erences bet ween4. 2 and V are noted i n t he t ext .

Fi l es t hat have an f or F extensi on ar e taken t o beFort r an- 77 l anguage sour ce pr ogr ams. The compi l er pr ocessesever y For t r an- 77 sour ce f i l e t o pr oduce a cor r espondi ngobj ect f i l e wi t h t he same f i l e name and an o extensi on.Sour ce f i l es t hat have an F ext ensi on are passed t hr ough t heC l anguage macr o pr epr ocessor bef ore bei ng compi l ed by t hef 77 compi l er. Fi l es t hat have an e ext ensi on are assumed t obe EFL ( Ext ended For t r an Language) f i l es, whi ch are passedt hr ough t he ef l pr epr ocessor bef ore bei ng compi l ed by t heFor t r an- 77 compi l er . Fi l es t hat have an r ext ensi on ar et aken t o be Rat f or f i l es and passed t hr ough t he rat f orpr epr ocessor bef ore bei ng compi l ed. Fi l es t hat have an sextensi on ar e assumed to be assembl y l anguage sourceprogr ams. These are assembl ed t o produce a cor r espondi ngobj ect f i l e wi t h t he same f i l e name and an o extensi on.

Fi l es wi t h ext ensi ons ot her t han f , F, e, r , and s ar eassumed t o be For t r an- compat i bl e l i br ar i es, or obj ect f i l essuch as t hose f i l es produced by an ear l i er compi l at i on orassembl y. These f i l es, t oget her wi t h any obj ect codepr oduced dur i ng t he compi l at i on, are l oaded t o pr oduce anexecut abl e pr ogr amf i l e named aout .

I f onl y one i nput f i l e wi t h an f , F, e, r , or s ext ensi on i ssuppl i ed, t he compi l er aut omat i cal l y del et es t he obj ect f i l e

146

Page 147: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 147/193

out put pr oduced f r om t hat i nput f i l e af t er execut abl epr ogr am f i l e aout has been cr eat ed.

Al l unr ecogni zed opt i ons and al l f i l e names wi t h extensi onsot her t han . f , . F, . e, . r , . c ar e passed t o t he l oader . Forassembl er opt i ons, see as( 1) ; f or l oader opt i ons, see l d( 1) . The f 77 opt i ons ar e:

- Bpr ef i x Run t he compi l er pr ogr am cont ai ned i n f i l epr ef i xf com. I f pr ef i x i s not gi ven,/ usr / l i b/ of com i s the def aul t compi l er used.

- c Compi l e onl y. Pr oduce obj ect f i l e out put ( even i f t here was onl y one sour ce f i l e) and do not l oadt he pr ogr am af t er compi l i ng i t .

- Dname=def Def i ne symbol name to be st r i ng def , when r unni ngt he C l anguage pr epr ocessor, as i f by a #def i nest atement . I f =def i s omi t t ed, def i nes name t o be

1 whi l e r unni ng t he C pr epr ocessor .

- Est r i ng Pass opt i on( s) st r i ng t o t he ef l pr epr ocessor whenpr ocessi ng i nput f i l es t hat have t he e extensi on.

- F Gener at e onl y For t r an l anguage out put f r om t heratf or or ef l prepr ocessor, pl aci ng i t i n a f i l et hat has t he sour ce f i l e name and the f ext ensi on,but do not r un the For t r an- 77 compi l er.

- g Generate speci al symbol t abl e data f or t he sdb( 1)debugger ( or t he opt i onal debugger) , and pass t he- l g f l ag t o t he l oader .

- I pat h I ncl ude sour ce f i l es f r om t he di r ect or y named pat hwhen r unni ng t he C l anguage prepr ocessor . Whencompi l i ng sour ce f i l es named wi t h t he F ext ensi on,search f or #i ncl ude f i l es ( whose names do notbegi n wi t h / ) f i r st i n t he di r ector y cont ai ni ngt he sour ce f i l e, t hen i n t he di r ect or y pat h, andt hen i n a l i st of st andar d def aul t s. Mul t i pl e - Iopt i ons can est abl i sh a hi er ar chy of #i ncl ude f i l edi rect or i es.

- i 2 Make t he def aul t l engt h of i nt eger const ant s andvar i abl es, and al l l ogi cal quant i t i es, be shor t .Compl ement ar y opt i on - i 4 i s t he def aul t , whi ch

cal l s f or l ong i nt eger var i abl es and const ant s.

- m Appl y t he M4 macr o prepr ocessor t o each EFL orRat f or sour ce f i l e bef or e passi ng i t t hr ough t heef l or r at f or pr epr ocessor .

- O Per f orm opt i mi zat i ons t hat speed up t he generat edcode; al so per f or m any space opt i mi zat i ons t hat donot i mpact code speed. See al so t he - q qual i f i eropt i ons.

147

Page 148: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 148/193

 - o out put Name the f i nal , execut abl e out put f i l e out put

rather than aout.

- onet r i p Generate obj ect code t hat execut es t he r ange of ever y do l oop at l east once, even i f t he i ni t i alval ue of t he l oop i ndex exceeds t he l i mi t val ue.

- p Pr epare t o generat e an execut i on pr of i l e usi ngpr of ( 1) . I ncl ude speci al pr of i l i ng code t hatcount s how many t i mes each r out i ne i s cal l ed. I f l oadi ng occur s, use a speci al st ar t up r out i ne thatcal l s moni t or( 3) and pr oduces a monout f i l e upont er mi nat i on. Use a speci al pr of i l i ng l i br ar yi nst ead of t he standar d C l i br ar y.

- pg Gener at e an execut i on pr of i l e usi ng gpr of .I ncl ude speci al pr of i l i ng code t hat count s howmany t i mes each r out i ne i s cal l ed. I f l oadi ngoccur s, use a speci al st ar t up r out i ne t hat cal l s

moni t or ( 3) and pr oduces one or mor e gmon. pi d upont er mi nat i on. A pr of i l i ng ver si on of t he st andar dl i br ar y i s used.

- R Make i ni t i al i zed var i abl es shar ed and r ead- onl y( by passi ng t he - r opt i on t o the assembl er ) .

- Rst r i ng Pass opt i on( s) st r i ng t o t he r at f or pr epr ocessorwhen pr ocessi ng i nput f i l es t hat have an rext ensi on.

- S Generate assembl y l anguage out put f or each sour cef i l e, but do not assembl e i t . Assembl er out putf or a sour ce f i l e wi t h t he ext ensi on f , F, e, r ,or c i s put i n a f i l e wi t h t he same name and a sext ensi on.

- U Do not conver t uppercase l et t ers t o l owercasel et t er s. By def aul t For t r an pr ogr ams ar econver t ed t o l ower case l et t er s except wi t hi nchar act er st r i ng const ant s.

- u Di sabl e aut omat i c data t ypi ng and, i nst ead, maket he def aul t t ype of a vari abl e the undef i ned t ype.

- v Repor t t he names of al l subpr ocesses i nvoked byt he compi l er and t hei r argument s.

- w Suppr ess warni ng di agnost i cs.

- w66 Recogni zed onl y f or compat i bi l i t y wi t h t hePor t abl e For t r an- 77 Compi l er , whi ch used t hi sopt i on t o suppr ess warni ngs about For t r an- 66f eat ur es encount ered dur i ng compi l at i on. TheFor t r an- 77 compi l er does not f l ag l anguageel ement s t hat are uni que to Fort r an- 66.

148

Page 149: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 149/193

- W[ a c l ] , ar gPass opt i on ar g to t he assembl er , compi l er , orl i nker , as speci f i ed r especti vel y by - Wa, ar g,- Wc, ar g, or - Wl , ar g. The i nt er nal opt i ons f or

appr opr i at e f or t he machi ne on whi ch t he

t he obsol eted swi t ch combi nat i on,

t he f 77 compi l er i ncl ude i mpl ement at i on opt i onsused t o reconf i gur e the compi l er f or al i enoperat i ng envi r onment s, and debuggi ng opt i ons usedf or t est i ng compi l er sof t war e. These opt i onsshoul d never be used i n normal operat i on; t hey aredescr i bed i n t he Fort r an- 77 Manual .

- q qual i f i er [ =ar g] The qual i f i er opt i ons provi de mor e detai l edcont r ol over t he generat ed code and act i on of t hecompi l er . They modi f y t he generat ed code of t hecompi l er t o ref l ect var i ous speci al r equi r ement sof a pr ogr am, and i n general shoul d onl y be usedf or speci al si t uat i ons. The qual i f i er opt i onsdeal wi t h ar chi t ecture, opt i mi zat i on sel ecti ons,f i l e conf i gur at i on, and For t r an l anguage

ext ensi ons. I n t hi s l i st i ng t hey ar e gr ouped bycat egory. Both t he qual i f i ers and any argument s,whi ch have compi l er - def i ned val ues, can beabbr evi ated t o thei r mi ni mum number of uni quechar acters. The qual i f i er s ar e:

por t abl eapc, apc01, apc02, dpc, xpc[ , 2ar g] , host _i s_t ar get ,

 These qual i f i er s sel ect gener at i on of codet hat i s compat i bl e wi t h Mul t i max syst emshavi ng APC DPC or XPC ( Nat i onalSemi conduct or NS32xxx- based) processorboar ds. The def aul t i s t o gener ate code

compi l er i s r unni ng. ( Di f f erences betweengener at ed APC and DPC code ar e pr i mar i l y i nal i gnment opt i mi zati on. )

apc The apc qual i f i er sel ects APC01 codeand t he l i bm_apc. a math l i br ary.

apc01 The apc01 qual i f i er i s t he same as t heapc qual i f i er . I t i s equi val ent to

- q apc - q nof pa.

apc02 The apc02 qual i f i er sel ect s APC02 code

( wi t h Cone i nst r uct i ons) and uses t hel i bm_f pa. a mat h l i br ar y. Thi s i sequi val ent t o t he obsol et ed swi t chcombi nat i on, - q apc - q f pa.

dpc The dpc qual i f i er sel ect s codeopt i mi zed f or a DPC syst em, and usest he l i bm_apc. a l i br ar y.

xpc[ , ar g]

149

Page 150: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 150/193

The xpc qual i f i er generat es codeopt i mi zed f or XPC syst ems, usi ng thel i bm_xpc. a mat h l i br ar y. Si nce xpcper mi t s access of 4 addi t i onalf l oat i ng poi nt ( f p) r egi st er s and usesf l oat i ng poi nt i nstr ucti ons t hat donot exi st f or APC and DPC boards, codecompi l ed usi ng t hi s opt i on may not bepor t abl e t o APC and DPC sys t ems. xpcaccept s t he ar gument s l i mi t f r egs andnol i mi t f regs. - q xpc, l i mi t f regsassures code compat i bi l i t y wi t h APCand DPC syst ems, sel ect i ng thel i bm_apc. a mat h l i br ar y r at her t hanl i bm_xpc. a and suppr essi ng the usageof some doubl e- pr eci si on f l oat i ngpoi nt r egi st er s that are avai l abl e t oXPC syst ems; onl y 4 doubl e- pr eci si onf l oat r egi st er s ar e used.- q xpc, nol i mi t f r egs per mi t s al l

f l oat i ng poi nt r egi st er s t o be used,and uses t he l i bm_xpc. a math l i br ary.

compi l er _r egi st er s, nocompi l er _r egi st er s

host _i s_t ar get The host _i s_t ar get qual i f i er opt i mi zescode f or t he syst em perf or mi ng thecompi l at i on. No at t empt i s made t opr eser ve por t abi l i t y. Thi s i s def aul tbehavi or .

por t abl e The por t abl e qual i f i er gener at es codet hat i s por t abl e across al l Mul t i maxAPC, DPC, and XPC syst ems. Auni ver sal mat h l i br ar y, l i bm_apc. a, i sused. Onl y opt i mi zat i ons t hat ar eexpl i ci t l y por t abl e ar e used.Produced code i s port abl e t o APC andDPC sys t ems even i f compi l ed on an XPCsyst em, si nce onl y 4 doubl e- pr eci si onf l oat r egi st er s ar e used.

al i gn_t ext , noal i gn_t extEnabl e or di sabl e al i gnment of t ext segment son boundar i es t o opt i mi ze burst mode onMul t i max syst ems havi ng APC s. The def aul ti s noal i gn_t ext , unl ess opt i mi ze=t i me i s

enabl ed.

asmdi r =pr ef i xUse the assembl er l ocated i n t he pr ef i xasf i l e i nst ead of t he def aul t assembl er ,/ bi n/ as.

Enabl e or di sabl e compi l er al l ocat i on of l ocal var i abl es t o r egi st er s beyond t hose

150

Page 151: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 151/193

speci f i ed by regi st er st or age cl assspeci f i cat i ons. The def aul t i scompi l er _r egi st er s. nocompi l er _r egi st er sshoul d onl y be used when code i s wr i t t en t odepend on the exi st ence of non- r egi st ercl ass var i abl es i n memory.

crt 0di r =pr ef i xUse t he pr ef i xcrt 0. o st ar t up f i l e i nst ead of the def aul t s tar t up f i l e, / l i b/ cr t 0. o.

d_l i nes, nod_l i nesEnabl e or di sabl e t he r ecogni t i on of anycomment l i ne, begi nni ng wi t h a D, as a codel i ne. The def aul t i s nod_l i nes.

di r ect _code, nodi r ect _codeEnabl e or di sabl e t he di r ect generat i on of code by t he compi l er . When enabl ed, t hecompi l er di r ect l y gener at es obj ect code,

bypassi ng the i nt ermedi ate st eps of pr oduci ng assembl y code and assembl i ng i t t opr oduce t he obj ect code. The nodi r ect_codequal i f i er shoul d onl y be needed i f t hesour ce f i l e cont ai ns asm st at ement s.di r ect _code i s enabl ed by def aul t .nodi r ect _code i s enabl ed i f t he - R opt i on i s

- q ext ensi ons qual i f i er i s

speci f i ed.

ext ensi ons[ =arg] , noextensi onsEnabl e or di sabl e t he speci f i cat i on of For t r an ext ensi ons. The def aul t qual i f i eri s noextensi ons. The avai l abl e argument sare:

berkel ey_f 77 Support s t he st andar d UNI Xf 77. Thi s i s equi val ent tonoext ensi ons.

ext ended_f 77 Support s an ext ensi on t o f 77t hat al l ows Fort r an pr ogr amswr i t t en f or VAX/ VMS t o becompi l ed on Mul t i max syst ems. Thi s i s t he def aul t when t he

gi ven wi t hout an argument .

par al l el Recogni zes t he extensi onst hat suppor t par al l elpr ogr ammi ng, i ncl udi ng shar edmemory decl arat i ons andspi nl ocks i n- l i ne. Thi s doesnot change t he val ue of anear l i er speci f i edberkel ey_f 77 or ext ended_f 77sel ecti on.

151

Page 152: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 152/193

l ddi r =pr ef i xUse t he l i nk edi t or i n pr ef i xl d i nst ead of t he def aul t , / bi n/ l d.

l ong_case, nol ong_caseEnabl e or di sabl e t he gener ati on of casest at ement s usi ng a f ul l f our - byt edi spl acement . nol ong_case i s t he def aul t ,al l owi ng case st at ement s t o span 4Ki l obytes. l ong_case al l ows case st atement st o span 2 Megabyt es. Thi s shoul d onl y beneeded i n unusual ci r cumst ances.

l ong_j ump, nol ong_j umpEnabl e or di sabl e the generat i on of j umpswi t h f our - byt e di spl acement s when t heassembl er i s unabl e to r esol ve t hem i n onebyte. The def aul t , nol ong_j ump, al l owsbr anches t o span up to _8 Ki l obyt es.l ong_j ump al l ows br anches t o span up t o _16

Megabytes. Di r ect code generat i on sel ect sone- , t wo- , or f our - byte di spl acement asappr opr i at e, r egar dl ess of t he set t i ng of t hi s opt i on.

l oops, nol oopsEnabl e or di sabl e l oop opt i mi zat i ons. Theseopt i mi zat i ons i ncl ude l oop- i nvar i anthoi st i ng and st r engt h r educt i on. Thedef aul t i s nol oops.

opt i mi ze[ =ar g] , noopt i mi zeEnabl e or di sabl e di f f er ent l evel s of opt i mi zat i on. The def aul t i s opt i mi ze=none. The avai l abl e ar guments ar e:

none Enabl e no speci al opt i mi zat i ons.none i s equi val ent t o noopt i mi ze.

space Enabl e opt i mi zat i ons whi ch mayt ake l onger t o generat e but whi chshoul d pr oduce a pr ogr am t hatr equi r es mi ni mal space. Thi sargument al so enabl espr el oad_const ant s and t ai l _merge.

st andar d Enabl e a set of opt i mi zat i ons

t hat do not t ake an excessi veamount of t i me t o gener at e andwhi ch do not f avor space overt i me ( or vi ce ver sa) .

t i me Enabl e opt i mi zat i ons whi ch mayt ake l onger t o recogni ze butwhi ch shoul d pr oduce a pr ogr amt hat r equi r es mi ni mal execut i ont i me. Thi s argument al so enabl esal i gn_t ext , l oops, and

152

Page 153: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 153/193

novol at i l e.

pr el oad_const ant s, nopr el oad_const ant sEnabl e or di sabl e t he l oadi ng of const antval ues and addr esses that ar e f r equent l yr ef erenced i n t he sour ce code at t he st artof a pr ogr am. Thi s opt i on saves space; i t

Enabl e or di sabl e br anch- t ai l mer gi ng, an

t he assumpt i on that memor y never changes

when i t i s cl ear t hat no var i abl es can bemodi f i ed asynchr onousl y. Asynchr onous

si gnal s, devi ce dr i ver s, or par al l el

 The - q f l ag and i t s qual i f i er opt i ons r epl ace t he f ol l owi ng

- J Repl aced by - q l ong_j ump.

may save execut i on t i me i f t he const ant s andaddr esses ar e al so r ef erenced f r equent l ydur i ng execut i on. no_prel oad_const ant s i st he def aul t ; pr el oad_const ant s i s enabl ed byt he - O opt i on.

s i ngl e_ l i b, nos i ngl e_ l i bEnabl e or di sabl e the use of si ngl epr eci si on mat h r out i nes f or cer t ai n bui l t - i nf unct i ons when t he f unct i ons are cal l ed wi t hsi ngl e pr eci si on argument s. The si ngl epreci s i on ver si ons of f er s i gni f i cant l yi ncr eased speed wi t h al most no reduct i on i n

accur acy. si ngl e_l i b i s enabl ed by def aul t .

t ai l _mer ge, not ai l _mer ge

opt i mi zat i on that r educes code si ze byshar i ng common por t i ons of t hen and el secl auses or of case swi t ches. t ai l _mer ge i sdi sabl ed by def aul t .

vol at i l e, novol at i l eEnabl e or di sabl e addi t i onal opt i mi zat i on on

except as t he r esul t of expl i ci t st or eoper at i ons. The def aul t i s vol at i l e, unl essopt i mi ze=t i me i s sel ect ed. novol at i l e,whi ch enabl es t he opt i mi zat i ons, i savai l abl e onl y when opt i mi ze=t i me i ssel ect ed. novol at i l e shoul d onl y be used

modi f i cat i on coul d happen, f or exampl e, wi t h

processes accessi ng shar ed memory.

RESTRI CTI ONS

opt i ons, whi ch are no l onger support ed:

- A Repl aced by - q nodi r ect _code.

- G Repl aced by - q di r ect _code.

- H Repl aced by - q not ai l _merge.

- T Repl aced by - q l oops.

153

Page 154: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 154/193

 - V Repl aced by - q novol ati l e.

FI LES. / f or t [ pi d] . ? t empor ar y f or t r an pr ocess f i l esa. out l oaded out put f i l ef i l e. [ f Fresc] i nput f i l ef i l e. o obj ect f i l e

gmon. [ pi d] f i l e pr oduced f or anal ysi s by moni t or ( 3)mon. out f i l e pr oduced f or anal ysi s by pr of ( 1)/ l i b/ cpp C pr epr ocessor/ l i b/ l i bc. a C l i brary/ l i b/ cpp C pr epr ocessor/ l i b/ l i bc. a C l i brary/ usr/ l i b/ f com For t r an compi l er/ usr / l i b/ l i bFBERK. a combi ned l i bF77. a, l i bI 77. a, and

l i bU77. a l i brary/ usr / l i b/ l i bFBERK_p. a pr of i l i ng combi ned Ber kel ey f uncti on

/ usr / l i b/ l i bm_f pa. a mat h l i br ar y f or APC02 systems wi t h

SEE ALSO

ef l (1F) , f pr ( 1F) f spl i t ( 1F) r at f or ( 1F) , s t ruct ( 1F) ,

ANSI X3. 9- 1978.

NOTESÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ

NAME

l i n t [ o p t i o n ] . . . f i l e . . .

t he t hi ngs t hat ar e cur r ent l y det ect ed ar e unr eachabl e

l i brary/ usr / l i b/ l i bFORT. a combi ned l i bFBERK. a and l i bX77. a

l i brary/ usr / l i b/ l i bFORT_p. a pr of i l i ng combi ned ext ended Ber kel eyf uncti on

/ usr / l i b/ l i bm_apc. a st andar d NS32081 code mat h l i br ar y

Cone pr ocessor/ usr / l i b/ l i bm_xpc. a XPC syst em mat h l i br ar y ( 8 f l oat -

r egi st er, NS32381)

as(1) , cc( 1) , l d( 1) , m4( 1) , pr of ( 1) , sdb( 1) , cdb( 1X) ,

i nt r o( 3F) epf ( 9F) ,Fort r an- 77 Manual .

Amer i can Nat i onal Standar d Progr ammi ng Language For t r an,

NOTESÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ

APPENDI X G - l i nt

$man l i nt

l i nt - a C pr ogr am checker

SYNOPSI S

DESCRI PTI ONl i nt at t empt s t o det ect f eat ur es of t he C pr ogr am f i l es t hatar e l i kel y t o be bugs, non- por t abl e, or wast ef ul . I t al sochecks type usage more st r i ct l y t han t he compi l ers. Among

st atement s, l oops not ent ered at t he t op, aut omat i c

154

Page 155: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 155/193

var i abl es decl ared and not used, and l ogi cal expr essi ons

l i nt wi l l t ake al l t he . c, . l n, and l l i b- l x. l n ( speci f i ed by

t o var i abl es t hat ar e not l ong.

- h Do not appl y heur i st i c t est s t hat at t empt t o i nt ui t

var i abl es used and not def i ned, or def i ned and not

a subset of f i l es of a l ar ger pr ogr am) .

 Thi s ar gument does not suppress t he def aul t use of 

whose val ue i s const ant . Moreover , t he usage of f unct i onsi s checked t o f i nd f unct i ons t hat r et ur n val ues i n somepl aces and not i n ot her s, f unct i ons cal l ed wi t h var yi ngnumber s or t ypes of argument s, and f unct i ons whose val uesare not used or whose val ues ar e used but none r et urned.

Ar gument s whose names end wi t h . c ar e t aken t o be C sour cef i l es. Ar gument s whose names end wi t h . l n are t aken t o bet he r esul t of an ear l i er i nvocat i on of l i nt wi t h ei t her t he- c or t he - o opt i on used. The . l n f i l es ar e anal ogous to . o( obj ect ) f i l es t hat are pr oduced by t he cc( 1) command whengi ven a . c f i l e as i nput . Fi l es wi t h ot her suf f i xes ar ewarned about and i gnored.

- l x) f i l es and pr ocess t hem i n t hei r command l i ne or der . Bydef aul t , l i nt appends the standar d C l i nt l i br ar y ( l l i b-l c. l n) t o t he end of t he l i st of f i l es. However, i f t he - popt i on i s used, t he port abl e C l i nt l i brary ( l l i b- por t . l n)

i s appended i nst ead. When t he - c opt i on i s not used, t hesecond pass of l i nt checks t hi s l i st of f i l es f or mut ualcompati bi l i t y. When t he - c opt i on i s used, t he . l n and t hel l i b- l x. l n f i l es are i gnored.

Any number of l i nt opt i ons may be used, i n any or der ,i nt er mi xed wi t h f i l e- name ar gument s. The f ol l owi ng opt i onsare used t o suppr ess cer t ai n ki nds of compl ai nt s:

- a Suppr ess compl ai nts about assi gnment s of l ong val ues

- b Suppr ess compl ai nts about br eak st at ement s t hatcannot be r eached. ( Progr ams pr oduced by l ex( 1) oryacc(1) wi l l of t en r esul t i n many such compl ai nt s. )

bugs, i mpr ove st yl e, and r educe wast e.

- u Suppr ess compl ai nts about f unct i ons and ext ernal

used. ( Thi s opt i on i s sui t abl e f or r unni ng l i nt on

- v Suppr ess compl ai nt s about unused argument s i nf unct i ons.

- x Do not r epor t var i abl es r ef er r ed t o by exter naldecl arat i ons but never used.

 The f ol l owi ng ar gument s al t er l i nt ' s behavi or :

- l x I ncl ude addi t i onal l i nt l i brary l l i b- l x. l n. Forexampl e, a l i nt ver si on of t he Mat h Li br ar y l l i b- l m. l ncan be i ncl uded by i nser t i ng - l m on t he command l i ne.

l l i b- l c. l n. These l i nt l i brar i es must be i n t he

155

Page 156: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 156/193

assumed di r ect ory. Thi s opt i on can be used t orefer ence l ocal l i nt l i brar i es and i s usef ul i n t hedevel opment of mul t i - f i l e pr oj ect s.

- o opt i on. The l i nt l i br ar y pr oduced i s the i nput t hat

causes t hi s f i l e t o be saved i n t he named l i nt l i br ar y.

and - O opt i ons of cc ar e al so r ecogni zed as separate

al l ow cer t ai n quest i onabl e code t o be al t ered or r emoved f or

a reser ved word f or al l code that i s pl anned t o be checked

- n Do not check compat i bi l i t y agai nst ei t her t he st andar dor t he por t abl e l i nt l i brary.

- p At t empt to check por t abi l i t y t o ot her di al ect s ( I BM andGCOS) of C. Al ong wi t h st r i ct er checki ng, t hi s opt i oncauses al l non- ext ernal names t o be t r uncat ed t o ei ghtcharact ers and al l ext ernal names t o be t r uncat ed t osi x charact ers and one case.

- c Cause l i nt t o pr oduce a . l n f i l e f or ever y . c f i l e ont he command l i ne. These . l n f i l es are the pr oduct of l i nt ' s f i r st pass onl y, and ar e not checked f or i nt er -f uncti on compat i bi l i t y.

- o l i bCause l i nt t o creat e a l i nt l i br ar y wi t h t he name

l l i b- l l i b. l n. The - c opt i on nul l i f i es any use of t he

i s gi ven t o l i nt ' s second pass. The - o opt i on si mpl y

 To produce a l l i b- l l i b. l n wi t hout ext r aneous messages,use of t he - x opt i on i s suggested. The - v opt i on i susef ul i f t he source f i l e( s) f or t he l i nt l i brary are j ust ext er nal i nt er f aces ( f or exampl e, t he way t he f i l el l i b- l c i s wr i t t en) . These opt i on set t i ngs are al soavai l abl e t hr ough t he use of " l i nt comment s" ( seebel ow) .

 The - D, - U, and - I opt i ons of cc( 1) and cpp( 1) and t he - g

argument s. The - g and - O opt i ons are i gnored, but , byr ecogni zi ng t hese opt i ons, l i nt ' s behavi or i s cl oser t o t hatof t he cc command. Ot her opt i ons are warned about andi gnor ed. The pr e- pr ocessor symbol "l i nt " i s def i ned t o

l i nt . Ther ef or e, t he symbol "l i nt " shoul d be t hought of as

by l i nt .

Cert ai n convent i onal comment s i n t he C sour ce wi l l changet he behavi or of l i nt :

/ *NOTREACHED*/

at appr opr i ate poi nt s s t ops comment s about unr eachabl ecode. ( Thi s comment i s t ypi cal l y pl aced j ust af t ercal l s to f unct i ons l i ke exi t ( 2) . )

/ *VARARGSn*/suppr esses t he usual checki ng f or var i abl e numbers of ar gument s i n t he f ol l owi ng f unct i on decl ar at i on. Thedata types of t he f i r st n argument s are checked; ami ssi ng n i s t aken t o be 0.

156

Page 157: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 157/193

/ *ARGSUSED*/t ur ns on t he - v opt i on f or t he next f unct i on.

/ *LI NTLI BRARY*/

SEE ALSO

ar e not under st ood; t hi s causes var i ous l i es.

at t he begi nni ng of a f i l e shut s of f compl ai nt s aboutunused f unct i ons and f unct i on ar gument s i n t hi s f i l e. Thi s i s equi val ent t o usi ng t he - v and - x opt i ons.

l i nt pr oduces i t s f i r st out put on a per - sour ce- f i l e basi s.Compl ai nt s r egar di ng i ncl uded f i l es are col l ect ed andpr i nt ed af t er al l sour ce f i l es have been pr ocessed.Fi nal l y, i f t he - c opt i on i s not used, i nf or mat i on gat her edf r om al l i nput f i l es i s col l ected and checked f orconsi stency. At t hi s poi nt , i f i t i s not c l ear whet her acompl ai nt st ems f r om a gi ven sour ce f i l e or f r om one of i t si ncl uded f i l es, t he sour ce f i l e name wi l l be pr i nt edf ol l owed by a quest i on mark.

 The behavi or of t he - c and t he - o opt i ons al l ows f ori ncrement al use of l i nt on a set of C sour ce f i l es.

Gener al l y, one i nvokes l i nt once f or each sour ce f i l e wi t ht he - c opt i on. Each of t hese i nvocat i ons pr oduces a . l nf i l e whi ch cor r esponds t o t he . c f i l e, and pr i nt s al lmessages t hat ar e about j ust t hat sour ce f i l e. Af t er al lt he sour ce f i l es have been separ at el y run t hr ough l i nt , i ti s i nvoked once mor e ( wi t hout t he - c opt i on) , l i st i ng al lt he . l n f i l es wi t h t he needed - l x opt i ons. Thi s wi l l pr i ntal l t he i nt er - f i l e i nconsi st enci es. Thi s scheme wor ks wel lwi t h make( 1) ; i t al l ows make t o be used t o l i nt onl y t hesour ce f i l es t hat have been modi f i ed si nce t he l ast t i me t heset of sour ce f i l es wer e l i nt ed.

FI LES/ usr/ l i b/ l i nt [ 12] f i r st and second passes/ us r / l i b/ l l i b- l c. l n decl arat i ons f or C Li brary

f unct i ons ( bi nar y f or mat ; sour ce i si n / u s r / l i b / l l i b - l c )

/ usr/ l i b/ l l i b- port . l n decl arat i ons f or port abl e f unct i ons( bi nar y f or mat ; sour ce i s i n/ u s r / l i b / l l i b - p o r t )

/ usr/ l i b/ l l i b- l m. l n decl arat i ons f or Mat h Li braryf unct i ons ( bi nar y f or mat ; sour ce i si n / usr / l i b/ l l i b- l m. l n)

/ usr / t mp/ *l i nt * t empor ar i es

cc( 1) , cpp( 1) , l ex(1) , make( 1) , yacc(1) , t mpnam( 3S) .

BUGSexi t ( 2) , l ongj mp( 3C) , and ot her f unct i ons t hat do not r et ur n

APPENDI X H - cb

$man cb

NAMEcb - C pr ogr am beaut i f i er

157

Page 158: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 158/193

 SYNOPSI S

c b [ - s ] [ - j ] [ - l l e n g ] [ f i l e . . . ]

DESCRI PTI ON The cb comand r eads C programs ei t her f r om i t s ar gument s orf r om t he st andard i nput , and wr i t es them on t he st andar d

l eng.

t abl e i s onl y creat ed and mai nt ai ned by ar when t here i s at

out put wi t h spaci ng and i ndent at i on t hat di spl ay t hest r uct ur e of t he code. Under def aul t opt i ons, cb pr eser vesal l user new- l i nes.

cb accept s t he f ol l owi ng opt i ons.

- s Canoni cal i zes t he code t o t he st yl e of Ker ni ghanand Ri t chi e i n The C Programmi ng Language.

- j Causes spl i t l i nes t o be put back t oget her .

- l l eng Causes cb t o spl i t l i nes t hat ar e l onger t han

SEE ALSOcc(1). The C Pr ogrammi ng Language. Pr ent i ce- Hal l , 1978.

BUGSPunct uat i on t hat i s hi dden i n pr epr ocessor st at ement s wi l lcause i ndent at i on er r or s.

NOTESÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜAPPENDI X I - ar

$man ar

NAMEar - ar chi ve and l i br ar y mai nt ai ner f or por t abl e ar chi ves

SYNOPSI Sar key [ posname ] af i l e [ name ] . . .

DESCRI PTI ON The ar command mai nt ai ns groups of f i l es combi ned i nt o asi ngl e ar chi ve f i l e. I t s mai n use i s to creat e and updat el i br ar y f i l es as used by t he l i nk edi t or . I t can be used,t hough, f or any si mi l ar pur pose. The magi c st r i ng and t hef i l e header s used by ar consi st of pr i nt abl e ASCI Ichar act er s. I f an ar chi ve i s composed of pri nt abl e f i l es,

t he ent i r e ar chi ve i s pr i nt abl e.

When ar cr eat es an archi ve, i t cr eat es headers i n a f ormatt hat i s por t abl e across al l machi nes. The por t abl e ar chi vef or mat and st r uct ur e i s descr i bed i n det ai l i n ar ( 4) . Thearchi ve symbol t abl e ( descr i bed i n ar ( 4) ) i s used by thel i nk edi t or ( l d( 1) ) t o ef f ect mul t i pl e passes over l i br ar i esof obj ect f i l es i n an ef f i ci ent manner . An ar chi ve symbol

l east one obj ect f i l e i n t he ar chi ve. The archi ve symbol

158

Page 159: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 159/193

t abl e i s i n a speci al l y named f i l e whi ch i s al ways t he f i r stf i l e i n t he ar chi ve. Thi s f i l e i s never ment i oned oraccessi bl e t o t he user . Whenever t he ar command i s used t ocr eat e or updat e the cont ent s of such an archi ve, t he symbolt abl e i s rebui l t . The s opt i on descri bed bel ow wi l l f or cet he symbol t abl e to be rebui l t . The symbol t abl e hol ds amaxi mum of 20, 000 symbol s.

Unl i ke command opt i ons, t he command key i s a r equi r ed par tof ar ' s command l i ne. The key ( whi ch may begi n wi t h a - ) i sf or med wi t h one of t he f ol l owi ng l et t er s: dr qt pmx.Ar gument s t o t he key, al t ernat i vel y, are made wi t h one of mor e of t he f ol l owi ng set : vuai bcl s. posname i s an ar chi vemember name used as a r ef erence poi nt i n posi t i oni ng ot herf i l es i n t he ar chi ve. af i l e i s t he ar chi ve f i l e. The namesar e const i t uent f i l es i n t he ar chi ve f i l e. The meani ngs of t he key char act er s are as f ol l ows:

d Del et e t he named f i l es f r om t he ar chi ve f i l e.

r Repl ace t he named f i l es i n t he ar chi ve f i l e. I f theopt i onal char act er u i s used wi t h r , t hen onl y t hosef i l es wi t h dat es of modi f i cat i on l at er t han t he ar chi vef i l es ar e r epl aced. I f an opt i onal posi t i oni ngchar act er f r om t he set ai b i s used, t hen t he posnameargument must be pr esent and speci f i es t hat new f i l esar e t o be pl aced af t er ( a) or bef ore ( b or i ) posname.Ot herwi se new f i l es ar e pl aced at t he end.

q Qui ckl y append t he named f i l es t o t he end of t hear chi ve f i l e. Opt i onal posi t i oni ng char acters ar ei nval i d. The command does not check whether t he addedmembers are al r eady i n t he archi ve. Thi s opt i on i susef ul t o avoi d quadr at i c behavi or when cr eat i ng al ar ge ar chi ve pi ece- by- pi ece. Unchecked, t he f i l e maygr ow exponent i al l y up to t he second degr ee.

t Pr i nt a t abl e of cont ent s of the ar chi ve f i l e. I f nonames ar e gi ven, al l f i l es i n t he ar chi ve ar e t abl ed.I f names are gi ven, onl y t hose f i l es are t abl ed.

p Pr i nt t he named f i l es i n t he ar chi ve.

m Move t he named f i l es t o t he end of t he ar chi ve. I f aposi t i oni ng character i s present , t hen the posnameargument must be pr esent and, as i n r, speci f i es wheret he f i l es ar e t o be moved.

x Extr act t he named f i l es. I f no names are gi ven, al lf i l es i n t he ar chi ve ar e ext r acted. I n nei t her casedoes x al t er t he ar chi ve f i l e.

 The meani ngs of t he key ar guments ar e as f ol l ows:

v Gi ve a ver bose f i l e- by- f i l e descri pt i on of t he maki ngof a new ar chi ve f i l e f r om t he ol d ar chi ve and t heconst i t uent f i l es. When used wi t h t , gi ve a l ong

159

Page 160: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 160/193

l i st i ng of al l i nf or mat i on about t he f i l es. When usedwi t h x, pr ecede each f i l e wi t h a name.

c Suppr ess t he message t hat i s produced by def aul t whenaf i l e i s creat ed.

l Pl ace t empor ar y f i l es i n t he l ocal ( cur r ent wor ki ng)di r ect or y, r at her t han i n t he def aul t t empor ar ydi r ectory, / t mp.

s For ce t he r egenerat i on of the archi ve symbol t abl e eveni f ar i s not i nvoked wi t h a command whi ch wi l l modi f yt he archi ve cont ent s. Thi s command i s usef ul t or est or e t he ar chi ve symbol t abl e af t er t he st r i p( 1)command has been used on t he archi ve.

SEE ALSOl d( 1) , l or der ( 1) , st r i p( 1) , t mpnam( 3S) , a. out ( 4) , ar ( 4) ."The Common Obj ect Fi l e For mat " i n t he UMAX V Pr ogrammer ' sGui de.

BUGSI f t he same f i l e i s ment i oned t wi ce i n an ar gument l i st , i tmay be put i n t he ar chi ve t wi ce.

NAME

#def i ne ARFMAG "` \ n" / * header t r ai l er st r i ng */

ar - common archi ve f i l e f ormat

DESCRI PTI ON The ar chi ve command ar ( 1) combi nes sever al f i l es i nt o one.Ar chi ves are used mai nl y as l i br ar i es to be sear ched by t hel i nk edi tor l d( 1) .

Each archi ve begi ns wi t h t he ar chi ve magi c st r i ng:

#def i ne ARMAG " ! <arch>\ n" / * magi c st r i ng */#def i ne SARMAG 8 / * l engt h of magi c st r i ng */

Each archi ve t hat cont ai ns common obj ect f i l es ( seea. out ( 4) ) i ncl udes an ar chi ve symbol t abl e. The l i nk edi t orl d uses t he symbol t abl e t o det ermi ne whi ch ar chi ve member sEach archi ve t hat cont ai ns common obj ect f i l es ( seea. out ( 4) ) i ncl udes an ar chi ve symbol t abl e. The l i nk edi t orl d uses t he symbol t abl e t o det ermi ne whi ch ar chi ve member s

must be l oaded dur i ng t he l i nk edi t process. The archi vesymbol t abl e ( i f i t exi sts) i s al ways the f i rst f i l e i n t hear chi ve ( but i s never l i st ed) and i s aut omat i cal l y creat edand updat ed by ar .

Fol l owi ng t he ar chi ve magi c st r i ng ar e t he ar chi ve f i l emembers. Each f i l e member i s preceded by a f i l e memberheader i n t he f ol l owi ng f or mat:

160

Page 161: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 161/193

st r uct ar_hdr { / * f i l e member header */char ar _dat e[12] ; / * f i l e member dat e */

member name */char ar _gi d[ 6] ; / * f i l e member gr oup

i dent i f i cat i on * /char ar _mode[ 8] ; / * f i l e member mode

( octal ) * /char ar _si ze[ 10] ; / * f i l e member si ze */char ar _f mag[ 2] ; / * header t r ai l er st r i ng */

};

Al l i nf or mat i on i n t he f i l e member header s i s i n pr i nt abl eASCI I . The numer i c i nf ormat i on i n t he header s i s st or ed asdeci mal numbers ( except f or ar _mode, whi ch i s i n oct al ) . Thus, i f t he ar chi ve cont ai ns pr i nt abl e f i l es, t he ar chi vei t sel f i s pr i ntabl e.

 The ar _name f i el d i s bl ank- padded and t er mi nat ed wi t h asl ash ( / ) . The ar _dat e f i el d i s the modi f i cat i on dat e of 

t he f i l e at t he t i me i t i s i nser t ed i nt o t he ar chi ve.Common f ormat ar chi ves can be moved f r om syst em t o syst em asl ong as t he por t abl e archi ve command ar i s used.

Each ar chi ve f i l e member begi ns on an even byt e boundary; anewl i ne i s i nser t ed bet ween f i l es i f necessar y.Never t hel ess t he si ze gi ven r ef l ect s t he act ual si ze of t hef i l e excl usi ve of paddi ng.

Not i ce t here i s no pr ovi si on f or empt y ar eas i n an ar chi vef i l e.

I f t he ar chi ve symbol t abl e exi sts, t he f i r st f i l e i n t hearchi ve has a zero l engt h name ( t hat i s, ar_ name[0] == ' / ' ) . The cont ent s of t hi s f i l e ar e:

 The number of symbol s. Lengt h: 4 byt es.

 The ar r ay of of f set s i nt o t he ar chi ve f i l e. Lengt h: 4byt es * " t he number of symbol s" .

 The name st r i ng t abl e. Lengt h: ar _si ze - ( 4 byt es *( " t he number of symbol s" + 1) ) .

 The st r i ng t abl e cont ai ns exact l y as many nul l - t er mi nat edst r i ngs as t her e ar e el ement s i n t he of f set s ar r ay. Eachof f set f r om t he ar r ay i s associ at ed wi t h t he cor r espondi ng

name f r om t he st r i ng t abl e ( i n or der ) . The names i n t hest r i ng t abl e ar e al l t he def i ned gl obal symbol s f ound i n t hecommon obj ect f i l es i n t he ar chi ve. Each of f set i s t hel ocat i on of t he ar chi ve header f or t he associ at ed symbol .

SEE ALSOar( 1) , l d( 1) , str i p( 1) , l dahr ead( 3X) , l df cn( 4) , a. out ( 4) .

CAVEATSst r i p r emoves al l ar chi ve symbol ent r i es f r omt he header .

161

Page 162: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 162/193

The ar chi ve symbol ent r i es must be r est ored wi t h the t sopt i on of ar command bef ore t he ar chi ve can be used wi t h t hel i nk edi tor l d.

NOTESÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ

NOTESÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜAPPENDI X J - t i me

$man t i me

NAMEt i me - get t i me

SYNOPSI S#i ncl ude <sys/ t ypes. h>

t i me_t t i me ( t l oc)t i me_t *t l oc;

DESCRI PTI ONt i me ret ur ns t he val ue of t i me i n seconds si nce 00: 00: 00GMT, J anuary 1, 1970.

I f t l oc i s not a nul l poi nt er , t he r et ur n val ue i s al sost or ed i n t he l ocat i on t o whi ch t l oc poi nt s.

t i me wi l l f ai l i f the f ol l owi ng i s t r ue:

[ EFAULT] poi nt s t o an i l l egal addr ess.

SEE ALSOst i me( 2) .

DI AGNOSTI CSUpon successf ul compl et i on, t i me r etur ns t he val ue of t i me.Ot her wi se, a val ue of - 1 i s r et ur ned and er r no i s set t oi ndi cat e t he er r or .

NAMEt i me, ct i me, l t i me, gmt i me - r et ur n syst em t i me

SYNOPSI Si nt eger f unct i on t i me( ) char act er *( *) f unct i on ct i me ( st i mei nt eger st i me subr out i ne l t i me ( st i me, t ar r ay)i nt eger s t i me, t ar r ay( 9) subr out i ne gmt i me ( st i me, t arr ay)

i nt eger st i me, t ar r ay( 9)

DESCRI PTI ON Ti me r et urns t he t i me si nce 00: 00: 00 GMT, J an 1, 1970,measur ed i n seconds. Thi s i s t he val ue of t he syst em cl ock

Ct i me conver t s a syst em t i me to a 24- char act er ASCI I st r i ng The f or mat i s descr i bed under ct i me( 3) . No newl i ne or NULLi s i ncl uded.

162

Page 163: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 163/193

Page 164: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 164/193

 

l et t er s, di gi t s, or under scor es start i ng wi t h a l et t er or

A si mpl e- command i s a sequence of bl ank separat ed wor ds

t he command t o be executed. Except as speci f i ed bel ow, t he

val ues) .

whi ch i s l ower t han t hat of && and | | . The symbol s && and

parent shel l . The st andard i nput and out put of t he spawned

appear i n a l i st , i nst ead of semi col ons, t o del i mi t

f ol l owi ng. Unl ess other wi se st at ed, t he val ue r eturned by a

command.

A bl ank i s a t ab or a space. An i dent i f i er i s a sequence of 

under scor e. I dent i f i er s ar e used as names f or al i ases,f unct i ons, and named par amet er s. A word i s a sequence of char act ers separat ed by one or more non- quot edmetacharact ers.

Commands.

whi ch may be preceded by a par amet er ass i gnment l i st . ( SeeEnvi r onment bel ow) . The f i r st word speci f i es t he name of 

r emai ni ng words are passed as argument s t o t he i nvokedcommand. The command name i s passed as ar gument 0 ( seeexec( 2) ) . The val ue of a si mpl e- command i s i t s exi t st at usi f i t t er mi nat es nor mal l y, or ( octal ) 200+st at us i f i tt er mi nat es abnor mal l y (see si gnal ( 2) f or a l i st of st at us

A pi pel i ne i s a sequence of one or more commands separat ed| . The st andard out put of each command but t he l ast i sconnected by a pi pe(2) t o t he st andard i nput of t he nextcommand. Each command i s r un as a separ at e pr ocess; t heshel l wai t s f or t he l ast command t o t ermi nate. The exi tst at us of a pi pel i ne i s t he exi t st at us of t he l ast command.A l i st i s a sequence of one or more pi pel i nes separat ed by; , &, &&, or | | , and opt i onal l y t er mi nat ed by ; , &, or | &.Of t hese f i ve symbol s, ; , &, and | & have equal pr ecedence,

| | al so have equal pr ecedence. A semi col on ( ; ) causessequent i al execut i on of t he pr ecedi ng pi pel i ne; an ampersand( &) causes asynchr onous execut i on of t he pr ecedi ng pi pel i ne( i . e. , t he shel l does not wai t f or t hat pi pel i ne t o f i ni sh) . The symbol | & causes asynchronous execut i on of t he precedi ngcommand or pi pel i ne wi t h a t wo- way pi pe est abl i shed t o the

command can be wr i t t en t o and r ead f r om by t he par ent shel lusi ng the - p opt i on of t he speci al commands r ead and pr i ntdescr i bed l at er . Onl y one such command can be act i ve at anygi ven t i me. The symbol && ( | | ) causes t he l i st f ol l owi ng i tt o be execut ed onl y i f t he pr ecedi ng pi pel i ne r et ur ns a zer o( non- zero) val ue. An arbi t r ary number of new- l i nes may

commands.

A command i s ei t her a si mpl e- command or one of t he

command i s t hat of t he l ast si mpl e- command execut ed i n the

f or i dent i f i er [ i n word . . . ] do l i st doneEach t i me a f or command i s execut ed, i dent i f i er i s sett o t he next wor d t aken f r om t he i n wor d l i st . I f i nword . . . i s omi t t ed, t hen t he f or command execut es t hedo l i st once f or each posi t i onal par amet er t hat i s set( see Par ameter Subst i t ut i on bel ow) . Execut i on ends

164

Page 165: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 165/193

when t here ar e no more words i n t he l i st .

sel ect i dent i f i er [ i n word . . . ] do l i st doneA sel ect command pr i nt s on st andar d er r or ( f i l e

pat t erns i s t he same as t hat used f or f i l e- namegenerat i on ( see Fi l e Name Generat i on bel ow) .

{ l i s t ; }

i dent i f i er ( ) { l i st ; }

descr i pt or 2) , t he set of words, each pr eceded by anumber . I f i n wor d . . . i s omi t t ed, t hen t heposi t i onal parameters ar e used i nst ead ( see ParameterSubst i t ut i on bel ow) . The PS3 pr ompt i s pr i nt ed and al i ne i s read f r om t he standar d i nput . I f t hi s l i neconsi st s of t he number of one of t he l i st ed wor ds, t hent he val ue of t he par amet er i dent i f i er i s set t o t hewor d cor r espondi ng t o t hi s number . I f t hi s l i ne i sempt y, t he sel ecti on l i st i s pr i nt ed agai n. Ot her wi set he val ue of t he par amet er i dent i f i er i s set t o nul l . The cont ent s of t he l i ne r ead f r om st andar d i nput i ssaved i n t he parameter REPLY. The l i st i s execut ed f oreach sel ecti on unt i l a br eak or end- of - f i l e i sencountered.

case word i n [ pat t ern [ | pat t ern ] . . . ) l i s t ; ; ] . . . esac

A case command executes t he l i st associ ated wi t h thef i r st pat t er n t hat mat ches wor d. The f or m of t he

i f l i s t t h e n l i s t [ e l i f l i s t t h e n l i s t ] . . . [ e l s e l i s t ] f i The l i st f ol l owi ng i f i s execut ed and, i f i t r et urns azero exi t stat us, t he l i st f ol l owi ng t he f i r st t hen i sexecut ed. Ot her wi se, t he l i st f ol l owi ng el i f i sexecut ed and, i f i t s val ue i s zer o, t he l i st f ol l owi ngt he next t hen i s execut ed. Fai l i ng t hat , t he el se l i sti s execut ed. I f no el se l i st or t hen l i st i s execut ed,t hen t he i f command retur ns a zero exi t st atus.

whi l e l i st do l i st doneunt i l l i s t do l i s t done

A whi l e command repeat edl y execut es t he whi l e l i st and,i f t he exi t st at us of t he l ast command i n t he l i st i szer o, execut es t he do l i st ; ot her wi se t he l oopt ermi nates. I f no commands i n t he do l i st areexecut ed, t hen t he whi l e command r et urns a zer o exi tst at us; unt i l may be used i n pl ace of whi l e t o negat et he l oop t er mi nat i on test .

( l i s t )Execut e l i st i n a separ at e envi r onment . Not e, t hat i f t wo adj acent open parent heses ar e needed f or nest i ng, a

space must be i nser t ed t o avoi d ar i t hmet i c eval uat i onas descr i bed bel ow.

l i st i s si mpl y execut ed. Not e t hat { i s a keyword andr equi r es a bl ank i n or der t o be recogni zed.

f unct i on i dent i f i er { l i s t ; }

Def i ne a f unct i on whi ch i s r ef er enced by i dent i f i er .

165

Page 166: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 166/193

The body of t he f unct i on i s t he l i st of commandsbet ween { and }. ( See Funct i ons bel ow) .

A word begi nni ng wi t h # causes t hat word and al l t he

charact er of an al i as name can be any pr i nt abl e charact er,

val i d i dent i f i er . The r epl acement st r i ng can cont ai n any

subst i t ut i on. Al i ases can be used t o r edef i ne speci al

keywords l i st ed above. Al i ases can be creat ed, l i st ed, and

ar e execut ed. Ther ef or e, f or an al i as to t ake ef f ect t he

of t he al i as t o be aut omat i cal l y set t o the f ul l pat h name

t i me pi pel i ne The pi pel i ne i s execut ed and t he el apsed t i me as wel las t he user and syst emt i me ar e pr i nt ed on st andarderror.

 The f ol l owi ng keywor ds ar e onl y r ecogni zed as t he f i r st wor dof a command and when not quot ed:

i f t hen el se el i f f i case esac f or whi l e unt i l do done { }f uncti on sel ect t i me

Comment s.

f ol l owi ng char act er s up t o a new- l i ne to be i gnor ed.

Al i asi ng. The f i r st wor d of each command i s r epl aced by t he t ext of an

al i as i f an al i as f or t hi s wor d has been def i ned. The f i r st

but t he rest of t he charact ers must be the same as f or a

val i d shel l scri pt i ncl udi ng t he met achar acters l i st edabove. The f i r st word of each command of t he r epl aced t extwi l l not be t est ed f or addi t i onal al i ases. I f t he l astchar act er of t he al i as val ue i s a bl ank t hen t he wor df ol l owi ng t he al i as wi l l al so be checked f or al i as

bui l t - i n commands but cannot be used t o redef i ne the

expor t ed wi t h t he al i as command and can be r emoved wi t h theunal i as command. Export ed al i ases remai n i n ef f ect f orsub- shel l s but must be r ei ni t i al i zed f or separ at ei nvocat i ons of t he shel l ( See I nvocat i on bel ow) .

Al i asi ng i s per f or med when scr i pt s are r ead, not whi l e t hey

al i as command has t o be executed bef or e t he command whi chr ef er ences t he al i as i s r ead.

Al i ases are f r equent l y used as a shor t hand f or f ul l pat hnames. An opt i on t o t he al i asi ng f aci l i t y al l ows the val ue

of t he cor r espondi ng command. These al i ases are cal l edt r acked al i ases. The val ue of a t r acked al i as i s def i ned

t he f i r st t i me t he i dent i f i er i s r ead and undef i ned eacht i me t he PATH var i abl e i s reset . These al i ases remai nt r acked so that t he next subsequent r ef er ence wi l l r edef i net he val ue. Sever al t r acked al i ases ar e compi l ed i nt o t heshel l . The - h opt i on of t he set command makes each commandname whi ch i s an i dent i f i er i nt o a tr acked al i as.

 The f ol l owi ng export ed al i ases ar e compi l ed i nt o t he shel lbut can be unset or r edef i ned:

166

Page 167: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 167/193

echo=' pr i nt - 'f al se=' l et 0'f uncti ons=' t ypeset - f 'hi s tory=' f c - l 'i nt eger =' t ypeset - i 'nohup=' nohup 'pwd=' pr i nt - $PWD'r=' f c - e - 't r ue=' : 't ype=' whence - v'hash=' al i as - t '

 Ti l de Subst i t ut i on.Af t er al i as subst i t ut i on i s per f or med, each wor d i s checkedt o see i f i t begi ns wi t h an unquot ed ~. I f i t does, t hent he word up t o a / i s checked to see i f i t matches a username i n t he / et c/ passwd f i l e. I f a mat ch i s f ound, t he ~and the mat ched l ogi n name i s r epl aced by t he l ogi ndi r ectory of t he mat ched user . Thi s i s cal l ed a t i l de

subst i t ut i on. I f no mat ch i s f ound, t he or i gi nal t ext i sl ef t unchanged. A ~ by i t sel f , or i n f r ont of a / , i s

t o see i f i t begi ns wi t h a ~ or i f a ~ appear s af t er a : .

`cat f i l e` can be r epl aced by the equi val ent but f ast er

charact ers * , @, #, ?, - , $, and ! . A named parameter ( aparameter denoted by an i dent i f i er ) has a val ue and zer o or

An el ement of an arr ay paramet er i s r ef erenced by a

r epl aced by t he val ue of t he HOME par amet er . A ~ f ol l owedby a + or - i s r epl aced by t he val ue of t he parameter PWDand OLDPWD r espect i vel y.

I n addi t i on, t he val ue of each keyword parameter i s checked

I n ei t her of t hese cases a t i l de subst i t ut i on i s at t empt ed.

Command Subst i t ut i on. The st andar d out put f r om a command encl osed i n a pai r of grave accent s ( `` ) may be used as part or al l of a word;t r ai l i ng new- l i nes are r emoved. The command subst i t ut i on

`<f i l e> . Command subst i t ut i on of most speci al commandst hat do not per f or m i nput / out put r edi r ect i on ar e car r i ed outwi t hout cr eat i ng a separat e pr ocess.

Par amet er Subst i t ut i on.A par amet er i s an i dent i f i er , a di gi t , or any of t he

more at t r i but es. Named par amet ers can be assi gned val uesand at t r i but es by usi ng t he t ypeset speci al command. Theat t r i but es suppor t ed by t he shel l ar e descri bed l at er wi t ht he t ypeset speci al command. Expor t ed par amet ers pass

val ues and at t r i but es t o sub- shel l s but onl y val ues t o theenvi r onment .

 The shel l suppor t s a l i mi t ed one- di mensi onal ar r ay f aci l i t y.

subscr i pt . A subscr i pt i s denot ed by a [ , f ol l owed by anari t hmet i c expr essi on ( see Ar i t hmeti c Eval uati on bel ow)f ol l owed by a ] . The val ue of al l subscr i pt s must be i n t her ange of 0 t hr ough 511. Ar r ays need not be decl ared. Anyr ef erence to a named parameter wi t h a val i d subscr i pt i s

167

Page 168: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 168/193

l egal and an ar r ay wi l l be cr eat ed i f necessary.Ref er enci ng an ar r ay wi t hout a subscr i pt i s equi val ent t or ef er enci ng t he f i r st el ement .

 The val ue of a named paramet er may al so be assi gned bywr i t i ng:

name=val ue [ name=val ue ] . . .

I f t he i nt eger at t r i but e, - i , i s set f or name t he val ue i ssubj ect t o ar i t hmet i c eval uat i on as descr i bed bel ow.Posi t i onal par amet er s, par amet ers denot ed by a number , maybe assi gned val ues wi t h t he set speci al command. Par amet er$0 i s set f r om argument zero when t he shel l i s i nvoked. The char act er $ i s used t o i nt r oduce subst i t ut abl epar amet ers.${par amet er }

 The val ue, i f any, of t he paramet er i s subst i t ut ed. The braces ar e r equi r ed when paramet er i s f ol l owed by al et t er , di gi t , or under scor e t hat i s not t o be

i nterpret ed as par t of i t s name or when a namedpar amet er i s subscri pt ed. I f par amet er i s a di gi t t heni t i s a posi t i onal par amet er . I f par amet er i s * or @,t hen al l t he posi t i onal par amet er s, st ar t i ng wi t h $1,ar e subst i t ut ed ( separ at ed by spaces) . I f an ar r ayi dent i f i er wi t h subscr i pt * or @ i s used, t hen t heval ue f or each of t he el ement s i s subst i t ut ed( separated by spaces) .

${#par amet er }I f par amet er i s not *, t he l engt h of t he val ue of t heparameter i s subst i t ut ed. Ot herwi se, t he number of posi t i onal par amet er s i s subst i t ut ed.

${#i dent i f i er [ *] } The number of el ement s i n t he ar r ay i dent i f i er i ssubst i t ut ed.

${par amet er : - word}I f par amet er i s set and i s non- nul l t hen subst i t ut e i t sval ue; ot her wi se subst i t ut e wor d.

${par amet er : =wor d}I f par amet er i s not set or i s nul l t hen set i t t o wor d;t he val ue of t he par amet er i s t hen subst i t ut ed.Posi t i onal parameters may not be assi gned t o i n t hi sway.

${par amet er : ?wor d}I f par amet er i s set and i s non- nul l t hen subst i t ut e i t sval ue; ot her wi se, pr i nt wor d and exi t f r om t he shel l .I f word i s omi t t ed t hen a st andard message i s pr i nt ed.

${par amet er : +wor d}I f par amet er i s set and i s non- nul l t hen subst i t ut ewor d; ot herwi se subst i t ut e nothi ng.

${par amet er #pat t ern}${par amet er ##pat t er n}

I f t he shel l patt er n mat ches t he begi nni ng of t he val ueof par amet er , t hen t he val ue of t hi s subst i t ut i on i st he val ue of t he paramet er wi t h t he mat ched port i ondel et ed; ot her wi se, t he val ue of t hi s par amet er i ssubst i t ut ed. I n t he f i r st f or m t he smal l est mat chi ng

168

Page 169: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 169/193

pat t er n i s del et ed and i n t he l at t er f or m t he l ar gestmatchi ng pat t ern i s del eted.

${par amet er %pat t ern}${par amet er %%pat t er n}

I f t he shel l patt er n mat ches t he end of t he val ue of par amet er , t hen t he val ue of par amet er wi t h t he mat chedpar t i s del et ed; ot her wi se subst i t ut e t he val ue of par amet er . I n t he f i r st f or m t he smal l est mat chi ngpat t er n i s del et ed and i n t he l at t er f or m t he l ar gestmatchi ng pat t ern i s del eted.

I n t he above, wor d i s not eval uat ed unl ess i t i s t o be usedas the subst i t ut ed st r i ng, so t hat , i n t he f ol l owi ngexampl e, pwd i s execut ed onl y i f d i s not set or i s nul l :

echo ${d: - `pwd`}

I f t he col on ( : ) i s omi t t ed f r om t he above expr essi ons, t hent he shel l onl y checks whether par ameter i s set or not .

I f t he shel l patt er n mat ches t he end of t he val ue of par ameter , t hen t he val ue of parameter wi t h t he matchedpar t i s del et ed; ot her wi se subst i t ut e t he val ue of par amet er . I n t he f i r st f or m t he smal l est mat chi ngpat t er n i s del et ed and i n t he l at t er f or m t he l ar gestmatchi ng pat t ern i s del eted.

I n t he above, wor d i s not eval uat ed unl ess i t i s t o be usedas the subst i t ut ed st r i ng, so t hat , i n t he f ol l owi ngexampl e, pwd i s execut ed onl y i f d i s not set or i s nul l :

echo ${d: - `pwd`}

I f t he col on ( : ) i s omi t t ed f r om t he above expr essi ons, t hent he shel l onl y checks whether par ameter i s set or not .

 The f ol l owi ng paramet er s ar e aut omat i cal l y set by t he shel l :# The number of posi t i onal parameters i n deci mal .- Fl ags suppl i ed t o t he shel l on i nvocat i on or by

t he set command.? The deci mal val ue r etur ned by t he l ast execut ed

command.$ The pr ocess number of t hi s shel l . _ The l ast ar gument of t he previ ous command. Thi s

par amet er i s not set f or commands whi ch ar easynchronous.

! The process number of t he l ast background command

i nvoked.PPI D The pr ocess number of t he parent of t he shel l .PWD The present worki ng di r ect ory set by t he cd

command.OLDPWD

 The previ ous wor ki ng di r ect or y set by t he cdcommand.

RANDOMEach t i me thi s par ameter i s r ef erenced, a randomi nt eger i s generated. The sequence of r andom

169

Page 170: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 170/193

numbers can be i ni t i al i zed by assi gni ng a numeri ci ni t i al i zed by assi gni ng a numer i c val ue t o

RANDOM.Each t i me thi s par ameter i s r ef erenced, a randomi nt eger i s generated. The sequence of r andomnumbers can be i ni t i al i zed by assi gni ng a numeri ct o RANDOM.

REPLY Thi s paramet er i s set by t he sel ect st at ement andby t he read speci al command when no argument s ar esuppl i ed.

 The f ol l owi ng paramet er s ar e used by t he shel l :

I f t he val ue of t hi s var i abl e ends i n emacs,

bel ow. ) Thi s f i l e i s typi cal l y used f or al i as and

CDPATH The sear ch path f or t he cd command.

COLUMNSI f t hi s var i abl e i s set , t he val ue i s used t odef i ne the wi dt h of t he edi t wi ndow f or t he shel ledi t modes and f or pr i nt i ng sel ect l i st s.

EDI TOR

gmacs, or vi and t he VI SUAL var i abl e i s not set ,t hen t he cor r espondi ng opt i on ( see Speci alCommands set bel ow) wi l l be t urned on.

ENV I f t hi s parameter i s set , t hen parametersubst i t ut i on i s per f or med on the val ue t o gener atet he path name of t he scr i pt t hat wi l l be execut edwhen t he shel l i s i nvoked. ( See I nvocat i on

f uncti on def i ni t i ons.FCEDI T

 The def aul t edi t or name f or t he f c command.I FS I nt er nal f i el d separ at or s, nor mal l y space, t ab,

and new- l i ne t hat i s used to separat e commandwords whi ch r esul t f r om command or par amet ersubst i t ut i on and f or separ at i ng words wi t h t hespeci al command r ead.

HI STFI LEI f t hi s par amet er i s set when t he shel l i si nvoked, t hen t he val ue i s t he pat h name of t hef i l e that wi l l be used t o st ore t he commandhi st ory. ( See Command Re- ent r y bel ow. )

HI STSI ZEI f t hi s par amet er i s set when t he shel l i si nvoked, t hen t he number of pr evi ousl y ent eredcommands t hat are accessi bl e by t hi s shel l wi l l begr eat er t han or equal t o t hi s number. The def aul t

i s 128.HOME The def aul t argument ( home di r ect ory) f or t he cdcommand.

MAI L I f t hi s par ameter i s set t o t he name of a mai lf i l e and t he MAI LPATH parameter i s not set , t hent he shel l i nf or ms t he user of ar r i val of mai l i nt he speci f i ed f i l e.

MAI LCHECK  Thi s var i abl e speci f i es how of t en ( i n seconds) t heshel l wi l l check f or changes i n t he modi f i cat i on

170

Page 171: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 171/193

t i me of any of t he f i l es speci f i ed by t he MAI LPATHor MAI L parameters . The def aul t val ue i s 600seconds. I f set t o 0, t he shel l wi l l check bef or eeach prompt .

MAI LPATHA col on ( : ) separ at ed l i st of f i l e names. I f t hi s par amet er i s set t hen t he shel l i nf or ms t heuser of any modi f i cat i ons t o the speci f i ed f i l est hat have occur r ed wi t hi n t he l ast MAI LCHECK seconds. Each f i l e name can be f ol l owed by a ?and a message t hat wi l l be pr i nted. The messagewi l l under go par amet er and command subst i t ut i onwi t h the paramet er , $_ def i ned as t he name of t hef i l e t hat has changed. The def aul t message i s youhave mai l i n $_.

PATH The sear ch pat h f or commands ( see Execut i onbel ow) . The user may not change PATH i f execut i ngunder r ksh ( except i n . pr of i l e) .

PS1 The val ue of t hi s paramet er i s expanded f orpar amet er subst i t ut i on t o def i ne the pr i mar y

pr ompt s t r i ng whi ch by def aul t i s "$ ". Thechar act er ! i n t he pr i mar y pr ompt st r i ng i sr epl aced by t he command number ( see Command Re-ent r y bel ow) .

PS2 Secondary pr ompt st r i ng, by def aul t " > " .PS3 Sel ect i on pr ompt st r i ng used wi t hi n a sel ect l oop,

by def aul t "#? " .SHELL

 The pat h name of t he shel l i s kept i n t heenvi r onment . At i nvocat i on, i f t he val ue of t hi svar i abl e cont ai ns an r i n t he basename, t hen t heshel l becomes r est r i ct ed.

 TMOUTI f set t o a val ue gr eat er t han zer o, t he shel lwi l l t er mi nat e i f a command i s not ent ered wi t hi nt he prescr i bed number of seconds. When t he t i merexpi r es, a warni ng i s pr i nt ed and a 60 secondgr ace per i od i s provi ded.

VI SUALI f t he val ue of t hi s var i abl e ends i n emacs,gmacs, or vi t hen t he corr espondi ng opt i on ( seeSpeci al Commands set bel ow) wi l l be t urned on.

 The shel l gi ves def aul t val ues t o PS1, PS2, MAI LCHECK, TMOUT, and I FS. HOME, MAI L, SHELL, PATH, and TZ ar e set byl ogi n( 1) . The r emai ni ng par amet er s ar e t ypi cal l y set i n/ etc / prof i l e, . prof i l e, or $(ENV) f i l es .

Af t er parameter and command subst i t ut i on, t he r esul t s of subst i t ut i ons are scanned f or t he f i el d separ at or char act er s( t hose f ound i n I FS ) and spl i t i nt o di st i nct ar gument swher e such char act er s ar e f ound. Expl i ci t nul l argument s ""or ' ' ar e r et ai ned. I mpl i ci t nul l ar gument s ( t hoser esul t i ng f r om parameters t hat have no val ues) are removed.

Fol l owi ng subst i t ut i on, each command word i s scanned f or t hechar act er s *, ?, and [ unl ess t he - f opt i on has been set .

171

Page 172: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 172/193

I f one of t hese charact ers appear s t hen t he word i s r egardedas a pat t ern. The wor d i s r epl aced wi t h al phabet i cal l ysort ed f i l e names t hat match t he pat t er n. I f no f i l e namei s f ound t hat mat ches t he pat t er n, t hen t he wor d i s l ef tunchanged. When a pat t ern i s used f or f i l e name generat i on,t he char act er . at t he st ar t of a f i l e name or i mmedi atel yf ol l owi ng a / , as wel l as t he char acter / i t sel f , must bemat ched expl i ci t l y. I n other i nst ances of pat t er n mat chi ngt he / and . ar e not t r eat ed speci al l y.

* Mat ches any st r i ng, i ncl udi ng t he nul l st r i ng.? Matches any si ngl e charact er.[ . . . ]

Matches any one of t he encl osed char act ers. A

pai r of charact ers separated by - matches anychar acter l exi cal l y bet ween t he pai r , i ncl usi ve.I f t he f i r st char acter f ol l owi ng t he openi ng [ i sa ! , t hen any charact er not encl osed i s matched.A - can be i ncl uded i n t he charact er set by

put t i ng i t as the f i r st or l ast char acter .

Each of t he met achar act er s l i st ed above ( See Def i ni t i onsabove) has a speci al meani ng t o the shel l and causest ermi nat i on of a word unl ess quoted. A character may bequot ed ( i . e. , made t o st and f or i t sel f ) by pr ecedi ng i t wi t ha \ . The pai r \ new- l i ne i s i gnor ed. Al l char actersencl osed bet ween a pai r of si ngl e quot e mar ks ( ' ' ) , except asi ngl e quote, are quot ed. I nsi de doubl e quote marks ( "" ) ,par amet er and command subst i t ut i on occurs and \ quot es t hechar acter s \ , ' , " , and $. $* i s equi val ent t o "$1 $2 . . . " ,wher eas $@ i s equi val ent t o $1 $2 . . . .

 The speci al meani ng of keywor ds can be r emoved by quot i ngany charact er of t he keyword. The r ecogni t i on of speci alcommand names l i st ed bel ow cannot be al t ered by quot i ngt hem.

An abi l i t y t o per f or m i nt eger ar i t hmet i c i s pr ovi ded wi t ht he speci al command l et . Eval uat i ons are per f ormed usi ngl ong ari t hmet i c. Const ant s ar e of t he f or m [ base#] n wher ebase i s a deci mal number between t wo and t hi r t y- si xr epr esent i ng the ar i t hmet i c base and n i s a number i n thatbase. I f t he base i s omi t t ed t hem base 10 i s used.

An i nt ernal i nt eger r epr esent at i on of a named parameter canbe speci f i ed wi t h t he - i opt i on of t he t ypeset speci al

command. When t hi s at t r i but e i s sel ect ed t he f i r stassi gnment t o t he paramet er det ermi nes t he ar i t hmet i c baset o be used when parameter subst i t ut i on occur s.

Si nce many of t he ar i t hmet i c operat ors r equi r e quot i ng, anal t er nat i ve f or m of t he l et command i s pr ovi ded. For anycommand whi ch begi ns wi t h a ( ( , al l t he charact ers unt i l amatchi ng ) ) are t r eat ed as a quoted expr essi on. Morepreci sel y, ( ( . . . ) ) i s equi val ent t o l et " . . . " .

172

Page 173: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 173/193

When used i nt eract i vel y, t he shel l pr ompt s wi t h t he val ue of PS1 bef ore r eadi ng a command. I f at any t i me a new- l i ne i st yped and f urt her i nput i s needed to compl et e a command,t hen t he secondar y pr ompt ( i . e. , t he val ue of PS2) i si ssued.

Bef ore a command i s execut ed, i t s i nput and output may ber edi r ect ed usi ng a speci al not at i on i nt er pr et ed by theshel l . The f ol l owi ng may appear anywhere i n a si mpl e-command or may pr ecede or f ol l ow a command and are notpassed on t o t he i nvoked command. Command and par amet ersubst i t ut i on occur s bef or e wor d or di gi t i s used except asnoted bel ow. Fi l e name generat i on occur s onl y i f t hepat t er n mat ches a si ngl e f i l e and bl ank i nt er pr et at i on i snot per f ormed.

<wor d Use f i l e word as st andar d i nput ( f i l edescri pt or 0) .

>wor d Use f i l e word as st andar d out put ( f i l e

descr i pt or 1) . I f t he f i l e does not exi stt hen i t i s creat ed; ot her wi se, i t i s t r uncat edt o zer o l engt h.

>>wor d Use f i l e word as st andar d out put . I f t he f i l eexi st s t hen out put i s appended t o i t ( by f i r stseeki ng t o t he end- of - f i l e) ; ot her wi se, t hef i l e i s created.

<<[ - ] wor d The shel l i nput i s r ead up t o a l i ne t hat i st he same as wor d, or t o an end- of - f i l e. Noparameter subst i t ut i on, command subst i t ut i onor f i l e name generat i on i s per f ormed on word. The r esul t i ng document , cal l ed a here-document , becomes t he st andard i nput . I f anychar act er of wor d i s quot ed, no i nt er pr et at i oni s pl aced upon t he charact ers of t he document ;ot herwi se, parameter and command subst i t ut i onoccur s, \ new- l i ne i s i gnored, and \ must beused t o quote t he char act er s \ , $, `, and t hef i r st char acter of wor d. I f - i s appended t o<<, al l l eadi ng t abs are st r i pped f r om wor d

t he st andard out put usi ng >& di gi t .

t he st andard out put usi ng >&- .

and f r om t he document .

<&di gi t The st andar d i nput i s dupl i cat ed f r om f i l edescr i pt or di gi t ( see dup( 2) ) . Si mi l ar l y f or

<&- The st andar d i nput i s cl osed. Si mi l ar l y f or

I f one of t he above i s pr eceded by a di gi t , t hen t he f i l edescri pt or number r ef er r ed t o i s t hat speci f i ed by the di gi t( i nst ead of t he def aul t 0 or 1) . For exampl e:

. . . 2>&1

173

Page 174: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 174/193

means f i l e descr i pt or 2 i s t o be opened f or wr i t i ng as adupl i cat e of f i l e descr i pt or 1.

 The or der i n whi ch r edi r ect i ons ar e speci f i ed i s

of t he ( f i l e descr i pt or , f i l e) associ at i on at t he t i me of 

. . . 1>f name 2>&1

been) and then f i l e descr i pt or 1 woul d be associ ated wi t h

and t he val ues ar e char act er st r i ngs. The shel l i nt er act s

name f ound, gi vi ng i t t he cor r espondi ng val ue and marki ng i t

ori gi nal l y i nheri t ed by t he shel l , whose val ues may be

be not ed i n expor t or t ypeset - x commands.

 The envi r onment f or any si mpl e- command or f unct i on may be

assi gnment s. A par amet er assi gnment argument i s a word of 

( expor t TERM; TERM=450; cmd args)

si gni f i cant . The shel l eval uat es each r edi r ect i on i n t er ms

eval uat i on. For exampl e:

f i r st associ at es f i l e descr i pt or 1 wi t h f i l e f name. I t t henassoci at es f i l e descri pt or 2 wi t h t he f i l e associ at ed wi t hf i l e descr i pt or 1 ( i . e. f name) . I f t he or der of r edi r ect i ons wer e r ever sed, f i l e descri pt or 2 woul d beassoci at ed wi t h t he t er mi nal ( assumi ng f i l e descr i pt or 1 had

f i l e f name.

I f a command i s f ol l owed by & and j ob cont r ol i s not act i ve,t hen t he def aul t st andard i nput f or t he command i s t he empty

f i l e / dev/ nul l . Ot her wi se, t he envi r onment f or t heexecut i on of a command cont ai ns t he f i l e descr i pt ors of t hei nvoki ng shel l as modi f i ed by i nput / out put speci f i cat i ons.

 The envi r onment ( see envi r on(5) ) i s a l i st of name- val uepai r s t hat i s passed t o an execut ed pr ogr am i n t he same wayas a normal argument l i st . The names must be i dent i f i ers

wi t h t he envi r onment i n several ways. On i nvocat i on, t heshel l scans t he envi r onment and cr eat es a paramet er f or each

export . Execut ed commands i nheri t t he envi r onment . I f t heuser modi f i es t he val ues of t hese parameters or cr eat es newones, usi ng t he expor t or t ypeset - x commands t hey becomepar t of t he envi r onment . The envi r onment seen by anyexecut ed command i s t hus composed of any name- val ue pai r s

modi f i ed by t he cur r ent shel l , pl us any addi t i ons whi ch must

augment ed by pref i xi ng i t wi t h one or more paramet er

t he f or m i dent i f i er =val ue. Thus:

 TERM=450 cmd ar gs and

are equi val ent ( as f ar as t he above execut i on of cmd i sconcerned) .

I f t he - k f l ag i s set , al l paramet er assi gnment argument sare pl aced i n t he envi r onment , even i f t hey occur af t er t hecommand name. The f ol l owi ng f i r st pr i nt s a=b c and t hen c:

echo a=b c

174

Page 175: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 175/193

set -kecho a=b c

Funct i ons. The f unct i on keywor d, descr i bed i n t he Commands sect i onabove, i s used t o def i ne shel l f uncti ons. Shel l f uncti onsare r ead i n and st or ed i nt er nal l y. Al i as names are r esol ved

shoul d be pl aced i n t he ENV f i l e.

when t he f unct i on i s r ead. Funct i ons ar e execut ed l i kecommands wi t h t he argument s passed as posi t i onal par amet er s.( See Execut i on bel ow) .

Funct i ons execut e i n the same pr ocess as t he cal l er andshar e al l f i l es, t r aps ( other t han EXI T and ERR) and pr esentwor ki ng di r ect or y wi t h t he cal l er . A t r ap set on EXI Ti nsi de a f unct i on i s execut ed af t er t he f unct i on compl et es.Or di nar i l y, var i abl es are shar ed bet ween the cal l i ng pr ogr amand t he f unct i on. However , t he t ypeset speci al command usedwi t hi n a f unct i on def i nes l ocal var i abl es whose scopei ncl udes t he cur r ent f uncti on and al l f uncti ons i t cal l s.

 The speci al command r et urn i s used t o r et urn f r om f unct i oncal l s. Er r or s wi t hi n f uncti ons r et ur n cont r ol t o t hecal l er .

Functi on i dent i f i er s can be l i st ed wi t h t he - f opt i on of t het ypeset speci al command. The t ext of f unct i ons wi l l al so bel i st ed. Funct i on can be undef i ned wi t h t he - f opt i on of t heunset speci al command.

Or di nar i l y, f unct i ons ar e unset when t he shel l execut es ashel l scr i pt . The - xf opt i on of t he t ypeset command al l owsa f unct i on t o be expor t ed t o scr i pt s t hat ar e execut edwi t hout a separ at e i nvocat i on of t he shel l . Funct i ons t hatneed t o be def i ned across separat e i nvocat i ons of t he shel l

 J obs.I f t he moni t or opt i on of t he set command i s t ur ned on, at ermi nat i ng backgr ound j ob i s so noted whenever ksh i swr i t i ng a pr ompt . When a j ob i s st art ed asynchr onousl y wi t h&, t he shel l pr i nt s a l i ne whi ch l ooks l i ke:

[ 1] 1234

i ndi cat i ng t hat t he j ob st ar t ed asynchr onousl y was j obnumber 1 and had one ( t op- l evel ) process , whose pr ocess i dwas 1234. I t keeps a t abl e of cur r ent j obs, pr i nt ed by t he

 j obs command, and assi gns t hem smal l i nt eger numbers.

 There ar e sever al ways t o r ef er t o j obs i n t he shel l . Thecharact er % i nt r oduces a j ob name. When r ef err i ng t o j obnumber 1, name i t as %1. J obs can al so be named by pref i xesof t he st r i ng t yped i n t o i nvoke t hem. Thus, ' ki l l %cc'woul d ki l l a backgr ound j ob whose name began wi t h t he st r i ng"cc" ( i f t her e wer e such a j ob) .

 The shel l mai nt ai ns a not i on of t he cur r ent and previ ous

175

Page 176: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 176/193

j obs. I n out put per t ai ni ng t o j obs, t he cur r ent j ob i smarked wi t h a + and t he previ ous j ob wi t h a - . Theabbr evi at i on %+ r ef ers t o t he cur r ent j ob and %- r ef ers t ot he pr evi ous j ob. %% i s al so a synonym f or t he cur r ent j ob.

 Thi s shel l l ear ns i mmedi at el y whenever a process changesst at e. I t nor mal l y i nf or ms t he user whenever a j ob i sf i ni shed execut i ng, but onl y j ust bef or e i t pr i nt s a pr ompt . Thi s i s done so t hat i t does not ot herwi se di st urb ot herwor k.

When at t empt i ng to l eave a l ogi n shel l whi l e j obs arer unni ng, be war ni ng wi l l be pr i nt ed t hat ' You have r unni ng j obs' . Use t he j obs command t o see what t hey ar e. I f i mmedi at el y t r yi ng exi t agai n, t he shel l wi l l gi ve a secondwar ni ng, and the j obs wi l l be t er mi nat ed.

 J ob Cont r ol .I f a j ob i s r unni ng, a Z <ct r l >Z can be t yped whi ch sends aSTOP si gnal t o t he cur r ent j ob. The shel l wi l l t hen

nor mal l y i ndi cat e that t he j ob has been ' St opped' , and pr i ntanother pr ompt . The st ate of t hi s j ob can t hen bemani pul at ed usi ng t he bg command, or r unni ng some ot hercommands and t hen event ual l y br i ng t he j ob back i nt o t hef oreground wi t h t he f oreground command f g. A Z t akesef f ect i mmedi at el y and i s l i ke an i nt er r upt i n t hat pendi ngout put and unr ead i nput are di scarded when i t i s t yped.

A j ob bei ng r un i n t he backgr ound wi l l st op i f i t t r i es t or ead f r om t he t er mi nal . Backgr ound j obs ar e nor mal l yal l owed t o pr oduce out put , but t hi s can be di sabl ed bygi vi ng t he command st t y t ost op. I f t hi s tt y opt i on i s set ,backgr ound j obs wi l l st op when t hey t r y t o pr oduce outputl i ke t hey do when t hey t r y to read i nput .

Si gnal s. The I NT and QUI T si gnal s f or an i nvoked command ar e i gnor edi f t he command i s f ol l owed by & and j ob moni t or opt i on i snot act i ve. Ot her wi se, si gnal s have t he val ues i nher i t ed byt he shel l f r om i t s par ent , wi t h t he except i on of si gnal 11( but see al so t he t r ap command bel ow) .

Execut i on.Each t i me a command i s execut ed, t he above subst i t ut i ons ar ecar r i ed out . I f t he command name mat ches one of t he Speci alCommands l i st ed bel ow, i t i s execut ed wi t hi n t he cur r entshel l process. Next , t he command name i s checked t o see i f i t mat ches one of t he user def i ned f unct i ons. I f i t does,

t he posi t i onal parameters are saved and t hen r eset t o theargument s of t he f unct i on cal l . When t he f unct i on compl etesor i ssues a r et ur n, t he posi t i onal par amet er l i st i sr est ored and any t r ap set on EXI T wi t hi n t he f unct i on i sexecut ed. The val ue of a f unct i on i s t he val ue of t he l astcommand execut ed. A f unct i on i s al so execut ed i n t hecurr ent shel l process . I f a command name i s not a speci alcommand or a user def i ned f unct i on, a pr ocess i s cr eat ed andan at t empt i s made t o execut e t he command vi a exec( 2) .

176

Page 177: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 177/193

The shel l parameter PATH def i nes t he search path f or t hedi r ect or y cont ai ni ng t he command. Al t ernat i ve di r ect or ynames are separated by a col on ( : ) . The def aul t path i s: / bi n: / usr/ bi n ( speci f yi ng t he cur r ent di r ector y, / bi n, and/ usr / bi n, i n t hat or der ) . Not e t hat t he cur r ent di r ectoryi s speci f i ed by a nul l pat h name, whi ch can appeari mmedi at el y af t er t he equal si gn, bet ween col on del i mi t er s,or at t he end of t he path l i st . I f t he command namecont ai ns a / t hen t he sear ch path i s not used. Ot herwi se,each di r ect or y i n t he pat h i s sear ched f or an execut abl ef i l e. I f t he f i l e has execut e per mi ssi on but i s not adi r ector y or an a. out f i l e, i t i s assumed t o be a f i l econt ai ni ng shel l commands. A sub- shel l i s spawned t o r eadi t . Al l non- expor t ed al i ases, f unct i ons, and namedpar ameters ar e r emoved i n t hi s case. A parent hesi zedcommand i s al so execut ed i n a sub- shel l .

Command Re- ent r y. The t ext of t he l ast HI STSI ZE ( def aul t 128) commands enter edf r om a t er mi nal devi ce i s saved i n a hi story f i l e. The f i l e

$HOME/ . hi st or y i s used i f t he HI STFI LE var i abl e i s not setor i s not wr i t abl e. A shel l can access the commands of al li nt eract i ve shel l s whi ch use t he same named HI STFI LE. Thespeci al command f c i s used t o l i st or edi t a por t i on t hi sf i l e. The por t i on of t he f i l e t o be edi t ed or l i st ed can besel ect ed by number or by gi vi ng t he f i r st char act er orcharact er s of t he command. A si ngl e command or r ange of commands can be speci f i ed. I f an edi t or pr ogr am i s notspeci f i ed as an ar gument t o f c, t he val ue of t he parameterFCEDI T i s used. I f FCEDI T i s not def i ned, / bi n/ ed i s used. The edi t ed command( s) i s pr i nt ed and r e- execut ed uponl eavi ng t he edi t or . The edi t or name - i s used t o ski p t heedi t i ng phase and t o r e- execut e t he command. I n t hi s case asubst i t ut i on parameter of t he f orm ol d=new can be used t omodi f y t he command bef ore execut i on. For exampl e, i f r i sal i ased t o ' f c - e - ' t ypi ng ' r bad=good c' wi l l r e- execut et he most r ecent command whi ch st ar t s wi t h t he l et t er c,r epl aci ng t he st r i ng bad wi t h t he st r i ng good.

I n- l i ne Edi t i ng Opt i onsNormal l y, each command l i ne ent ered f r om a t ermi nal devi cei s si mpl y typed f ol l owed by a new- l i ne ( ' RETURN' or' LI NE FEED' ) . I f ei t her t he emacs, or vi opt i on i s acti ve,t he user can edi t t he command l i ne. To be i n ei t her of t hese edi t modes set t he cor r espondi ng opt i on. An edi t i ngopt i on i s aut omat i cal l y sel ected each t i me t he VI SUAL orEDI TOR var i abl e i s assi gned a val ue endi ng i n ei t her of 

t hese opt i on names.

 The edi t i ng f eat ures r equi r e t hat t he user ' s t er mi nal accept' RETURN' as car r i age ret ur n wi t hout l i ne f eed and that aspace ' ' must over wr i t e t he cur r ent char act er on t hescr een. ADM t ermi nal user s shoul d set t he " space - advance"swi t ch t o ' space' . Hewl ett - Packar d seri es 2621 t er mi nalusers shoul d set t he st r aps t o ' bcGHxZ etX' .

 The edi t i ng modes i mpl ement a concept where t he user i s

177

Page 178: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 178/193

l ooki ng t hr ough a wi ndow at t he cur r ent l i ne. The wi ndowwi dt h i s t he val ue of COLUMNS i f i t i s def i ned, other wi se80. I f t he l i ne i s l onger t han t he wi ndow wi dt h mi nus t wo,a mark i s di spl ayed at t he end of t he wi ndow t o not i f y theuser . As t he cursor moves and r eaches t he wi ndow boundar i est he wi ndow wi l l be cent ered about t he cur sor . The mark i s a> ( <, *) i f t he l i ne ext ends on t he r i ght ( l ef t , bot h)si de( s) of t he wi ndow.

Emacs Edi t i ng Mode Thi s mode i s ent er ed by enabl i ng ei t her t he emacs or gmacsopt i on. The onl y di f f erence between t hese t wo modes i s theway t hey handl e T. To edi t , t he user moves the cur sor t ot he poi nt needi ng cor r ect i on and t hen i nsert s or del eteschar act ers or words as needed. Al l t he edi t i ng commands arecont r ol charact ers or escape sequences. The notat i on f orcont r ol char act er s i s car et ( ) f ol l owed by t he char act er .For exampl e, F i s t he not at i on f or cont r ol F. Thi s i sent ered by depr essi ng ' f ' whi l e hol di ng down t he ' CTRL'( cont r ol ) key. The ' SHI FT' key i s not depr essed. ( The

not at i on ? i ndi cat es the DEL ( del et e) key. )

 The not at i on f or escape sequences i s M- f ol l owed by acharact er. For exampl e, M- f ( pr onounced Meta f ) i s ent eredby depr essi ng ESC ( asci i 033 ) f ol l owed by ' f ' . ( M- F woul dbe t he not at i on f or ESC f ol l owed by ' SHI FT' ( capi t al ) ' F' . )Al l edi t commands operat e f r om any pl ace on t he l i ne ( not j ust at t he begi nni ng) . Nei t her t he "RETURN" nor t he "LI NEFEED" key i s ent ered af t er edi t commands except when not ed.

F Move cur sor f orward ( r i ght ) one charact er.M- f Move cur sor f orward one word. ( The edi t or' s i dea

of a wor d i s a st r i ng of char act er s consi st i ng of onl y l et t er s, di gi t s and under scor es. )

B Move cur sor backward ( l ef t ) one char act er .M- b Move cur sor backwar d one wor d.A Move cur sor t o st ar t of l i ne.E Move cur sor t o end of l i ne.] char Move cur sor t o charact er char on cur r ent l i ne.X X I nter change t he cur sor and mar k.

erase ( User def i ned erase charact er as def i ned by t hest t y command, usual l y H or #. ) Del ete pr evi ouscharacter.

D Del et e cur r ent character .M- d Del et e cur r ent word.M- H ( Met a- backspace) Del et e previ ous word.M- h Del et e previ ous word.

M- ? ( Meta- DEL) Del ete pr evi ous word ( i f t he i nt err uptchar act er i s ? ( DEL, t he def aul t ) t hen t hi scommand wi l l not wor k) .

T Transpose cur r ent charact er wi t h next charact er i nemacs mode. Transpose t wo previ ous char act ers i ngmacs mode.

C Capi t al i ze cur r ent char act er .M- C Capi t al i ze cur r ent word.K Ki l l f r om t he cur sor t o t he end of t he l i ne. I f 

gi ven a par amet er of zer o t hen ki l l f r om t he st ar t

178

Page 179: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 179/193

of l i ne t o t he cur sor .W Ki l l f r om t he cur sor t o t he mar k.

M- p Push t he r egi on f r om t he cur sor t o t he mark on t hestack.

ki l l ( User def i ned ki l l char acter as def i ned by t hest t y command, usual l y G or @. ) Ki l l t he ent i r ecur r ent l i ne. I f t wo ki l l char acter s ar e ent er edi n successi on, al l ki l l char acter s f r om t hen oncause a l i ne f eed ( usef ul when usi ng papert er mi nal s) .

Y Rest or e l ast i t em r emoved f r om l i ne. ( Yank i t emback t o t he l i ne. )

L Li ne f eed and pr i nt cur r ent l i ne.@ ( Nul l charact er ) Set mark.

M- ( Met a space) Set mar k.J ( New l i ne) Execut e t he cur r ent l i ne.M ( Ret ur n) Execut e t he cur r ent l i ne.

eof End- of - f i l e char acter, nor mal l y D, wi l l t er mi nat et he shel l i f t he curr ent l i ne i s nul l .

P Fet ch previ ous command. Each t i me P i s ent ered

t he previ ous command back i n t i me i s accessed.M- < Fet ch t he l east r ecent ( ol dest ) hi st or y l i ne.M- > Fet ch t he most r ecent ( youngest ) hi st ory l i ne.N Fet ch next command. Each t i me N i s ent er ed t he

next command f orward i n t i me i s accessed.Rst r i ng Reverse search hi st ory f or a pr evi ous command l i ne

cont ai ni ng st r i ng. I f a par amet er of zer o i sgi ven t he sear ch i s f or war d. St r i ng i s ter mi nat edby a "RETURN" or "NEWLI NE".

O Operat e - Execute t he curr ent l i ne and f etch t henext l i ne r el at i ve t o cur r ent l i ne f r om t hehi s tory f i l e.

M- di gi t s ( Escape) Def i ne numer i c par amet er , t he di gi t s ar et aken as a par amet er t o the next command. Thecommands t hat accept a paramet er ar e F, B,er ase, D, K, R, P and N.

M- l et t er Sof t - key - The al i as l i st i s sear ched f or an al i asby the name _l et t er and i f an al i as of t hi s namei s def i ned, i t s val ue wi l l be i nser t ed on t hel i ne. The l et t er must not be one of t he abovemeta- f unct i ons.

M- _ The l ast par amet er of t he previ ous command i si nser t ed on t he l i ne.

M- . The l ast par amet er of t he pr evi ous command i si nser t ed on t he l i ne.

M- * At t empt f i l e name generat i on on t he cur r ent word.U Mul t i pl y paramet er of next command by 4.

\ Escape next char act er . Edi t i ng char act er s, t heuser ' s er ase, ki l l and i nt er r upt ( nor mal l y ? )char act ers may be ent ered i n a command l i ne or i na sear ch st r i ng i f pr eceded by a \ . The \ r emovest he next char acter' s edi t i ng f eat ur es ( i f any) .

V Di spl ay ver si on of t he shel l .

vi Edi t i ng Mode There ar e t wo t ypi ng modes. I ni t i al l y, when enter i ng acommand t he user i s i n t he i nput mode. To edi t , t he user

179

Page 180: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 180/193

ent er s cont r ol mode by t ypi ng ESC ( 033 ) and moves t hecur sor t o the poi nt needi ng cor r ect i on and t hen i nsert s ordel etes charact ers or words as needed. Most cont r olcommands accept an opt i onal r epeat count pr i or t o t hecommand.

When i n vi mode on most syst ems, canoni cal processi ng i si ni t i al l y enabl ed and t he command wi l l be echoed agai n i f t he speed i s 1200 baud or gr eat er and i t cont ai ns anycont r ol charact ers or l ess t han one second has el apsed si ncet he prompt was pr i nt ed. The ESC char act er t ermi nat escanoni cal processi ng f or t he r emai nder of t he command andt he user can t hen modi f y t he command l i ne. Thi s scheme hast he advant ages of canoni cal pr ocessi ng wi t h the t ype- aheadechoi ng of r aw mode.

I f t he opt i on vi r aw i s al so set , t he t er mi nal wi l l al wayshave canoni cal pr ocessi ng di sabl ed. Thi s mode may behel pf ul f or cer t ai n t er mi nal s.

I nput Edi t CommandsBy def aul t t he edi t or i s i n i nput mode.

erase ( User def i ned erase charact er as def i ned by t hest t y command, usual l y H or #. ) Del ete pr evi ouscharacter.

W Del et e t he pr evi ous bl ank separated word.D Ter mi nate t he shel l .V Escape next charact er . Edi t i ng char act er s, t he

user' s er ase or ki l l characters may be ent ered i na command l i ne or i n a search st r i ng i f pr ecededby a V. The V r emoves t he next charact er ' sedi t i ng f eat ur es ( i f any) .

\ Escape t he next er ase or ki l l char act er .

Mot i on Edi t Commands These commands wi l l move t he cur sor .

[ count ] l Cur sor f or war d ( r i ght ) one char act er .[ count ] w Cur sor f orward one al pha- numer i c word.[ count ] W Cur sor t o the begi nni ng of t he next word t hat

f ol l ows a bl ank.[ count ] e Cur sor t o end of word.[ count ] E Cur sor t o end of t he cur r ent bl ank del i mi t ed word.[ count ] h Cur sor backwar d ( l ef t ) one char act er .[ count ] b Cursor backward one word.

[ count ] B Cur sor t o pr ecedi ng bl ank separated word.[ count ] f c Fi nd t he next char act er c i n t he cur r ent l i ne.[ count ] Fc Fi nd t he pr evi ous char act er c i n t he cur r ent l i ne.[ count ] t c Equi val ent t o f f ol l owed by h.[ count ] Tc Equi val ent t o F f ol l owed by l .; Repeat s t he l ast si ngl e charact er f i nd command, f ,

F, t , or T., Reverses t he l ast si ngl e charact er f i nd command.0 Cur sor t o st ar t of l i ne.^ Cur sor t o f i r st non- bl ank char act er i n l i ne.

180

Page 181: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 181/193

$ Cur sor t o end of l i ne.

Sear ch Edi t Commands These commands access t he command hi st or y.

[ count ] k Fetch pr evi ous command. Each t i me k i s ent ered t heprevi ous command back i n t i me i s accessed.

[ count ] - Equi val ent to k.[ count ] j Fetch next command. Each t i me j i s ent ered t he

next command f orward i n t i me i s accessed.[ count ] + Equi val ent t o j .[ count ] G The command number count i s f et ched. The def aul t

i s t he l east r ecent hi st ory command./ st r i ng Sear ch backwar d t hr ough hi st ory f or a pr evi ous

command cont ai ni ng st r i ng. St r i ng i s t ermi natedby a "RETURN" or " NEW LI NE". I f st r i ng i s nul lt he pr evi ous st r i ng wi l l be used.

?st r i ng Same as / except t hat sear ch wi l l be i n t hef or war d di r ect i on.

n Sear ch f or next mat ch of t he l ast pat t ern t o / or

? commands.N Sear ch f or next match of t he l ast pat t ern t o / or?, but i n r ever se di r ecti on. Sear ch hi st or y f ort he st r i ng ent ered by t he pr evi ous / command.

 Text Modi f i cat i on Edi t Commands These commands wi l l modi f y t he l i ne.

a Ent er i nput mode and ent er t ext af t er t he cur r entcharacter.

A Append t ext t o t he end of t he l i ne. Equi val ent t o$a.

[ count ] cmot i onc[ count ] mot i on

Del ete cur r ent charact er t hr ough t he charact ermot i on moves t he cur sor t o and enter i nput mode.I f mot i on i s c, t he ent i r e l i ne wi l l be del et edand i nput mode enter ed.

C Del et e t he cur r ent charact er t hr ough t he end of l i ne and ent er i nput mode. Equi val ent t o c$.

S Equi val ent t o cc.D Del et e t he cur r ent charact er t hr ough t he end of 

l i ne.[ count ] dmot i ond[ count ] mot i on

Del ete cur r ent charact er t hr ough t he charact ermot i on moves t he cur sor to. Equi val ent t o d$. I f 

mot i on i s d , t he ent i r e l i ne wi l l be del et ed.i Ent er i nput mode and i nsert t ext bef or e t hecurrent character.

I I nser t t ext bef or e t he begi nni ng of t he l i ne.Equi val ent t o t he t wo char act er sequence i .

[ count ] P Pl ace t he pr evi ous t ext modi f i cat i on bef or e t hecursor.

[ count ] p Pl ace t he pr evi ous text modi f i cat i on af t er t hecursor.

R Ent er i nput mode and r epl ace charact ers on t he

181

Page 182: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 182/193

scr een wi t h charact er s t yped i n over l ay f ashi on.r c Repl ace t he cur r ent char act er wi t h c.[ count ] x Del et e cur r ent char act er .[ count ] X Del ete pr ecedi ng charact er.[ count ] . Repeat t he pr evi ous t ext modi f i cat i on command.~ I nver t t he case of t he cur r ent charact er and

advance the cur sor .[ count ] _ Causes t he count word of t he previ ous command t o

be appended and i nput mode entered. The l ast wor di s used i f count i s omi t t ed.

* Causes an * t o be appended t o t he curr ent word andf i l e name generat i on at t empt ed. I f no match i sf ound, i t r i ngs the bel l . Ot her wi se, t he wor d i sr epl aced by t he mat chi ng pat t ern and i nput mode i sent ered.

Ot her Edi t CommandsMi scel l aneous commands.

u Undo t he l ast t ext modi f yi ng command.

U Undo al l t he t ext modi f yi ng commands perf or med ont he l i ne.[ count ] v Ret urns t he command f c - e ${VI SUAL: - ${EDI TOR: - vi }}

count i n t he i nput buf f er . I f count i s omi t t ed,t hen t he cur r ent l i ne i s used.

L Li ne f eed and pr i nt cur r ent l i ne. Has ef f ect onl yi n cont r ol mode.

J ( New l i ne) Execut e t he cur r ent l i ne, r egar dl essof mode.

M ( Ret ur n) Execut e t he cur r ent l i ne, r egar dl ess of mode.

# Equi val ent t o I #<cr >. Usef ul f or causi ng t hecur r ent l i ne t o be i nser t ed i n t he hi st or y wi t houtbei ng execut ed.

Speci al Commands. The f ol l owi ng si mpl e- commands ar e execut ed i n t he shel lpr ocess. I nput / Out put r edi r ecti on i s per mi t t ed. Fi l edescr i pt or 1 i s t he def aul t out put l ocat i on. Par amet erassi gnment l i st s precedi ng t he command do not r emai n i nef f ect when t he command compl et es unl ess not ed.

: [ a r g . . . ]Par ameter assi gnment s r emai n i n ef f ect af t er t hecommand compl et es. The command onl y expandsparameters . A zero exi t code i s r etur ned.

. f i l e [ ar g . . . ]Par ameter assi gnment s r emai n i n ef f ect af t er t hecommand compl et es. Read and execut e commands f r om f i l eand r et urn. The commands ar e execut ed i n t he cur r entshel l envi r onment . The sear ch path speci f i ed by PATHi s used t o f i nd t he di r ector y cont ai ni ng f i l e. I f anyargument s arg ar e gi ven, t hey become t he posi t i onalparameters . Ot herwi se t he posi t i onal par ameters ar eunchanged.

182

Page 183: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 183/193

al i as [ - t x ] [ name[ =val ue ] . . . ]

Al i as wi t h no ar gument s pr i nt s t he l i st of al i ases i nt he f orm name=val ue on st andard out put . An al i as i sdef i ned f or each name whose val ue i s gi ven. A t r ai l i ngspace i n val ue causes t he next word t o be checked f orl i st t r acked al i ases. The val ue of a t r acked al i as i st he f ul l pat h name corr espondi ng t o t he gi ven name. The val ue becomes undef i ned when t he val ue of PATH i sr eset but t he al i ases r emai ned t r acked. Wi t hout t he - tf l ag, f or each name i n t he argument l i st f or whi ch noval ue i s gi ven, t he name and val ue of t he al i as i spr i nt ed. The - x f l ag i s used t o set or pr i nt expor t edal i ases. An expor t ed al i as i s def i ned across sub- shel lenvi r onment s. Al i as r etur ns t r ue unl ess a name i sgi ven f or whi ch no al i as has been def i ned.

bg [ %j ob ] Thi s command i s onl y bui l t - i n on syst ems t hat suppor t j ob cont r ol . Put s t he speci f i ed j ob i nt o t he

backgr ound. The cur r ent j ob i s put i n t he backgr oundi f j ob i s not speci f i ed.

break [ n ]Exi t f r om t he encl osi ng f or whi l e unt i l or sel ect l oop,i f any. I f n i s speci f i ed t hen br eak n l evel s.

cont i nue [ n ]Resume t he next i t er at i on of t he encl osi ng f or whi l eunt i l or sel ect l oop. I f n i s speci f i ed t hen r esume att he n- t h encl osi ng l oop.

cd [ arg ]cd ol d new

 Thi s command can be i n ei t her of t wo f or ms. I n t hef i r st f or m i t changes t he cur r ent di r ector y t o ar g. I f  ar g i s - t he di r ect or y i s changed t o the pr evi ousdi r ectory. The shel l parameter HOME i s the def aul targ. The parameter PWD i s set t o t he curr entdi r ectory. The shel l parameter CDPATH def i nes t hesear ch pat h f or t he di r ect or y cont ai ni ngar g. Al t er nat i vedi r ect or ynamesar e a col on ( : ) . Thedef aul t pat h i s <nul l > ( speci f yi ng the cur r entdi r ectory) . Not e t hat t he cur r ent di r ectory i sspeci f i ed by a nul l path name, whi ch can appeari mmedi atel y af t er t he equal si gn or between the col ondel i mi t er s anywher e el se i n t he pat h l i st . I f argbegi ns wi t h a / t hen t he search path i s not used.

Ot her wi se, each di r ect or y i n t he pat h i s sear ched f orarg.

 The second f or m of cd subst i t ut es t he st r i ng new f ort he st r i ng ol d i n t he cur r ent di r ectory name, PWD andt r i es t o change t o t hi s new di r ect or y.

 The cd command may not be execut ed by r ksh.

eval [ arg . . . ]

183

Page 184: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 184/193

The ar gument s ar e read as i nput t o the shel l and t her esul t i ng command(s) executed.

exec [ ar g . . . ]Par ameter assi gnment s r emai n i n ef f ect af t er t hecommand compl et es. I f ar g i s gi ven, t he commandspeci f i ed by the ar gument s i s execut ed i n pl ace of t hi sshel l wi t hout cr eat i ng a new pr ocess. I nput / out putargument s may appear and af f ect t he cur r ent process.I f no ar gument s ar e gi ven, t he ef f ect of t hi s commandi s t o modi f y f i l e descri pt or s as pr escr i bed by t hei nput / out put redi rect i on l i st . I n t hi s case, any f i l edescr i pt or number s great er t han 2 t hat are opened wi t ht hi s mechani sm are cl osed when i nvoki ng anot herprogr am.

exi t [ n ]Causes t he shel l t o exi t wi t h t he exi t st at us speci f i ed by n. I f n i s omi t t ed t hen t he exi t st at us i s t hat of t he l ast commandexecut ed. An end- of - f i l e wi l l al so cause t he shel l t o exi t except

f or a shel l whi ch has the i gnor eeof opt i on ( see set bel ow) t ur nedon.

exi t [ name] The gi ven names ar e mar ked f or aut omat i c export t o t heenvi r onment of subsequent l y- execut ed commands.

f c [ - e ename ] [ - nl r ] [ f i rs t ] [ l ast ]f c - e - [ ol d=new ] [ command ]

I n t he f i r st f or m, a r ange of commands f r om f i r st t ol ast i s sel ect ed f r omt he l ast HI STSI ZE commands t hatwere typed at t he t ermi nal . The argument s f i r st andl ast may be speci f i ed as a number or as a st r i ng. Ast r i ng i s used t o l ocat e t he most r ecent commandst ar t i ng wi t h t he gi ven st r i ng. A negati ve number i sused as an of f set t o t he curr ent command number . I f t he f l ag - l , i s sel ect ed, t he commands are l i st ed onst andard out put . Ot herwi se, t he edi t or pr ogr am enamei s i nvoked on a f i l e cont ai ni ng t hese keyboardcommands. I f ename i s not suppl i ed, t hen t he val ue of t he par ameter FCEDI T ( def aul t / bi n/ ed) i s used as t heedi t or. When edi t i ng i s compl et e, t he edi t edcommand( s) i s execut ed. I f l ast i s not speci f i ed, i twi l l be set to f i r s t . I f f i r s t i s not speci f i ed thedef aul t i s t he pr evi ous command f or edi t i ng and - 16 f orl i st i ng. The f l ag - r r ever ses the or der of t hecommands and t he f l ag - n suppresses command number s

when l i st i ng. I n t he second f or m t he most r ecentcommand i n the hi st ory whose f i r st l et t ers matchcommand i s r e- execut ed af t er t he subst i t ut i on ol d=newi s per f ormed.

f g [ %j ob ] Thi s command i s onl y bui l t - i n on syst ems t hat suppor t j ob cont r ol . I f j ob i s speci f i ed, i t br i ngs i t t o t hef or egr ound. Ot her wi se, t he cur r ent j ob i s br ought i nt ot he f oregr ound.

184

Page 185: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 185/193

  j obs [ - l ]

Li sts t he acti ve j obs; gi ven t he - l opt i on l i stspr ocess i d' s i n addi t i on t o the nor mal i nf or mat i on.

ki l l [ - s i g ] process . . .Sends ei t her t he TERM ( t er mi nat e) si gnal or t hespeci f i ed si gnal t o t he speci f i ed j obs or pr ocesses.Si gnal s are ei t her gi ven by number or by names ( asgi ven i n <si gnal . h>, st r i pped of t he pr ef i x "SI G") . The si gnal names ar e l i st ed by ' ki l l - l ' . There i s nodef aul t , sayi ng j ust ' ki l l ' does not send a si gnal t ot he cur r ent j ob. I f t he si gnal bei ng sent i s TERM( t ermi nate) or HUP ( hangup) , t hen t he j ob or pr ocesswi l l be sent a CONT ( cont i nue) si gnal i f i t i s st opped. The ar gument process can be ei t her a process i d or a j ob.

l et arg . . .Each arg i s an ari t hmet i c expr essi on t o be eval uated.

Al l cal cul at i ons are done as l ong i nt eger s and no checkf or over f l ow i s per f or med. Expr essi ons consi st of const ant s, named paramet ers, and operat ors. Thef ol l owi ng set of oper at or s, l i st ed i n or der of decr easi ng precedence, are i mpl ement ed:- unary mi nus! l ogi cal negat i on* / % mul t i pl i cat i on, di vi si on, r emai nder+ - addi t i on, subt r acti on< > compar i son== ! = equal i t y i nequal i t y= ar i t hmet i c r epl acement

Sub- expr essi ons i n par ent heses () ar e eval uated f i r stand can be used t o overr i de t he above precedence rul es. The eval uat i on wi t hi n a precedence group i s f r om r i ghtt o l ef t f or t he = oper at or and f r om l ef t t o r i ght f orthe others.

A parameter name must be a val i d i dent i f i er . When aparameter i s encount ered, t he val ue associ ated wi t h t heparameter name i s subst i t ut ed and expr essi on eval uat i onr esumes. Up t o ni ne l evel s of r ecur si on ar e per mi t t ed. The r et urn code i s 0 i f t he val ue of t he l astexpr essi on i s non- zero, and 1 ot herwi se.

newgr p [ ar g . . . ]Equi val ent t o exec newgr p ar g . . . .

pr i nt [ - Rnprsu[ n ] ] [ arg . . . ] The shel l output mechani sm. Wi t h no f l ags or wi t h f l ag- , t he ar gument s ar e pr i nt ed on st andard out put asdescr i bed by echo( 1) . I n r aw mode, - R or - r , t heescape convent i ons of echo are i gnored. The - R opt i onwi l l pr i nt al l subsequent ar gument s and opt i ons ot hert han - n. The - p opt i on causes t he argument s t o be

185

Page 186: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 186/193

wr i t t en ont o the pi pe of t he pr ocess spawned wi t h | &i nst ead of st andar d out put . The - s opt i on causes t hear gument s t o be wr i t t en ont o t he hi st or y f i l e i nst eadof st andard out put . The - u f l ag can be used t o speci f ya one di gi t f i l e descr i pt or uni t number n on whi ch t heout put wi l l be pl aced. The def aul t i s 1. I f t he f l ag- n i s used, no new- l i ne i s added t o the out put .

r ead [ - pr su[ n ] ] [ name?pr ompt ] [ name . . . ] The shel l i nput mechani sm. One l i ne i s r ead and i sbr oken up i nt o words usi ng t he charact ers i n I FS assepar at or s. I n r aw mode, - r , a \ at t he end of a l i nedoes not si gni f y l i ne cont i nuat i on. The f i r st wor d i sassi gned t o the f i r st name, t he second word t o t hesecond name, etc. , wi t h l ef t over words assi gned t o t hel ast name. The - p opt i on causes t he i nput l i ne t o bet aken f r om t he i nput pi pe of a pr ocess spawned by t heshel l usi ng | &. I f t he - s f l ag i s pr esent , t he i nputwi l l be saved as a command i n t he hi st ory f i l e. Thef l ag - u can be used t o speci f y a one di gi t f i l e

descri pt or uni t t o r ead f r om. The f i l e descri pt or canbe opened wi t h t he exec speci al command. The def aul tval ue of n i s 0. I f name i s omi t t ed t hen REPLY i s usedas t he def aul t name. The r etur n code i s 0 unl ess anend- of - f i l e i s encount er ed. An end- of - f i l e wi t h t he - popt i on causes cl eanup f or t hi s pr ocess so t hat anothercan be spawned. I f t he f i r st argument cont ai ns a ?,t he r emai nder of t hi s word i s used as a prompt when t heshel l i s i nteract i ve. I f t he gi ven f i l e descr i ptor i sopen f or wr i t i ng and i s a t er mi nal devi ce t hen t hepr ompt i s pl aced on t hi s uni t . Ot herwi se t he pr ompt i si ssued on f i l e descr i pt or 2. The r et ur n code i s 0unl ess an end- of - f i l e i s encount er ed.

r eadonl y [ name . . . ] The gi ven names ar e mar ked r eadonl y and t hese namescannot be changed by subsequent ass i gnment .

return [ n ]Causes a shel l f unct i on t o ret ur n t o the i nvoki ngscr i pt wi t h t he r et ur n st at us speci f i ed by n. I f n i somi t t ed t hen t he r et ur n st at us i s t hat of t he l astcommand execut ed. I f r etur n i s i nvoked whi l e not i n af unct i on t hen i t i s t he same as an exi t .

set [ - aef hkmnostuvx ] [ - o opt i on . . . ] [ ar g . . . ] The f l ags f or t hi s command have meani ng as f ol l ows:

- a Al l subsequent parameters t hat are def i ned areaut omat i cal l y expor t ed.- e I f t he shel l i s non- i nt er acti ve and i f a

command f ai l s, execut e t he ERR t r ap, i f set ,and exi t i mmedi atel y. Thi s mode i s di sabl edwhi l e r eadi ng pr of i l es.

- f Di sabl es f i l e name gener at i on.- h Each command whose name i s an i dent i f i er

becomes a t r acked al i as when f i r st encount ered.- k Al l parameter assi gnment argument s are pl aced

186

Page 187: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 187/193

i n t he envi r onment f or a command, not j ustt hose t hat pr ecede t he command name.

- m Backgr ound j obs wi l l r un i n a separ ate pr ocessgr oup and a l i ne wi l l pr i nt upon compl et i on. The exi t st at us of background j obs i s r eport edi n a compl et i on message. On syst ems wi t h j obcont r ol , t hi s f l ag i s t ur ned on aut omat i cal l yf or i nt eract i ve shel l s .

- n Read commands but do not execut e t hem.- o The f ol l owi ng argument can be one of t he

f ol l owi ng opt i on names:al l export Same as - a.er r exi t Same as - e.

vi Put s t he user i n i nsert mode of a vi

t yped i n vi mode.

emacs Put s t he user i n an emacs st yl e i n-l i ne edi t or f or command ent r y.

gmacs Put s t he user i n a gmacs st yl e i n-l i ne edi t or f or command ent r y.

i gnor eeof The shel l wi l l not exi t on end- of -f i l e. The command exi t must beused.

keywor d Same as - k.mar kdi r s Al l di r ect or y names r esul t i ng f r omf i l e name gener at i on have a t r ai l i ng/ appended.

moni t or Same as - m.noexec Same as - n.nogl ob Same as - f .nounset Same as - u.ver bose Same as - v.t r ackal l Same as - h.

s tyl e i n- l i ne edi tor unt i l hi t t i ngt he escape charact er 033. Thi s put st he user i n move mode. A r et urnsends t he l i ne.

vi r aw Each char act er i s pr ocessed as i t i s

xt r ace Same as - x.I f no opt i on name i s suppl i ed t hen t he cur r entopt i on set t i ngs ar e pr i nt ed.

- s Sor t t he posi t i onal par amet er s.- t Exi t af t er r eadi ng and execut i ng one command.- u Treat unset parameters as an er r or when

substi t ut i ng.- v Pr i nt shel l i nput l i nes as t hey ar e r ead.- x Pr i nt commands and t hei r argument s as t hey are

execut ed.

- Tur ns of f - x and - v f l ags and st ops exami ni ngargument s f or f l ags.- - Do not change any of t he f l ags; usef ul i n

set t i ng $1 t o a val ue begi nni ng wi t h - . I f noar gument s f ol l ow t hi s f l ag t hen t he posi t i onalpar amet ers are unset .

Usi ng + r ather t han - causes t hese f l ags t o be tur nedof f . These f l ags can al so be used upon i nvocat i on of t he shel l . The cur r ent set of f l ags may be f ound i n

187

Page 188: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 188/193

Page 189: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 189/193

new i nst ance of t he parameter name i s cr eat ed. Theparameter val ue and type ar e rest ored when t he f unct i oncompl et es. The f ol l owi ng l i st of at t r i but es may bespeci f i ed:- F Thi s f l ag pr ovi des UNI X t o host - name f i l e mappi ng

on non- UNI X machi nes.- L Lef t j ust i f y and r emove l eadi ng bl anks f r om val ue.

I f n i s non- zer o, i t def i nes t he wi dt h of t hef i el d, ot her wi se i t i s det er mi ned by the wi dt h of t he val ue of f i r st assi gnment . When t he paramet eri s assi gned, i t i s f i l l ed on t he r i ght wi t h bl anksor t r uncat ed, i f necessar y, t o f i t i nt o t he f i el d.Leadi ng zeros ar e r emoved i f t he - Z f l ag i s al soset . The - R f l ag i s t ur ned of f .

- R Ri ght j ust i f y and f i l l wi t h l eadi ng bl anks. I f ni s non- zer o, i t def i nes t he wi dt h of t he f i el d,ot her wi se i t i s det er mi ned by t he wi dt h of t heval ue of f i r st assi gnment . The f i el d i s l ef tf i l l ed wi t h bl anks or t r uncat ed f r om t he end i f t he par ameter i s reassi gned. The L f l ag i s tur ned

o f f .- Z Ri ght j ust i f y and f i l l wi t h l eadi ng zer os i f t hef i r st non- bl ank char acter i s a di gi t and t he - Lf l ag has not been set . I f n i s non- zer o, i tdef i nes t he wi dt h of t he f i el d, ot her wi se i t i sdet er mi ned by t he wi dt h of t he val ue of f i r stassi gnment .

- e Tag t he parameter as havi ng an err or. Thi s t ag i scur r ent l y unused by the shel l and can be set orcl ear ed by t he user .

- f The names r ef er t o f unct i on names r ather t hanparamet er names. No assi gnment s can be made andt he onl y ot her val i d f l ag i s - x.

- i Par amet er i s an i nt eger . Thi s makes ar i t hmet i cf aster. I f n i s non- zer o, i t def i nes the out putar i t hmet i c base, ot her wi se t he f i r st assi gnmentdetermi nes t he out put base.

- l Al l upper - case char act er s conver t ed t o l ower- case. The upper - case f l ag, - u i s t urned of f .

- p The out put of t hi s command, i f any, i s wr i t t enont o t he t wo- way pi pe.

- r The gi ven names are marked r eadonl y and t hesenames cannot be changed by subsequent assi gnment .

- t Tags t he named par amet er s. Tags are userdef i nabl e and have no speci al meani ng t o theshel l .

- u Al l l ower - case char act er s ar e conver t ed t o upper -

case char acters. The l ower - case f l ag, - l i st ur ned of f .- x The gi ven names are marked f or aut omat i c expor t t o

t he envi r onment of subsequent l y- execut ed commands.

Usi ng + r ather t han - causes t hese f l ags t o be tur nedof f . I f no name argument s ar e gi ven but f l ags ar especi f i ed, a l i st of names ( and opt i onal l y t he val ues )of t he parameters whi ch have these f l ags set i spr i nt ed. ( Usi ng + r at her t han - keeps t he val ues t o be

189

Page 190: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 190/193

pr i nt ed. ) I f no names and f l ags ar e gi ven, t he namesand at t r i but es of al l par amet er s are pr i nt ed.

ul i mi t [ - cdf mpt ] [ n ]- c I mposes a si ze l i mi t of n bl ocks on t he si ze of 

cor e dumps ( not on UMAX V) .- d I mposes a si ze l i mi t of n bl ocks on t he si ze of 

t he data area ( not on UMAX V) .- f I mposes a si ze l i mi t of n bl ocks on f i l es wr i t t en

by chi l d pr ocesses ( f i l es of any si ze may ber ead) .

- m I mposes a sof t l i mi t of n bl ocks on t he si ze of physi cal memor y ( not on UMAX V) .

- p Changes t he pi pe si ze t o n ( not on UMAX V) .- t I mposes a t i me l i mi t of n seconds t o be used by

each pr ocess ( not on UMAX V) .

I f no opt i on i s gi ven, - f i s assumed. I f n i s notgi ven, t he cur r ent l i mi t i s pr i nt ed.

umask [ nnn ] The user f i l e- cr eat i on mask i s set t o nnn ( seeumask(2)) . I f nnn i s omi t t ed, t he cur r ent val ue of t hemask i s pr i nt ed.

unal i as name . . . The paramet er s gi ven by t he l i st of names ar e r emovedf rom the al i as l i s t .

unset [ - f ] name . . . The paramet er s gi ven by t he l i st of names ar eunassi gned, i . e. , t hei r val ues and at t r i but es ar eer ased. Readonl y var i abl es cannot be unset. I f t hef l ag, - f , i s set , t hen t he names ref er t o f uncti onnames .

wai t [ n ]Wai t f or t he speci f i ed pr ocess and r epor t i t st er mi nat i on st at us. I f n i s not gi ven t hen al lcur r ent l y act i ve chi l d pr ocesses ar e wai t ed f or . Ther etur n code f r om t hi s command i s t hat of t he pr ocesswai t ed f or .

whence [ - v ] name . . .For each name, i ndi cate how i t woul d be i nt erpr eted i f used as a command name.

 The f l ag, - v, produces a mor e ver bose r eport .

I nvocat i on.I f t he shel l i s i nvoked by exec(2) , and t he f i r st char acterof ar gument zer o ($0) i s - , t hen t he shel l i s assumed to bea l ogi n shel l and commands are read f r om/ et c/ pr of i l e andt hen f r om ei t her . pr of i l e i n t he cur r ent di r ector y or$HOME/ . pr of i l e, i f ei t her f i l e exi st s. Next , commands ar er ead f r om t he f i l e named by per f ormi ng parametersubst i t ut i on on t he val ue of t he envi r onment parameter ENV

190

Page 191: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 191/193

Page 192: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 192/193

 EXI T STATUS

Er r or s det ect ed by t he shel l , such as synt ax er r ors, causet he shel l t o r et ur n a non- zer o exi t st at us. I f t he shel l i sbei ng used non- i nt er act i vel y then execut i on of t he shel lf i l e i s abandoned. Ot her wi se, t he shel l r et ur ns the exi tst atus of t he l ast command execut ed ( see al so t he exi tcommand above) .

FI LES/ et c/ passwd/ et c/ prof i l e$HOME/ . pr of i l e$HOME/ . kshr c/ t mp/ sh*/ dev/ nul l

SEE ALSOcat ( 1) , cd( 1) , echo( 1) , emacs( 1) , env(1) , gmacs( 1) ,newgr p( 1) , shl ( 1) , t est ( 1) , umask( 1) , vi ( 1) .

dup( 2) , exec(2) , f or k( 2) , pi pe( 2) , si gnal ( 2) , umask( 2) ,ul i mi t ( 2) , wai t ( 2) , rand( 3C) , a. out ( 4) , prof i l e( 4) i n t heUMAX V Pr ogr ammer ' s Ref er ence Manual .envi r on( 7) i n t he UMAX V Admi ni st r ator ' s Ref erence Manual .

CAVEATSI f a command whi ch i s a t r acked al i as i s execut ed, and thena command wi t h the same name i s i nst al l ed i n a di r ect ory i nt he sear ch pat h bef ore t he di r ect ory wher e t he or i gi nalcommand was f ound, t he shel l wi l l cont i nue to exec t heor i gi nal command. Use t he - t opt i on of t he al i as command t ocorr ect t hi s s i t uat i on.

I f movi ng t he cur r ent di r ectory or one above i t , pwd may notgi ve t he cor r ect r esponse. Use the cd command wi t h a f ul lpat h name t o cor r ect t hi s si t uat i on.

Some very ol d shel l scr i pt s cont ai n a ^ as a synonym f or t hepi pe char act er | .

NOTESÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ

NOTESÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ

I NDEX

. net rc f i l e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146

. pr of i l e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14Backsl ash. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13Bour neShel l . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Chi l d pr ocess. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39Chmod. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4Excl amat i on mar k ( ! ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46Gr ave accent mar ks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36HOME var i abl e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

192

Page 193: b Sh Programming

7/28/2019 b Sh Programming

http://slidepdf.com/reader/full/b-sh-programming 193/193