Giới thiệu sách Shell Script Linux: Learning Linux Shell Scripting

Learning Linux Shell Scripting

Table of Contents
Preface ix

Chapter 1: Getting Started and Working with Shell Scripting 1
Comparison of shells 2
Tasks done by shell 3
Working in shell 3
Learning basic Linux commands 4
Our frst script – Hello World 5
Compiler and interpreter – difference in process 7
When not to use scripts 7
Various directories 8
Working more effectively with shell – basic commands 9
Working with permissions 13
Changing fle permissions 13
Command chmod 14
Technique one – the symbolic method 14
Technique two – the numeric method 14
Setting umask 14
Setuid 15
Setgid 15
Sticky bit 15
Summary 16

Chapter 2: Drilling Deep into Process Management,
Job Control, and Automation 17
Introducing process basics 17
Monitoring processes using ps 19
Process management 23
Process monitoring tools – top, iostat, and vmstat 26
Understanding "at" 29
Understanding "crontab" 30
Summary 32

Chapter 3: Using Text Processing and Filters in Your Scripts 33
Text fltering tools 33
Head and tail 34
The diff command 35
The cut command 36
The paste command 37
The join command 37
The uniq command 38
The comm command 39
The tr command 40
IO redirection 41
File descriptors 41
Redirection 41
Brace expansion 45
Pattern matching with the vi editor 46
Pattern searching using grep 48
Summary 51

Chapter 4: Working with Commands 53
Learning shell interpretation of commands 53
Checking and disabling shell internal commands 55
The exit status 56
Command substitution 57
Command separators 59
Command1; command2 59
Command grouping 60
Logical operators 61
Command1 & command2 61
Command1 && command2 61
Command1 || command2 61
Pipes 63
Summary 63

Chapter 5: Exploring Expressions and Variables 65
Understanding variables 65
Working with environment variables 68
The local variable and its scope 70
Exporting variables 71
Working with read-only variables 74
Working with command line arguments (special variables,
set and shift, getopt) 74
Understanding set 76
Understanding shift 79
Resetting positional parameters 81
Understanding getopts 81
Understanding default parameters 83
Working with arrays 84
Creating an array and initializing it 84
Accessing array values 85
Summary 87

Chapter 6: Neat Tricks with Shell Scripting 89
Interactive Shell scripts – reading user input 89
Summarizing the read command with options 92
The here document and the << operator 92
The here operator with the sort command 93
The here operator with the wc command 94
The utility ed and here operator 95
A script for sending messages to all logged-in users 96
Using the << here operator for FTP usage and data transfer 96
Turning off variable substitution 97
The here string and the <<< operator 98
File handling 98
Introducing fle handling 99
Using exec to assign fle descriptor (fd) to fle 99
Understanding the opening, writing, and closing of a fle 99
Understanding reading from a fle 100
Understanding reading and writing to a fle 101
Using command read on fle descriptor (fd) 101
Reading from one fle and writing to another fle 101
Displaying the fle descriptor information from the /proc folder 103
File handling – reading line by line 104
Executing the command and storing the results in a fle 104
Summarizing usage of the exec command 105
Debugging 105
Debugging mode – disabling the shell (option -n) 106
Debugging mode – displaying commands (option -v) 107
Debugging mode – the tracing execution (option -x) 109
Summarizing the debugging options for the Bash shell 112
Using the set command 112
Summary of debugging options for set command 113
The vi editor setting for debugging 113
Good practices for Shell scripts 114
Summary 115

Chapter 7: Performing Arithmetic Operations in Shell Scripts 117
Using a command declare for arithmetic 117
Listing integers 119
Using the let command for arithmetic 120
Using the expr command for arithmetic 122
Using an arithmetic expansion 124
Binary, octal, and hex arithmetic operations 128
A floating-point arithmetic 129
Summary 132

Chapter 8: Automating Decision Making in Scripts 133
Checking the exit status of commands 134
Understanding the test command 135
Using the test command with single brackets 135
Using the test command with double brackets 136
String comparison options for the test command 136
Numerical comparison operators for the test command 138
File test options for the test command 142
File testing binary operators 143
Logical test operators 144
Conditional constructs – if else 145
Numerical handling if constructs 147
Using the command exit and the ? variable 148
String handling with the if construct 149
Checking for null values 151
File handling with the if command 153
Multiple test commands and if constructs 155
The if/elif/else command 158
The null command 160
Switching case 161
Implementing simple menus with select 167
Looping with the for command 172
Exiting from the current loop iteration with the continue command 177
Exiting from a loop with a break 179
Working with the do while loop 181
Using until 184
Piping the output of a loop to a Linux command 187
Running loops in the background 188
The IFS and loops 188
Summary 189
Chapter 9: Working with Functions 191
Understanding functions 191
Displaying functions 195
Removing functions 196
Passing arguments or parameters to functions 196
Sharing the data by many functions 200
Declaring local variables in functions 200
Returning information from functions 202
Returning a word or string from a function 204
Running functions in the background 204
Command source and period (.) 205
Creating a library of functions 205
Summary 206

Chapter 10: Using Advanced Functionality in Scripts 207
Understanding signals and traps 207
Using the trap command 209
Ignoring signals 210
Resetting signals 210
Listing traps 211
Using traps in function 211
Running scripts or processes even if the user logs out 212
Creating dialog boxes with the dialog utility 213
Creating a message box (msgbox) 214
Creating a message box (msgbox) with a title 214
The yes/no box (yesno) 215
The input box (inputbox) 216
The textbox (textbox) 218
A password box 218
The menu box (menu) 219
The checklist box (checklist) 221
The radiolist box (radiolist) 221
The progress meter box (gauge) 222
Summary 223

Chapter 11: System Startup and Customizing a Linux System 225
System startup, inittab, and run levels 225
The kernel startup and init process 225
Understanding run levels 227
System initialization boot scripts 227
User initialization scripts 229
Systemwide settings scripts 229
User level settings – default fles 229
Summary 231
Chapter 12: Pattern Matching and Regular Expressions
with sed and awk 233
The basics of regular expressions 233
sed – noninteractive stream editor 234
Understanding sed 235
Understanding regular expression usage in sed 235
Addressing in sed 236
How to modify a fle with sed 237
Printing – the p command 237
Deleting – the d command 238
Substitution – the s command 240
Range of selected lines: the comma 241
Multiple edits – the e command 243
Reading from fles – the r command 244
Writing to fles – the w command 244
Appending – the a command 245
Inserting – the i command 246
Changing – the c command 247
Transform – the y command 247
Quit – the q command 248
Holding and getting – the h and g commands 249
Holding and exchanging – the h and x commands 249
sed scripting 250
Using awk 252
The meaning of awk 252
Using awk 252
Input from commands 254
How awk works 255
awk commands from within a fle 256
Records and felds 257
Records 257
Fields 259
Field separators 259
Patterns and actions 260
Patterns 260
Actions 261
Regular expressions 262
Writing the awk script fle 263
Using variables in awk 264
Decision making using an if statement 264
Using the for loop 265
Using the while loop 266
Using the do while loop 266
Summary 267
Index 269