| help | account  


Linux and Unix Shell Programming
View Larger Image
David Tansley
Addison-Wesley, Paperback, Published January 2000, 504 pages, ISBN 0201674726
List Price: $68.99
Our Price: $53.50
You Save: $15.49 (22% Off)


FREE Shipping on Orders over $40!*
Availability: Out-Of-Stock

Customer Reviews: 2     Average Customer Rating:

Write a Review and tell the world about this title!

Books on similar topics, in best-seller order:

Books from the same publisher, in best-seller order:

With more and more systems being run under UNIX and Linux, the ability to program and customize the shell quickly and reliably to get the best out of any individual system is becoming a more and more important skill for anyone operating and maintaining these systems.

This comprehensive book is a practical, easy-to-use guide to programming and using the Bourne shell for beginners and experienced users - the Bourne shell is the standard shell for UNIX, and is also fully backward compatible to the Linux BASH shell. This book will fully illustrate the ability of the shell to unlock the real potential of UNIX and Linux, and aims to get the reader up, running and creating robust shell scripts for real tasks and situations as quickly as possible – shell scripts that will work on any mainstream UNIX or Linux machine.

If you are new to UNIX and Linux or if you are a power user in waiting then this book is for you. Most shell programming books merely annotate manual pages and syntax , but in this book users of all abilities will find plenty of practical working examples - all of which are available as full code script via an ftp site – as well as a host of tips, tricks and code one-liners, that will save you time on a day-to-day basis. The book is organized into self-contained chapters on individual topics for ease of reference.

Linux and Unix Shell Programming is structured in 5 parts:
Part 1 deals with the Shell itself, introducing the various commands and syntax
Part 2 introduces all the major text filtering tools available to the shell
Part 3 explains the login environment and customization
Part 4 covers basic shell programming
Part 5 introduces more advanced shell programming techniques

Features:
Practical advice and guidance for beginner and advanced programmers alike
Easy access to specific topics through self-contained chapters for day-to-day reference.
Every chapter backed up with real, practical examples
Full of tested time-saving code one-liners
All programming examples and code available in electronic form

Table of Contents

Acknowledgements xviii
Introduction xix
The Bourne shell xx
Shell portability xx
Organization of the book xx
The shell xxi
Text file filtering xxi
Login environment xxi
Basic shell programming xxi
Better scripting skills xxii
Assumptions xxii
Conventions used in this book xxii

PART 1- The shell 1

1 File security and permissions 3
1.1 Files 4
1.2 Types of files 5
1.3 Permissions 5
1.4 Changing permission bits 7
1.4.1 Symbolic mode 7
1.4.2 chmod examples 8
1.4.3 Absolute mode 9
1.4.4 More chmod examples 10
1.4.5 Absolute or symbolic, your choice 11
1.5 Directories 11
1.6 Set-uid 12
1.6.1 Why use set-uid? 13
1.6.2 Adding set-uid permission examples 13
1.7 chown and chgrp 14
1.7.1 chown examples 14
1.7.2 chgrp examples 15
1.7.3 Finding out what groups you belong to 15
1.7.4 Finding out what groups other users belong to 15
1.8 umask 16
1.8.1 How to work out umask values 16
1.8.2 Common umask values 17
1.9 Symbolic (soft) links 18
1.9.1 Using soft links saves multiple copies of files 18
1.9.2 Example of using a soft link 19
1.10 Conclusion 20

2 Using find and xargs 21
2.1 find options 22
2.1.1 Find files by name 23
2.1.2 Find files by perm mode 24
2.1.3 Ignoring directories 24
2.1.4 Find files by user and nouser 24
2.1.5 Find files by group and nogroup 25
2.1.6 Find files by modification times 25
2.1.7 Finding files that are newer or older in days or minutes 25
2.1.8 Find files by type 26
2.1.9 Find files by size 26
2.1.10 Find files by depth 27
2.1.11 Find files by mount 27
2.1.12 Find files using cpio 27
2.1.13 Using exec or OK to run shell commands 28
2.1.14 Find examples 29
2.2 Xargs 30
2.3 Conclusion 31

