33,99 €
Advance your understanding of the Linux command line with this invaluable resource Linux Command Line and Shell Scripting Bible, 4th Edition is the newest installment in the indispensable series known to Linux developers all over the world. Packed with concrete strategies and practical tips, the latest edition includes brand-new content covering: * Understanding the Shell * Writing Simple Script Utilities * Producing Database, Web & Email Scripts * Creating Fun Little Shell Scripts Written by accomplished Linux professionals Christine Bresnahan and Richard Blum, Linux Command Line and Shell Scripting Bible, 4th Edition teaches readers the fundamentals and advanced topics necessary for a comprehensive understanding of shell scripting in Linux. The book is filled with real-world examples and usable scripts, helping readers navigate the challenging Linux environment with ease and convenience. The book is perfect for anyone who uses Linux at home or in the office and will quickly find a place on every Linux enthusiast's bookshelf.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 1126
Veröffentlichungsjahr: 2020
Cover
Title Page
Copyright
Dedication
About the Authors
About the Technical Editor
Acknowledgments
Introduction
Who Should Read This Book
How This Book Is Organized
Minimum Requirements
Where to Go from Here
Part I: The Linux Command Line
CHAPTER 1: Starting with Linux Shells
Investigating Linux
Examining Linux Distributions
Summary
CHAPTER 2: Getting to the Shell
Reaching the Command Line
Accessing CLI via a Linux Console Terminal
Accessing CLI via Graphical Terminal Emulation
Using the GNOME Terminal Emulator
Using the Konsole Terminal Emulator
Using the xterm Terminal Emulator
Summary
CHAPTER 3: Basic Bash Shell Commands
Starting the Shell
Using the Shell Prompt
Interacting with the Bash Manual
Navigating the Filesystem
Listing Files and Directories
Handling Files
Managing Directories
Viewing File Contents
Summary
CHAPTER 4: More Bash Shell Commands
Monitoring Programs
Monitoring Disk Space
Working with Data Files
Summary
CHAPTER 5: Understanding the Shell
Investigating Shell Types
Exploring Parent and Child Shell Relationships
Understanding External and Built-In Commands
Summary
CHAPTER 6: Using Linux Environment Variables
Exploring Environment Variables
Setting User-Defined Variables
Removing Environment Variables
Uncovering Default Shell Environment Variables
Setting the PATH Environment Variable
Locating System Environment Variables
Learning about Variable Arrays
Summary
CHAPTER 7: Understanding Linux File Permissions
Exploring Linux Security
Using Linux Groups
Decoding File Permissions
Changing Security Settings
Sharing Files
Access Control Lists
Summary
CHAPTER 8: Managing Filesystems
Exploring Linux Filesystems
Working with Filesystems
Managing Logical Volumes
Summary
CHAPTER 9: Installing Software
Exploring Package Management
Inspecting the Debian-Based Systems
The Red Hat–Based Systems
Managing Software Using Containers
Installing from Source Code
Summary
CHAPTER 10: Working with Editors
Visiting the vim Editor
Navigating the nano Editor
Exploring the Emacs Editor
Exploring the KDE Family of Editors
Exploring the GNOME Editor
Summary
Part II: Shell Scripting Basics
CHAPTER 11: Basic Script Building
Using Multiple Commands
Creating a Script File
Displaying Messages
Using Variables
Redirecting Input and Output
Employing Pipes
Performing Math
Exiting the Script
Working through a Practical Example
Summary
CHAPTER 12: Using Structured Commands
Working with the
if-then
Statement
Exploring the if-then-else Statement
Nesting ifs
Trying the test Command
Considering Compound Testing
Working with Advanced if-then Features
Considering the case Command
Working through a Practical Example
Summary
CHAPTER 13: More Structured Commands
Looking at the
for
Command
Trying the C-Style for Command
Exploring the while Command
Using the until Command
Nesting Loops
Looping on File Data
Controlling the Loop
Processing the Output of a Loop
Working through a Few Practical Examples
Summary
CHAPTER 14: Handling User Input
Passing Parameters
Using Special Parameter Variables
Being Shifty
Working with Options
Standardizing Options
Getting User Input
Working through a Practical Example
Summary
CHAPTER 15: Presenting Data
Understanding Input and Output
Redirecting Output in Scripts
Redirecting Input in Scripts
Creating Your Own Redirection
Listing Open File Descriptors
Suppressing Command Output
Using Temporary Files
Logging Messages
Working through a Practical Example
Summary
CHAPTER 16: Script Control
Handling Signals
Running Scripts in Background Mode
Running Scripts without a Hang-up
Controlling the Job
Being Nice
Running like Clockwork
Working through a Practical Example
Summary
Part III: Advanced Shell Scripting
CHAPTER 17: Creating Functions
Exploring Basic Script Functions
Returning a Value from a Function
Using Variables in Functions
Investigating Array Variables and Functions
Considering Function Recursion
Creating a Library
Using Functions on the Command Line
Working Through a Practical Example
Summary
CHAPTER 18: Writing Scripts for Graphical Desktops
Creating Text Menus
Doing Windows
Getting Graphic
Working Through a Practical Example
Summary
CHAPTER 19: Introducing
sed
and
gawk
Manipulating Text
Looking at the sed Editor Basic Commands
Working Through a Practical Example
Summary
CHAPTER 20: Regular Expressions
Exploring Regular Expressions
Defining BRE Patterns
Trying Out Extended Regular Expressions
Working Through Some Practical Examples
Summary
CHAPTER 21: Advanced
sed
Looking at Multiline Commands
Holding Space
Negating a Command
Changing the Flow
Replacing via a Pattern
Placing
sed
Commands in Scripts
Creating
sed
Utilities
Working Through a Practical Example
Summary
CHAPTER 22: Advanced
gawk
Using Variables
Working with Arrays
Considering Patterns
Structured Commands
Printing with Formats
Using Built‐in Functions
Trying Out User‐Defined Functions
Working Through a Practical Example
Summary
CHAPTER 23: Working with Alternative Shells
Considering the Dash Shell
Looking at the Dash Shell Features
Scripting in Dash
Exploring the zsh Shell
Viewing Parts of the zsh Shell
Scripting with zsh
Working Through a Practical Example
Summary
Part IV: Creating and Managing Practical Scripts
CHAPTER 24: Writing Simple Script Utilities
Performing Backups
Managing Account Deletion
Monitoring Your System
Summary
CHAPTER 25: Getting Organized
Understanding Version Control
Setting Up Your Git Environment
Committing with Git
Summary
APPENDIX A: Quick Guide to Bash Commands
Reviewing Built‐In Commands
Looking at Common Bash Commands
Assessing Environment Variables
APPENDIX B: Quick Guide to
sed
and
gawk
The sed Editor
The
gawk
Program
Index
End User License Agreement
Chapter 1
TABLE 1-1 The SysVinit Runlevels
TABLE 1-2 Linux Filesystems
TABLE 1-3 Linux Shells
TABLE 1-4 Other Linux Graphical Desktops
TABLE 1-5 Core Linux Distributions
TABLE 1-6 Specialized Linux Distributions
Chapter 2
TABLE 2-1 Graphical Interface Elements
TABLE 2-2
setterm
Options for Foreground and Background Appearance
TABLE 2-3 Popular Graphical Terminal Emulator Packages
TABLE 2-4 The File Menu
TABLE 2-5 The Edit Menu
TABLE 2-6 The View Menu
TABLE 2-7 The Search Menu
TABLE 2-8 The Terminal Menu
TABLE 2-9 The Tabs Menu
TABLE 2-10 The File Menu
TABLE 2-11 The Edit Menu
TABLE 2-12 The View Menu
TABLE 2-13 The Bookmarks Menu
TABLE 2-14 The Settings Menu
TABLE 2-15 The Help Menu
TABLE 2-16 xterm Command-Line Parameters
TABLE 2-17 xterm
+/–
Command-Line Parameters
Chapter 3
TABLE 3-1 The Linux Man Page Conventional Section Names
TABLE 3-2 The Linux Man Page Section Areas
TABLE 3-3 Common Linux Directory Names
Chapter 4
TABLE 4-1 The
ps
Command Unix Parameters
TABLE 4-2 The
ps
Command BSD Parameters
TABLE 4-3 The
ps
Command GNU Parameters
TABLE 4-4 Linux Process Signals
TABLE 4-5 The
mount
Command Parameters
TABLE 4-6 The
sort
Command Parameters
TABLE 4-7 Linux File Compression Utilities
TABLE 4-8 The
tar
Command Functions
TABLE 4-9 The
tar
Command Options
Chapter 5
TABLE 5-1 The
bash
Command-Line Options
Chapter 6
TABLE 6-1 The Bash Shell Bourne Variables
TABLE 6-2 The Bash Shell Environment Variables
Chapter 7
TABLE 7-1 The
useradd
Command-Line Parameters
TABLE 7-2 The
useradd
Change Default Values Parameters
TABLE 7-3 User Account Modification Utilities
TABLE 7-4 The
chage
Command Parameters
TABLE 7-5 Linux File Permission Codes
TABLE 7-6 The
chmod
SUID, SGID, and Sticky Bit Octal Values
Chapter 8
TABLE 8-1 Journaling Filesystem Methods
TABLE 8-2 Common
fdisk
Commands
TABLE 8-3 Common
gdisk
Commands
TABLE 8-4 Command-Line Programs to Create Filesystems
TABLE 8-5 The
fsck
Commonly Used Command-Line Options
TABLE 8-6 The Growing and Shrinking LVM Commands
Chapter 10
TABLE 10-1 vim Editing Commands
TABLE 10-2 nano Control Commands
TABLE 10-3 The KWrite Edit Menu Items
TABLE 10-4 The KWrite Tools
TABLE 10-5 The GNOME Desktop gedit Plugins
Chapter 11
TABLE 11-1 The
expr
Command Operators
TABLE 11-2 Linux Exit Status Codes
Chapter 12
TABLE 12-1 The
test
Numeric Comparisons
TABLE 12-2 The
test
String Comparisons
TABLE 12-3 The
test
File Comparisons
TABLE 12-4 The Double Parentheses Command Symbols
Chapter 14
TABLE 14-1 Common Linux Command-Line Options
Chapter 15
TABLE 15-1 Linux Standard File Descriptors
TABLE 15-2 Default lsof Output
Chapter 16
TABLE 16-1 Linux Signals
TABLE 16-2 The
jobs
Command Parameters
Chapter 17
TABLE 17.1 The shtool Library Functions
Chapter 18
TABLE 18.1 The dialog Widgets
TABLE 18.2 The
dialog
Command Options
TABLE 18.3 kdialog Window Options
TABLE 18.4 The zenity Window Widgets
Chapter 19
TABLE 19.1 The
sed
Command Options
TABLE 19.2 The
gawk
Options
Chapter 20
TABLE 20.1 BRE Special Character Classes
Chapter 21
TABLE 21.1 The
sed
Editor Hold Space Commands
Chapter 22
TABLE 22.1 The
gawk
Data Field and Record Variables
TABLE 22.2 More
gawk
Built‐in Variables
TABLE 22.3 Format Specifier Control Letters
TABLE 22.4 The
gawk
Mathematical Functions
TABLE 22.5 The
gawk
String Functions
TABLE 22.6 The
gawk
Time Functions
Chapter 23
TABLE 23.1 The Dash Command-Line Parameters
TABLE 23.2 The Dash Shell Built-in Commands
TABLE 23.3 The zsh Shell Command-Line Parameters
TABLE 23.4 The zsh Core Built-in Commands
TABLE 23.5 The zsh Modules
Appendix A
TABLE A.1 Bash Built‐In Commands
TABLE A.2 The Bash Shell External Commands
TABLE A.3 Bash Shell Environment Variables
Appendix B
TABLE B.1 The
sed
Command Options
TABLE B.2 The
gawk
Options
TABLE B.3 The
gawk
Data Field and Record Variables
TABLE B.4 More
gawk
Built‐In Variables
Chapter 1
FIGURE 1-1 The Linux system
FIGURE 1-2 The Linux system memory map
FIGURE 1-3 The KDE Plasma desktop on an openSUSE Linux system
FIGURE 1-4 A GNOME 3 desktop on an Ubuntu Linux system
FIGURE 1-5 The Cinnamon desktop from Linux Mint
FIGURE 1-6 The Xfce desktop as seen in the MX Linux distribution
Chapter 2
FIGURE 2-1 A simple terminal emulator running on a Linux desktop
FIGURE 2-2 Linux virtual console login screen
FIGURE 2-3 Linux virtual console with
inversescreen
being turned on
FIGURE 2-4 Finding GNOME Terminal in GNOME Shell
FIGURE 2-5 GNOME Terminal on CentOS
FIGURE 2-6 GNOME Terminal on Ubuntu
FIGURE 2-7 GNOME Terminal icon in the Favorites bar
FIGURE 2-8 Reaching the Keyboard Shortcuts window
FIGURE 2-9 Creating a keyboard shortcut
FIGURE 2-10 The Konsole terminal emulator
FIGURE 2-11 The xterm terminal
Chapter 3
FIGURE 3-1 Manual pages for the
hostname
command
FIGURE 3-2 A Linux virtual directory file path
FIGURE 3-3 The Linux file structure
FIGURE 3-4 Using the
more
command to display a text file
Chapter 4
FIGURE 4-1 The output of the
top
command while it is running
Chapter 5
FIGURE 5-1 Parent and child Bash shell processes
FIGURE 5-2 Subshell nesting
FIGURE 5-3 External command forking
Chapter 7
FIGURE 7-1 The Linux file permissions
Chapter 10
FIGURE 10-1 The vim main window
FIGURE 10-2 The nano editor window
FIGURE 10-3 Editing a file using the Emacs editor in console mode
FIGURE 10-4 The Emacs file browser
FIGURE 10-5 The Emacs graphical window
FIGURE 10-6 The default KWrite window editing a shell script program
FIGURE 10-7 The KWrite Find section
FIGURE 10-8 The KWrite Tool Mode Script submenu
FIGURE 10-9 The KWrite Configure Editor dialog box
FIGURE 10-10 The main Kate editing window
FIGURE 10-11 The Kate Documents List
FIGURE 10-12 The Kate Plugin Manager
FIGURE 10-13 The Kate built-in terminal window
FIGURE 10-14 The Kate configuration dialog box
FIGURE 10-15 The gedit main editor window
FIGURE 10-16 The gedit menu system
FIGURE 10-17 The gedit Side Panel
FIGURE 10-18 The gedit Side Panel's file manager
FIGURE 10-19 The gedit Plugins tab
FIGURE 10-20 The gedit Plugins tab after installation
FIGURE 10-21 The gedit Embedded Terminal plugin
Chapter 11
FIGURE 11-1 Using piping to send data to the
more
command
FIGURE 11-2 Using the
more
command with the
ls
command
Chapter 18
FIGURE 18-1 Displaying a menu from a shell script
FIGURE 18-2 Using the
msgbox
widget in the
dialog
command
FIGURE 18-3 Using the
yesno
widget in the
dialog
command
FIGURE 18-4 The
inputbox
widget
FIGURE 18-5 The
textbox
widget
FIGURE 18-6 The
menu
widget with menu items
FIGURE 18-7 The
fselect
widget
FIGURE 18-8 The
meminfo
command output displayed using the
textbox dialog
op...
FIGURE 18-9 A kdialog checklist dialog window
FIGURE 18-10 The sys admin menu script using
kdialog
FIGURE 18-11 The zenity calendar dialog window
FIGURE 18-12 The zenity file selection dialog window
FIGURE 18-13 The system admin menu using zenity
FIGURE 18-14 The dialog form feature
Chapter 20
FIGURE 20-1 Matching data against a regular expression pattern
Chapter 21
FIGURE 21-1 Reversing the order of a text file using the hold space
Chapter 24
FIGURE 24.1 Creating an archive directory hierarchy
Chapter 25
FIGURE 25-1 Conceptual depiction of the Git environment
FIGURE 25-2 MWGuard remote repository
Cover Page
Table of Contents
Begin Reading
iii
iv
v
vii
ix
xi
xxxi
xxxii
xxxiii
1
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
755
756
757
758
759
760
761
762
763
764
765
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
Richard Blum
Christine Bresnahan
Linux® Command Line and Shell Scripting Bible
Copyright © 2021 by John Wiley & Sons, Inc., Indianapolis, Indiana
Published simultaneously in Canada and the United Kingdom
ISBN: 978-1-119-70091-3ISBN: 978-1-119-70094-4 (ebk)ISBN: 978-1-119-70093-7 (ebk)
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at www.wiley.com/go/permissions.
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEB SITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.
For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com.
Library of Congress Control Number: 2020949805
Trademarks: Wiley and the Wiley logo are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. Linux is a registered trademark of Linus Torvalds. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book.
To the Lord God Almighty, “in whom are hidden all the treasures of wisdom and knowledge.”
— Colossians 2:3
Richard Blum has worked in the IT industry for more than 30 years as both a systems and a network administrator. During that time, he's had the opportunity to work with lots of different computer products, including Windows, NetWare, Cisco, Avaya, different flavors of UNIX, and of course, Linux. Over the years he's also volunteered for several nonprofit organizations to help support small networks that had little financial support. Rich is the author of many Linux-based books for total Linux geeks and teaches online courses in Linux and web programming. When he's not busy being a computer nerd, Rich enjoys playing piano and bass guitar and spending time with his wife, Barbara, and their two daughters, Katie Jane and Jessica.
Christine Bresnahan started working with computers more than 30 years ago in the IT industry as a systems administrator. Christine is an adjunct professor at Ivy Tech Community College, where she teaches Linux certification and Python programming classes. She also writes books and produces instructional resources for the classroom. During her downtime, Christine enjoys spending time with her husband and family, hiking, and gardening.
Jason W. Eckert is an experienced technical trainer, consultant, and best-selling author in the technology industry. With 45 industry certifications, 25 published textbooks, and over 30 years of technology and programming experience, Jason brings his expertise to every class that he teaches at triOS College. For more information about him, visit jasoneckert.net.
First, all glory and praise go to God, who through His Son, Jesus Christ, makes all things possible and gives us the gift of eternal life.
Many thanks go to the fantastic team of people at John Wiley & Sons for their outstanding work on this project. Thanks to Kenyon Brown, the acquisitions editor, for offering us the opportunity to work on this book. Also thanks to Patrick Walsh, the project editor, for keeping things on track and making this book more presentable. Thanks, Pat, for all your hard work and diligence. The technical editor, Jason Eckert, did a wonderful job of double-checking all the work in the book, plus making suggestions to improve the content. Thanks to Saravanan Dakshinamurthy and his team for their endless patience and diligence to make our work readable. We would also like to thank Carole Jelen at Waterside Productions, Inc., for arranging this opportunity for us, and for helping us out in our writing careers.
Christine would like to thank her husband, Timothy, for his encouragement, patience, and willingness to listen, even when he has no idea what she is talking about. Rich would like to thank his wife, Barbara, for the life-sustaining baked goods she readily prepared to help him keep up his energy while writing!
Welcome to the fourth edition of Linux Command Line and Shell Scripting Bible. Like all books in the Bible series, you can expect to find both hands-on tutorials and real-world information, as well as reference and background information that provides a context for what you are learning. This book is a fairly comprehensive resource on the Linux command line and shell commands. By the time you have completed Linux Command Line and Shell Scripting Bible, you will be well prepared to write your own shell scripts that can automate practically any task on your Linux system.
If you're a systems administrator in a Linux environment, you'll benefit greatly by knowing how to write shell scripts. The book doesn't walk you through the process of setting up a Linux system, but after you have it running, you'll want to start automating some of the routine administrative tasks. That's where shell scripting comes in, and that's where this book helps you out. This book demonstrates how to automate any administrative task using shell scripts, from monitoring system statistics and data files to generating reports for your boss.
If you're a home Linux enthusiast, you'll also benefit from Linux Command Line and Shell Scripting Bible. Nowadays, it's easy to get lost in the graphical world of prebuilt widgets. Most desktop Linux distributions try their best to hide the Linux system from the typical user. However, sometimes you must know what's going on under the hood. This book shows you how to access the Linux command-line prompt and what to do when you get there. Often, performing simple tasks, such as file management, can be done more quickly from the command line than from a fancy graphical interface. You can use a wealth of commands from the command line, and this book shows you how to use them.
This book leads you through the basics of the Linux command line and into more complicated topics, such as creating your own shell scripts. The book is divided into four parts, each one building on the previous parts.
Part I assumes that you either have a Linux system running or are looking into getting a Linux system. Chapter 1, “Starting with Linux Shells,” describes the parts of a total Linux system and shows how the shell fits in. After describing the basics of the Linux system, this part continues with the following:
Using a terminal emulation package to access the shell (
Chapter 2
)
Introducing the basic shell commands (
Chapter 3
)
Using more advanced shell commands to peek at system information (
Chapter 4
)
Understanding what the shell is used for (
Chapter 5
)
Working with shell variables to manipulate data (
Chapter 6
)
Understanding the Linux filesystem and security (
Chapter 7
)
Working with Linux filesystems from the command line (
Chapter 8
)
Installing and updating software from the command line (
Chapter 9
)
Using the Linux editors to start writing shell scripts (
Chapter 10
)
In Part II, you begin writing shell scripts. As you go through the chapters, you'll do the following:
Learn how to create and run shell scripts (
Chapter 11
)
Alter the program flow in a shell script (
Chapter 12
)
Iterate through code sections (
Chapter 13
)
Handle data from the user in your scripts (
Chapter 14
)
See different methods for storing and displaying data from your script (
Chapter 15
)
Control how and when your shell scripts run on the system (
Chapter 16
)
Part III dives into more advanced areas of shell script programming, including these things:
Creating your own functions to use in all your scripts (
Chapter 17
)
Utilizing the Linux graphical desktop for interacting with your script users (
Chapter 18
)
Using advanced Linux commands to filter and parse data files (
Chapter 19
)
Using regular expressions to define data (
Chapter 20
)
Learning advanced methods of manipulating data in your scripts (
Chapter 21
)
Working with advanced features of scripting to generate reports from raw data (
Chapter 22
)
Modifying your shell scripts to run in other Linux shells (
Chapter 23
)
The last section of the book, Part IV, demonstrates how to use shell scripts in real-world environments. In this part, you will learn these things:
How to put all the scripting features together to write your own scripts (
Chapter 24
)
How to organize and track your script versions using the popular git software (
Chapter 25
)
You will find many different organizational and typographical features throughout this book designed to help you get the most out of the information.
Whenever the authors want to bring something important to your attention, the information appears in a Warning.
This information is important and is set off in a separate paragraph with a special icon. Warnings provide information about things to watch out for, whether simply inconvenient or potentially hazardous to your data or systems.
For additional items of interest that relate to the chapter text, the authors use Tip.
Tips provide additional, ancillary information that is helpful, but somewhat outside of the current presentation of information.
Linux Command Line and Shell Scripting Bible doesn't focus on any specific Linux distribution, so you can follow along in the book using any Linux system you have available. The bulk of the book references the Bash shell, which is the default shell for most Linux systems.
After you've finished reading Linux Command Line and Shell Scripting Bible, you're well on your way to incorporating Linux commands in your daily Linux work. In the ever-changing world of Linux, it's always a good idea to stay in touch with new developments. Often, Linux distributions change, adding new features and removing older ones. To keep your knowledge of Linux fresh, always stay well informed. Find a good Linux forum site and monitor what's happening in the Linux world. Many popular Linux news sites, such as Slashdot and DistroWatch, provide up-to-the-minute information about new advances in Linux.
Chapter 1
Starting with Linux Shells
Chapter 2
Getting to the Shell
Chapter 3
Basic Bash Shell Commands
Chapter 4
More Bash Shell Commands
Chapter 5
Understanding the Shell
Chapter 6
Using Linux Environment Variables
Chapter 7
Understanding Linux File Permissions
Chapter 8
Managing Filesystems
Chapter 9
Installing Software
Chapter 10
Working with Editors
Investigating Linux
Understanding parts of the Linux kernel
Exploring the Linux desktop
Examining Linux distributions
Before you can dive into working with the Linux command line and shells, it's a good idea to first understand what Linux is, where it came from, and how it works. This chapter walks you through what Linux is and explains where the shell and command line fit in the overall Linux picture.
If you've never worked with Linux before, you may be confused as to why there are so many different versions of it available. We're sure that you've heard various terms such as distribution, LiveDVD, and GNU when looking at Linux packages and been confused. Wading through the world of Linux for the first time can be a tricky experience. This chapter takes some of the mystery out of the Linux system before you start working on commands and scripts.
For starters, four main parts make up a Linux system:
The Linux kernel
The GNU utilities
A graphical desktop environment
Application software
Each of these four parts has a specific job in the Linux system. Each one of the parts by itself isn't very useful. Figure 1-1 shows a basic diagram of how the parts fit together to create the overall Linux system.
FIGURE 1-1 The Linux system
This section describes these four main parts in detail and gives you an overview of how they work together to create a complete Linux system.
The core of the Linux system is the kernel. The kernel controls all the hardware and software on the computer system, allocating hardware when necessary and executing software when required.
If you've been following the Linux world at all, no doubt you've heard the name Linus Torvalds. Linus is the person responsible for creating the first Linux kernel software while he was a student at the University of Helsinki. He intended it to be a copy of the Unix system, at the time a popular operating system used at many universities.
After developing the Linux kernel, Linus released it to the Internet community and solicited suggestions for improving it. This simple process started a revolution in the world of computer operating systems. Soon Linus was receiving suggestions from students as well as professional programmers from around the world.
Allowing anyone to change programming code in the kernel would result in complete chaos. To simplify things, Linus acted as a central point for all improvement suggestions. It was ultimately Linus's decision whether or not to incorporate suggested code in the kernel. This same concept is still in place with the Linux kernel code, except that instead of just Linus controlling the kernel code, a team of developers has taken on the task.
The kernel is primarily responsible for four main functions:
System memory management
Software program management
Hardware management
Filesystem management
The following sections explore each of these functions in more detail.
One of the primary functions of the operating system kernel is memory management. Not only does the kernel manage the physical memory available on the server, but it can also create and manage virtual memory, or memory that does not actually exist.
It does this by using space on the hard disk, called the swap space. The kernel swaps the contents of virtual memory locations back and forth from the swap space to the actual physical memory. This allows the system to think there is more memory available than what physically exists (shown in Figure 1-2).
FIGURE 1-2 The Linux system memory map
The memory locations are grouped into blocks called pages. The kernel locates each page of memory in either the physical memory or the swap space. The kernel then maintains a table of the memory pages that indicates which pages are in physical memory and which pages are swapped out to disk.
The kernel keeps track of which memory pages are in use and automatically copies memory pages that have not been accessed for a period of time to the swap space area (called swapping out), even if other memory is available. When a program wants to access a memory page that has been swapped out, the kernel must make room for it in physical memory by swapping out a different memory page, and swap in the required page from the swap space. Obviously, this process takes time and can slow down a running process. The process of swapping out memory pages for running applications continues for as long as the Linux system is running.
The Linux operating system calls a running program a process. A process can run in the foreground, displaying output on a display, or it can run in the background, behind the scenes. The kernel controls how the Linux system manages all the processes running on the system.
The kernel creates the first process, called the init process, to start all other processes on the system. When the kernel starts, it loads the init process into virtual memory. As the kernel starts each additional process, it gives the process a unique area in virtual memory to store the data and code that the process uses.
A few different types of init process implementations are available in Linux, but these days the two most popular are:
SysVinit:
The
SysVinit (SysV)
initialization method, the original method used by Linux, was based on the Unix System V initialization method. Though it is not used by many Linux distributions these days, you still may find it around in older Linux distributions.
Systemd:
The
systemd
initialization method, created in 2010, has become the most popular initialization and process management system used by Linux distributions.
The SysVinit initialization method used a concept called runlevels to determine what processes to start. The runlevel defines the state of the running Linux system and what processes should run in each state. Table 1-1 shows the different runlevels associated with the SysVinit initialization method.
TABLE 1-1 The SysVinit Runlevels
Runlevel
Description
0
Shut down the system
1
Single-user mode used for system maintenance
2
Multi-user mode without networking services enabled
3
Multi-user mode with networking services enabled
4
Custom
5
Multi-user mode with GUI available
6
Reboot the system
The /etc/inittab file defines the default runlevel for a system. The processes that start for specific runlevels are defined in subdirectories of the /etc/rc.d directory. You can view the current runlevel at any time using the runlevel command:
$ runlevel
N 5
$
The systemd initialization method became popular because it has the ability to start processes based on different events:
When the system boots
When a particular hardware device is connected
When a service is started
When a network connection is established
When a timer has expired
The systemd method determines what processes to run by linking events to unit files. Each unit file defines the programs to start when the specified event occurs. The systemctl program allows you to start, stop, and list the unit files currently running on the system.
The systemd method groups unit files together into targets. A target defines a specific running state of the Linux system, similar to the SysVinit runlevel concept. At system startup, the default.target unit defines all the unit files to start. You can view the current default target using the systemctl command:
$ systemctl get-default
graphical.target
$
The graphical.target unit file defines the processes to start when a multi-user graphical environment is running, similar to the old SysVinit runlevel 5.
In Chapter 4, “More Bash Shell Commands,” you'll see how to use the ps command to view the processes currently running on the Linux system.
Still another responsibility for the kernel is hardware management. Any device that the Linux system must communicate with needs driver code inserted inside the kernel code. The driver code allows the kernel to pass data back and forth to the device, acting as an intermediary between applications and the hardware. Two methods are used for inserting device driver code in the Linux kernel:
Drivers compiled in the kernel
Driver modules added to the kernel
Previously, the only way to insert device driver code was to recompile the kernel. Each time you added a new device to the system, you had to recompile the kernel code. This process became even more inefficient as Linux kernels supported more hardware. Fortunately, Linux developers devised a better method to insert driver code into the running kernel.
Programmers developed the concept of kernel modules to allow you to insert driver code into a running kernel without having to recompile the kernel. Also, a kernel module could be removed from the kernel when the device was finished being used. This greatly simplified and expanded using hardware with Linux.
The Linux system identifies hardware devices as special files, called device files. There are three classifications of device files:
Character
Block
Network
Character device files are for devices that can handle data only one character at a time. Most types of modems and terminals are created as character files. Block files are for devices that can handle data in large blocks at a time, such as disk drives.
The network file types are used for devices that use packets to send and receive data. This includes network cards and a special loopback device that allows the Linux system to communicate with itself using common network programming protocols.
Linux creates special files, called nodes, for each device on the system. All communication with the device is performed through the device node. Each node has a unique number pair that identifies it to the Linux kernel. The number pair includes a major and a minor device number. Similar devices are grouped into the same major device number. The minor device number is used to identify a specific device within the major device group.
Unlike some other operating systems, the Linux kernel can support different types of filesystems to read and write data to and from hard drives. Besides having over a dozen filesystems of its own, Linux can read and write to and from filesystems used by other operating systems, such as Microsoft Windows. The kernel must be compiled with support for all types of filesystems that the system will use. Table 1-2 lists the standard filesystems that a Linux system can use to read and write data.
TABLE 1-2 Linux Filesystems
Filesystem
Description
ext
Linux extended filesystem — the original Linux filesystem
ext2
Second extended filesystem; provided advanced features over ext
ext3
Third extended filesystem; supports journaling
ext4
Fourth extended filesystem; supports advanced journaling
btrfs
A newer, high-performance filesystem that supports journaling and large files
exfat
The extended Windows filesystem, used mainly for SD cards and USB sticks
hpfs
OS/2 high-performance filesystem
jfs
IBM's journaling filesystem
iso9660
ISO 9660 filesystem (CD-ROMs)
minix
MINIX filesystem
msdos
Microsoft FAT16
ncp
NetWare filesystem
nfs
Network File System
ntfs
Support for Microsoft NT filesystem
proc
Access to system information
smb
Samba SMB filesystem for network access
sysv
Older Unix filesystem
ufs
BSD filesystem
umsdos
Unix-like filesystem that resides on top of msdos
vfat
Windows 95 filesystem (FAT32)
XFS
High-performance 64-bit journaling filesystem
Any hard drive that a Linux server accesses must be formatted using one of the filesystem types listed in Table 1-2.
The Linux kernel interfaces with each filesystem using the Virtual File System (VFS). This provides a standard interface for the kernel to communicate with any type of filesystem. VFS caches information in memory as each filesystem is mounted and used.
Besides having a kernel to control hardware devices, a computer operating system needs utilities to perform standard functions, such as controlling files and programs. Although Linus created the Linux system kernel, he had no system utilities to run on it. Fortunately for him, at the same time he was working, a group of people were working together on the Internet trying to develop a standard set of computer system utilities that mimicked the popular Unix operating system.
The GNU organization (GNU stands for GNU's Not Unix) developed a complete set of Unix utilities but had no kernel system to run them on. These utilities were developed under a software philosophy called open source software (OSS).
The concept of OSS allows programmers to develop software and then release it to the world with no licensing fees attached. Anyone can use, modify, or incorporate the software into their own system without having to pay a license fee. Uniting Linus's Linux kernel with the GNU operating system utilities created a complete, functional, free operating system.
Although the bundling of the Linux kernel and GNU utilities is often just called Linux, you will see some Linux purists on the Internet refer to it as the GNU/Linux system to give credit to the GNU organization for its contributions to the cause.
The GNU project was mainly designed for Unix system administrators to have a Unix-like environment available. This focus resulted in the project porting many common Unix system command-line utilities. The core bundle of utilities supplied for Linux systems is called the coreutils package.
The GNU coreutils package consists of three parts:
Utilities for handling files
Utilities for manipulating text
Utilities for managing processes
Each of these three main groups of utilities contains several utility programs that are invaluable to the Linux system administrator and programmer. This book covers each of the utilities contained in the GNU coreutils package in detail.
The GNU/Linux shell is a special interactive utility. It provides a way for users to start programs, manage files on the filesystem, and manage processes running on the Linux system. The core of the shell is the command prompt. The command prompt is the interactive part of the shell. It allows you to enter text commands, and then it interprets the commands and executes them in the kernel.
The shell contains a set of internal commands that you use to control tasks such as copying files, moving files, renaming files, displaying the programs currently running on the system, and stopping programs running on the system. Besides the internal commands, the shell allows you to enter the name of a program at the command prompt. The shell passes the program name off to the kernel to start it.
You can also group shell commands into files to execute as a program. Those files are called shell scripts. Any command that you can execute from the command line can be placed in a shell script and run as a group of commands. This provides great flexibility in creating utilities for commonly run commands or processes that require several commands grouped together.
Quite a few Linux shells are available to use on a Linux system. Different shells have different characteristics, some being more useful for creating scripts and some being more useful for managing processes. The default shell used in all Linux distributions is the Bash shell. The Bash shell was developed by the GNU project as a replacement for the standard Unix shell, called the Bourne shell (after its creator). The Bash shell name is a play on this wording, referred to as the “Bourne again shell.”
In addition to the Bash shell, we will cover several other popular shells in this book. Table 1-3 lists the different shells we will examine.
TABLE 1-3 Linux Shells
Shell
Description
ash
A simple, lightweight shell that runs in low-memory environments but has full compatibility with the Bash shell
korn
A programming shell compatible with the Bourne shell but supporting advanced programming features like associative arrays and floating-point arithmetic
tcsh
A shell that incorporates elements from the C programming language into shell scripts
zsh
An advanced shell that incorporates features from Bash, tcsh, and korn, providing advanced programming features, shared history files, and themed prompts
Most Linux distributions include more than one shell, although usually they pick one of them to be the default. If your Linux distribution includes multiple shells, feel free to experiment with different shells and see which one fits your needs.
In the early days of Linux (the early 1990s), all that was available was a simple text interface to the Linux operating system. This text interface allowed administrators to start programs, control program operations, and move files around on the system.
With the popularity of Microsoft Windows, computer users expected more than the old text interface to work with. This spurred more development in the OSS community, and the Linux graphical desktops emerged.
Linux is famous for being able to do things in more than one way, and no place is this more relevant than in graphical desktops. In Linux you can choose from a plethora of graphical desktops. The following sections describe a few popular ones.
Two basic elements control your video environment — the video card in your PC and your monitor. To display fancy graphics on your computer, the Linux software needs to know how to talk to both of them. The X Window software is the core element in presenting graphics.
The X Window software is a low-level program that works directly with the video card and monitor in the PC and controls how Linux applications can present fancy windows and graphics on your computer.
Linux isn't the only operating system that uses X Window; versions have been written for many different operating systems. In the Linux world, a few software packages can implement it. Two X Window packages are most commonly used in Linux:
X.org
Wayland
The X.org package is the older of the two, based on the original Unix X Window System version 11 (often called X11). More Linux distributions are migrating to the newer Wayland software, which is more secure and easier to maintain.
When you first install a Linux distribution, it attempts to detect your video card and monitor, and it then creates an X Window configuration file that contains the required information. During installation you may notice a time when the installation program scans your monitor for supported video modes. Sometimes this causes your monitor to go blank for a few seconds. Because lots of different types of video cards and monitors are out there, this process can take a little while to complete.
The core X Window software produces a graphical display environment but nothing else. Although this is fine for running individual applications, it is not too useful for day-to-day computer use. There is no desktop environment allowing users to manipulate files or launch programs. To do that, you need a desktop environment on top of the X Window system software.
The K Desktop Environment (KDE) was first released in 1996 as an open source project to produce a graphical desktop similar to the Microsoft Windows environment. The KDE desktop incorporates all the features you are probably familiar with if you are a Windows user. Figure 1-3 shows the current version, called KDE Plasma, running in the openSUSE Linux distribution.
The KDE Plasma desktop allows you to place both application and file icons in a special area on the desktop. If you single-click an application icon, the Linux system starts the application. If you single-click a file icon, the KDE desktop attempts to determine what application to start to handle the file.
FIGURE 1-3 The KDE Plasma desktop on an openSUSE Linux system
The bar at the bottom of the desktop is called the Panel. The Panel consists of four parts:
The K menu:
Much like the Windows Start menu, the K menu contains links to start installed applications.
Program shortcuts:
These are quick links to start applications directly from the Panel.
The taskbar:
The taskbar shows icons for applications currently running on the desktop.
Applets:
These are small applications that have an icon in the Panel that can often change depending on information from the application.
All of the Panel features are similar to what you would find in Windows. In addition to the desktop features, the KDE project has produced a wide assortment of applications that run in the KDE environment.
The GNU Network Object Model Environment (GNOME) is another popular Linux desktop environment. First released in 1999, GNOME has become the default desktop environment for many Linux distributions (the most popular being Red Hat Linux).
The GNOME desktop underwent a radical change with version 3, released in 2011. It departed from the standard look and feel of most desktops using standard menu bars and taskbars to make the interface more user-friendly across multiple platforms, such as tablets and mobile phones. This change led to controversy (see the “Other desktops” section), but slowly many Linux enthusiasts accepted the new look and feel of the GNOME 3 desktop.
Figure 1-4 shows the standard GNOME desktop used in the Ubuntu Linux distribution.
FIGURE 1-4 A GNOME 3 desktop on an Ubuntu Linux system
The GNOME 3 desktop cleans up the desktop interface by reducing the available menus to just three:
Activities:
Displays favorites, as well as any running application icons
Calendar:
Shows the current date/time, along with any system notification messages
System:
Shows network connections, system settings, and options to restart the system
The GNOME 3 desktop was designed to work on multiple types of devices, so you won't find a lot of menus. To launch applications, you must search for them using the Activities Overview, which is a search feature on the Activities menu.
Not to be outdone by KDE, the GNOME developers have also produced a host of graphical applications that integrate with the GNOME desktop.
One of the main features of Linux is choice, and nowhere is that more evident than in the graphical desktop world. There are a plethora of different types of graphical desktops available in the Linux world. If you're not happy with the default desktop in your Linux distribution, it usually doesn't take much effort to change it to something else!
When the GNOME desktop project radically changed its interface in version 3, many Linux developers who preferred the look and feel of GNOME version 2 created spin-off versions based on GNOME 2. Of these, two became somewhat popular:
Cinnamon:
The Cinnamon desktop was developed in 2011 by the Linux Mint distribution in an attempt to continue development of the original GNOME 2 desktop. It's now available as an option in several Linux distributions, including Ubuntu Fedora and openSUSE.
MATE:
The MATE desktop was also developed in 2011 by an Arch Linux user who disliked the switch to GNOME 3. It incorporates a few features of GNOME 3 (such as replacing the taskbar) but maintains the overall look and feel of GNOME 2.
Figure 1-5 shows the Cinnamon desktop as it appears in the Linux Mint distribution.
The downside to these fancy graphical desktop environments is that they require a fair amount of system resources to operate properly. In the early days of Linux, a hallmark and selling feature of Linux was its ability to operate on older, less powerful PCs that the newer Microsoft desktop products couldn't run on. However, with the popularity of KDE Plasma and GNOME 3 desktops, this has changed, since it takes just as much memory to run a KDE Plasma or GNOME 3 desktop as it does to run the latest Microsoft desktop environment.
If you have an older PC, don't be discouraged. The Linux developers have banded together to take Linux back to its roots. They've created several low memory–oriented graphical desktop applications that provide basic features that run perfectly fine on older PCs.
Although these graphical desktops don't have all that many applications designed around them, they still run many basic graphical applications that support features such as word processing, spreadsheets, databases, drawing, and, of course, multimedia support.
Table 1-4 shows some of the smaller Linux graphical desktop environments that can be used on lower-powered PCs and laptops.
FIGURE 1-5 The Cinnamon desktop from Linux Mint
TABLE 1-4 Other Linux Graphical Desktops
Desktop
Description
Fluxbox