3 Running commands unattended 32
3.1 Cron and crontab 33
3.1.1 crontab fields 33
3.1.2 crontab entry examples 33
3.1.3 crontab options 34
3.1.4 Creating a new crontab entry 35
3.1.5 Listing your crontab 35
3.1.6 Editing your crontab 36
3.1.7 Removing your crontab 36
3.1.8 Restoring a lost crontab 36
3.2 The at command 37
3.2.1 Submitting commands and scripts using at 38
3.2.2 Listing jobs submitted 39
3.2.3 Deleting a submitted job 39
3.3 The & command 40
3.3.1 Submitting a command to the background 40
3.3.2 Checking the process using ps 41
3.3.3 Killing a background job 41
3.4 The nohup command 42
3.4.1 Submitting a job using nohup 42
3.4.2 Submitting several jobs at once 43
3.5 Conclusion 43

4 Filename substitution 44
4.1 Using the 44
4.2 Using the ? 45
4.3 Using [Ä.Ä.Ä.Ä] and [Ä!Ä.Ä.Ä.Ä] 46
4.4 Conclusion 47
5 Shell input and output 48
5.1 echo 48
5.2 read 51
5.3 cat 52
5.4 Pipes 54
5.5 tee 55
5.6 Standard input, output and errors 56
5.6.1 Standard input 57
5.6.2 Standard output 57
5.6.3 Standard error 57
5.7 File redirection 57
5.7.1 Redirecting standard output 58
5.7.2 Redirecting standard input 59
5.7.3 Redirecting standard error 60
5.8 Combining standard output and error 61
5.9 Merging standard output and standard error 61
5.10 exec 62
5.11 Using file descriptors 62
5.12 Conclusion 63
6 Command execution order 64

6.1 Using && 64
6.2 Using || 65
6.3 Grouping commands using () and {} 66
6.4 Conclusion 67

PART 2 - Text filtering 69

7 Introducing regular expressions 71
7.1 Matching a single character with a period 72
7.2 Matching a string or character at the beginning of a line with a'^ 73
7.3 Matching a string or character at the end of a line with $ 74
7.4 Matching a single or string of characters with 75
7.5 Escaping the meaning of a special character using \ 76
7.6 Matching ranges or sets using [] 76
7.7 Matching a specific number of occurrenceswith \{ \} 78
7.8 Conclusion 81

8 The grep family 82
8.1 grep 83
8.1.1 Please quote me 83
8.1.2 grep options 84
8.1.3 Searching more than one file 84
8.1.4 Line matches 84
8.1.5 Line numbers 84
8.1.6 Do not match 85
8.1.7 Getting an exact match 85
8.1.8 Being case-sensitive 85
8.2 grep and regular expressions 86
8.2.1 Pattern ranges 86
8.2.2 Don't match at the beginning of a line 86
8.2.3 Trapping upper and lower cases 86
8.2.4 Matching any characters 87
8.2.5 Date searching 87
8.2.6 Combining ranges 88
8.2.7 Occurrences in a pattern 88
8.2.8 Using grep to match either or all patterns 89
8.2.9 Blank lines 89
8.2.10 Matching special characters 89
8.2.11 Searching for formatted filenames 90
8.2.12 Searching for ip addresses 90
8.3 Class names 90
8.3.1 Pattern matching with wildcards 91
8.4 grep on the system 92
8.4.1 Directories 92
8.4.2 passwd file 92
8.4.3 Using the ps command 93
8.4.4 Using grep on a string 93
8.5 egrep 94
8.6 Conclusion 95

9 Introducing awk 96
9.1 Calling awk 97
9.2 awk script 98
9.2.1 Patterns and actions 98
9.2.2 Fields and records 99
9.2.3 Regular expressions and operators in awk 103
9.2.4 Metacharacters 103
9.2.5 Conditional operators 103
9.2.6 awk built-in variables 108
9.2.7 NF, NR and FILENAME 109
9.2.8 awk operators 110
9.2.9 Built-in string functions 114
9.2.10 String escape sequences 118
9.2.11 awk output functions (printf) 119
9.2.12 printf modifiers 119
9.2.13 awk arrays 126
9.3 Conclusion 130

10 Using sed 131
10.1 How sed reads data 132
10.2 Calling sed 132
10.2.1 Saving sed output 133
10.2.2 Ways to find text in a file using sed 133
10.2.3 Basic sed editing commands 133
10.3 sed and regular expressions 134
10.4 Basic sed editing examples 134
10.4.1 Displaying lines using p(rint) 134
10.4.2 Range printing 135
10.4.3 Pattern printing 135
10.4.4 Searching using a pattern and line number 135
10.4.5 Matching a metacharacter 136
10.4.6 Displaying the whole file 136
10.4.7 Any characters 136
10.4.8 First line 136
10.4.9 Last line 137
10.4.10 Printing line numbers 137
10.4.11 Appending text 137
10.4.12 Creating a sed script file 138
10.4.13 Inserting text 139
10.4.14 Changing text 140
10.4.15 Deleting text 141
10.4.16 Substituting text 142
10.5 Modifying strings with substitute (&) 143
10.6 Writing to a file within sed 144
10.7 Reading text from a file 144
10.8 Quitting after a match 145
10.9 Displaying control characters in a file 146
10.10 Using sed on the system 147
10.10.1 Dealing with control characters 147
10.10.2 Dealing with report output 149
10.10.3 Getting rid of numbers at the beginning of a line 149
10.10.4 Appending text 150
10.10.5 Passing values from the shell to sed 150
10.10.6 Assigning a shell variable from sed output 151
10.11 Quick one-liners 151
10.12 Conclusion 153

11 Merge and divide 154
11.1 Using sort 154
11.1.1 A word to the wise 155
11.1.2 sort options 155
11.1.3 Saving output 156
11.1.4 How sort starts 156
11.1.5 How sort references fields 156
11.1.6 Is the file already sorted? 157
11.1.7 Basic sort 157
11.1.8 Reverse sort 157
11.1.9 Sorting on a specific field 158
11.1.10 Numeric field sorting 158
11.1.11 Unique sorting 159
11.1.12 Other sort methods using -k 159
11.1.13 Order of sort keys using -k 160
11.1.14 Specifying sort sequences 160
11.1.15 Using pos 160
11.1.16 Sort output using head and tail 161
11.1.17 Using sort output with awk 162
11.1.18 Merging two sorted files 162
11.2 sort on the system 163
11.3 Using uniq 164
11.3.1 Occurrences 165
11.4 Using join 166
11.4.1 Joining two files 167
11.5 Using cut 169
11.5.1 Using the field separator 170
11.5.2 Cutting specific fields 170
11.6 Using paste 171
11.6.1 Specifying columns 172
11.6.2 Using a different field separator 172
11.6.3 Piping into a paste command 173
11.7 Using split 173
11.8 Conclusion 175

12 Using tr 176
12.1 About tr 176
12.1.1 Character ranges 177
12.1.2 Saving output 178
12.1.3 Getting rid of repeated characters 178
12.1.4 Deleting blank lines 178
12.1.5 Upper to lower case 179
12.1.6 Lower to upper case 180
12.1.7 Deleting certain characters 180
12.1.8 Translating control characters 181
12.1.9 Quick conversions 182
12.1.10 Matching more than one character 183
12.2 Conclusion 183

PART 3 - The login environment 185

13 The login environment 187
13.1 The /etc/profile 188
13.2 Your $HOME .profile 191
13.3 Using stty 193
13.4 Creating a .logout file 195
13.5 Conclusion 196

14 Environment and shell variables 197
14.1 What is a shell variable? 197
14.2 Local variables 198
14.2.1 Displaying a variable 199
14.2.2 Clearing a variable 199
14.2.3 Displaying all local shell variables 200
14.2.4 Variable values together 200
14.2.5 Testing if a variable is set (substitution) 200
14.2.6 Using variables to hold arguments for system commands 202
14.2.7 Making a variable read-only 202
14.3 Environment variables 203
14.3.1 To assign an environment variable 204
14.3.2 Displaying environment variables 204
14.3.3 Clearing environment variables 204
14.3.4 Built-in shell variables 205
14.3.5 Other environment variables 208
14.3.6 Using the set command 209
14.3.7 Exporting variables to child processes 210
14.4 Positional variable parameters 211
14.4.1 Using positional parameters in scripts 212
14.4.2 Passing parameters to system commands 213
14.4.3 Special variable parameters 213
14.4.4 Last exit status 215
14.5 Conclusion 216

15 Quoting 217
15.1 The need for quoting 217
15.2 Double quotes 218
15.3 Single quotes 219
15.4 Back quote 219
15.5 Backslash 220
15.6 Conclusion 222

PART 4 - Basic shell programming 223

16 Introduction to shell scripts 225
16.1 Reasons for using a shell script 225
16.1.1 Try out new ideas 226
16.2 What a script contains 226
16.3 Running a script 226
16.4 Conclusion 228

17 Conditional testing 229
17.1 Testing for a file status 229
17.2 Using logical operators with tests 231
17.3 Testing strings 232
17.4 Testing numbers 233
17.5 Using expr 234
17.5.1 Incrementing counters 235
17.5.2 Testing for a number 235
17.5.3 Pattern matching 236
17.6 Conclusion 236

18 Control flow structures 237
18.1 Exit statuses 237
18.2 Control structures 238
18.2.1 Flow control 239
18.2.2 Iteration 239
18.3 if then else statements 240
18.3.1 Simple if statements 240
18.3.2 Testing values of variables 241
18.3.3 Checking the output of grep 242
18.3.4 Testing the output of grep on a variable 242
18.3.5 Checking the outcome of a file copy 243
18.3.6 Testing for current directory 244
18.3.7 Testing for file permissions 244
18.3.8 Testing for parameters passed to a script 244
18.3.9 Determining if a script is in interactive mode 245
18.3.10 Simple if else statements 246
18.3.11 Testing for set variables 246
18.3.12 Checking the user who is running a script 246
18.3.13 Using script parameters to pass down to a system command 247
18.3.14 Using the null: command 248
18.3.15 Testing the outcome of a directory creation 248
18.3.16 Another copy 249
18.3.17 More than one if statement 250
18.3.18 Testing and setting environment variables 250
18.3.19 Checking the last command status 252
18.3.20 Adding and checking integer values 252
18.3.21 Simple security login script 254
18.3.22 Using elif 255
18.3.23 Multiple checks using elif 255
18.3.24 Checking multiple file locations 256
18.4 Case statement 257
18.4.1 Simple case statement 258
18.4.2 Using | with pattern matching 259
18.4.3 Prompting for a y or n 260
18.4.4 Case and passing command parameters 260
18.4.5 Trapping input with no pattern commands 261
18.4.6 Default variable values 263
18.5 for loop 264
18.5.1 Simple for loop 264
18.5.2 Printing out a string list 265
18.5.3 Using ls with the for loop 265
18.5.4 Using parameters with a for loop 266
18.5.5 Pinging servers with a for loop 267
18.5.6 Backing up files using a for loop 268
18.5.7 Multiple translation 268
18.5.8 Multiple sed deletes 268
18.5.9 Counting iterations 269
18.5.10 for loops and here documents 269
18.5.11 Nested for loops 270
18.6 until loop 271
18.6.1 Simple until loop 272
18.6.2 Monitoring for a file presence 272
18.6.3 Monitoring disk space 273
18.7 while loop 273
18.7.1 Simple while loop 274
18.7.2 Using the while loop to read from the keyboard 274
18.7.3 Using the while loop to read data from files 275
18.7.4 Reading files using IFS 275
18.7.5 Processing a file with conditional tests 276
18.7.6 Running totals 278
18.7.7 Reading a pair of records at a time 279
18.7.8 Ignoring the # character 281
18.7.9 Processing formatted reports 282
18.7.10 The while loop and file descriptors 284
18.8 Controlling loops using break and continue 285
18.8.1 break 285
18.8.2 Breaking out of a case statement 285
18.8.3 continue 286
18.8.4 Skipping lines of files 286
18.9 Menus 287
18.10 Conclusion 290

19 Shell functions 291
19.1 Declaring functions in a script 292
19.2 Using functions in a script 292
19.3 Passing parameters to a function 293
19.4 Returning from a called function 293
19.5 Testing the values returned by a function 294
19.6 Using functions in the shell 295
19.7 Creating a function file 295
19.8 Sourcing the file 296
19.9 Checking the loaded functions 296
19.10 Executing shell functions 296
19.10.1 Deleting shell functions 297
19.10.2 Editing shell functions 297
19.10.3 Function examples 298
19.10.4 Putting it all together 314
19.11 Calling functions 314
19.11.1 Calling functions inside a script 315
19.11.2 Calling functions from a function file 316
19.12 Sourcing files is not only for functions 317
19.13 Conclusion 319

20 Passing parameters to scripts 320
20.1 The shift command 321
20.1.1 Simple use of shift 322
20.1.2 Last parameter supplied on the command line 322
20.1.3 Using shift to process file conversions 322
20.2 getopts 327
20.2.1 An example getopts script 327
20.2.2 How getopts works 329
20.2.3 Specifying values using getopts 329
20.2.4 How values can be accessed 331
20.2.5 Using getopts to process file conversions 332
20.3 Conclusion 334

21 Creating screen output 335
21.1 tput 335
21.1.1 String output 336
21.1.2 Numeric output 336
21.1.3 Boolean output 336
21.2 Using tput 337
21.2.1 Assigning tput commands 337
21.2.2 Using Boolean output 337
21.2.3 Using tput in your scripts 337
21.2.4 Generating escape sequences 338
21.2.5 Cursor position 340
21.2.6 Centring text on the screen 341
21.2.7 Finding out your terminal attributes 341
21.2.8 Using function keys with your scripts 343
21.2.9 Using colours 344
21.2.10 Generating colours 344
21.2.11 Creating better menus 348
21.3 Conclusion 355

22 Creating screen input 356
22.1 Adding records 357
22.2 Deleting records 368
22.3 Amending records 374
22.4 Viewing records 378
22.5 Conclusion 382

23 Debugging scripts 383
23.1 Common errors 383
23.1.2 The classic missing quote 384
23.1.3 The test error 384
23.1.4 Character case 384
23.1.5 for loops 384
23.1.6 echo 385
23.2 The set command 385
23.3 Conclusion 386

24 Shell built-in commands 387
24.1 Complete list of shell built-in commands 387
24.1.1 pwd 388
24.1.2 set 388
24.1.3 times 389
24.1.4 type 389
24.1.5 ulimit 390
24.1.6 wait 391
24.2 Conclusion 391

PART 5 - Better scripting skills 393

25 Going further with here documents 395
25.1 Creating a quick file 396
25.2 Creating a quick print document 396
25.3 Automating menus 396
25.4 Automating ftp transfers 398
25.5 Accessing databases 402
25.6 Conclusion 404

26 Shell utilities 405
26.1 Creating hold files 405
26.1.1 Using date to create log files 405
26.1.2 Creating unique temporary files 407
26.2 Signals 408
26.2.1 Killing a process 409
26.2.2 Detecting a signal 411
26.3 trap 411
26.3.1 Trapping signals and taking action 412
26.3.2 Catching a signal and taking action 413
26.3.3 Locking up your terminal 415
26.3.4 Ignoring signals 417
26.4 eval 419
26.4.1 Executing commands held in a string 419
26.4.2 Making a value a variable name 421
26.5 logger command 422
26.5.1 Using the logger command 423
26.5.2 Using logger in your scripts 423
26.6 Conclusion 425

27 A small collection of scripts 426
27.1 pingall 426
27.2 backup_gen 427
27.3 del.lines 433
27.4 access.deny 435
27.5 logroll 438
27.6 nfsdown 439
27.7 Conclusion 440

28 Run level scripts 441
28.1 How can you tell if you have run level directories? 442
28.2 Finding out your current run level 443
28.3 Bringing you up to speed on inittab 443
28.4 Now for the run levels 444
28.4.1 The different run levels 445
28.4.2 Format of a run level script 446
28.4.3 Installing a run level script 447
28.5 Using inittab to launch applications 449
28.6 Other methods of starting and shutting down services 449
28.7 Conclusion 450

29 cgi scripts 451
29.1 What is a Web page? 452
29.2 cgi 452
29.3 Connecting to a Web server 452
29.4 cgi and HTM script 453
29.4.1 Basic cgi script 453
29.4.2 Displaying the output of a shell command 456
29.4.3 Using SSI 457
29.4.4 Access counter 458
29.4.5 Printing out current Web environment settings using a link 460
29.4.6 Other common environment variables 461
29.5 Introducing the get and post methods 464
29.5.1 get method 464
29.5.2 post method 471
29.5.3 Populating a list 479
29.5.4 Automatically refreshing a Web page 480
29.6 Conclusion 482

A ASCII chart 483

B Useful shell commands 487

Index 501


Customer Reviews

Customer Reviews: 2     Average Customer Rating:

Jan 29, 2001     Andrew from Michigan, USA
Good concept...poor execution
I would agree with the previous reviewer, but my rating isn't _quite_ so harsh. The concepts covered are extensive, and the descriptions are generally quite good. There is a lot of good information on UNIX concepts (unattended execution, shell IO, sed, awk, etc.) However, the examples often are explained incompletely or incorrectly, and the sample scripts are often inaccurate and don't agree with the explanations. As a result, this is a mediocre book that could have been a great book if it had been edited more carefully.

Jun 21, 2000     Peter Kinman from Laguna Beach, CA, USA
Good concept, bad execution
At first sight, this book seems to fufill a real need for an all-in-one-place review of shell programming. The main problem (and it is a major one) is the incredible number of typos. Hardly a page passes without at least one or two mistakes. Some of the mistakes are obvious and easy to ignore, others can be really confusing. Whoever edited this book should find a new career (if the book was actually edited). Too bad, it could have been so much more.



Forgot your password?
FAQs
Shipping Options
Returns
Your Orders
Your Account