This book is divided into five parts, which introduces shell programming skills, various UNIX commands and syntax in detail, and also involves word processing under UNIX and a small number of system management issues. This book is comprehensive in content and concise and smooth in text, making it suitable for Shell programmers to learn and refer to.
Table of Contents Translator's Preface Part One Shell Chapter 1 File Security and Permissions 1 1.1 Document 1 1.2 File Type 2 1.3 Permission 2 1.4 Change permission bit 4 1.4.1 Symbol Mode 4 1.4.2 chmod command example 5 1.4.3 Absolute mode 5 1.4.4 Other examples of chmod command 6 1.4.5 You can choose to use symbolic mode or absolute mode 7 1.5 Contents 7 1.6 suid/guid 7 1.6.1 Why use suid/guid 8 1.6.2 Example of setting suid/guid 8 1.7 chown and chgrp 9 1.7.1 chown example 9 1.7.2 chgrp example 9 1.7.3 Find out which user groups you belong to 9 1.7.4 Find out which groups other users belong to10 1.8umask 10 1.8.1 How to calculate the umask value 10 1.8.2 Commonly used umask value 11 1.9 Symbolic links 12 1.9.1 Using soft links to save multiple images of a file 12 1.9.2 Symbolic link example 12 1.10 Summary 13 Chapter 2 Using find and xargs 14 2.1 find command options 14 2.1.1 Using name option 15 2.1.2 Using perm option 16 2.1.3 Ignore a directory 16 2.1.4 Using the user and nouser options 16 2.1.5 Using the group and nogroup options 16 2.1.6 Find files based on change time 17 2.1.7 Find files newer or older than a certain file 17 2.1.8 Using type option 17 2.1.9 Using size option 18 2.1.10 Using depth option 18 2.1.11 Using mount option 18 2.1.12 Using cpio option 18 2.1.13 Use exec or ok to execute shell commands 19 2.1.14 Example 20 of find command 2.2 xargs 20 2.3 Summary 21 Chapter 3 Executing Commands in the Background 22 3.1 cron and crontab 22 3.1.1 crontab domain 22 3.1.2 Examples of crontab entries 23 3.1.3 crontab command option 23 3.1.4 Create a new crontab file 24 3.1.5 List crontab files 24 3.1.6 Edit crontab file 24 3.1.7 Delete crontab file 25 3.1.8 Recover lost crontab files 25 3.2 at command 25 3.2.1 Use the at command to submit commands or scripts 26 3.2.2 List submitted assignments 27 3.2.3 Clear a job 27 3.3 & Command 27 3.3.1 Submit command to the background 28 3.3.2 Use the ps command to view the process 28 3.3.3 Kill background processes 28 3.4 nohup command 29 3.4.1 Use the nohup command to submit jobs 29 3.4.2 Submit several jobs at once 29 3.5 Summary 30 Chapter 4 File Name Replacement 31 4.1 Use* 31 4.2 Use? 32 4.3 Using [...] and [!...] 32 4.4 Summary 33 Chapter 5 Shell Input and Output 34 5.1 echo 34 5.2 read 35 5.3 cat 37 5.4 Pipeline 38 5.5 tee 39 5.6 Standard input, output, and errors 40 5.6.1 Standard input 40 5.6.2 Standard output 40 5.6.3 Standard error 40 5.7 File Redirection 40 5.7.1 Redirecting standard output 41 5.7.2 Redirecting standard input 42 5.7.3 Redirecting standard error 42 5.8 Using standard output and standard error together 43 5.9 Merging standard output and standard error 43 5.10 exec 44 5.11 Using file descriptors 44 5.12 Summary 45 Chapter 6 Command Execution Sequence 46 6.1 Using && 46 6.2 Use || 46 6.3 Use () and { } to combine commands 47 6.4 Summary 48 Part 2 Text Filtering Chapter 7 Introduction to Regular Expressions 49 7.1 Using periods to match single characters 50 7.2 Match a string or character sequence with ^ at the beginning of the line 50 7.3 Match string or character 51 with $ at end of line 7.4 Use * to match a single character or its repeated sequence in a string 51 7.5 Use to mask the meaning of a special character 52 7.6 Use [] to match a range or set52 7.7 Use {} to match the number of times the pattern result appears 53 7.8 Summary 55 Chapter 8 grep family 56 8.1 grep 57 8.1.1 Double quotation marks 57 8.1.2 grep option 57 8.1.3 Query multiple files 57 8.1.4 Line Match 57 8.1.5 Number of lines 58 8.1.6 Displaying non-matching lines 58 8.1.7 Exact match 58 8.1.8 Case sensitivity58 8.2 grep and regular expressions 58 8.2.1 Mode range 59 8.2.2 Does not match beginning of line 59 8.2.3 Set case 59 8.2.4 Match any character 59 8.2.5 Date query 59 8.2.6 Range combination 60 8.2.7 Pattern appearance probability 60 8.2.8 Use grep to match “AND” or “OR” patterns 61 8.2.9 Blank line 61 8.2.10 Match special characters 61 8.2.11 Query formatted file name 61 8.2.12 Query IP address 61 8.3 Class name 62 8.4 System grep command 62 8.4.1 Table of Contents 63 8.4.2 passwd file 63 8.4.3 Using the ps command 63 8.4.4 Using grep 64 on a string 8.5 egrep 64 8.6 Summary 65 Chapter 9 Introduction to AWK 66 9.1 Calling awk 66 9.2 awk script 67 9.2.1 Patterns and Actions 67 9.2.2 Domains and records 67 9.2.3 Regular expressions and their operations in awk 70 9.2.4 Metacharacters 70 9.2.5 Conditional operators 70 9.2.6 awk built-in variables 73 9.2.7 NF, NR and FILENAME 74 9.2.8 awk operator 75 9.2.9 Built-in string functions 78 9.2.10 String masking sequence 80 9.2.11 awk output function printf 81 9.2.12 printf modifier 81 9.2.13 awk array 86 9.3 Summary 88 Chapter 10 Introduction to sed Usage 89 10.1 How to read data with sed 89 10.2 Calling sed 89 10.2.1 Saving sed output 90 10.2.2 How to use sed to query text in a file 90 10.2.3 Basic sed editing commands 90 10.3 sed and regular expressions 91 10.4 Basic sed programming examples 91 10.4.1 Using p(rint) to display line 91 10.4.2 Print range 91 10.4.3 Print mode 92 10.4.4 Querying using patterns and line numbers 92 10.4.5 Matching metacharacters 92 10.4.6 Displaying the entire file 92 10.4.7 Any character 92 10.4.8 First line 92 10.4.9 Last line 93 10.4.10 Print line number 93 10.4.11 Additional text 93 10.4.12 Create sed script file 94 10.4.13 Inserting text 94 10.4.14 Modify text 95 10.4.15 Delete text 96 10.4.16 Replacement text 96 10.5 Modifying strings using substitution 97 10.6 Write sed results to file command 97 10.7 Reading text from a file 98 10.8 Exit 98 after matching 10.9 Displaying control characters in files 99 10.10 Using system sed 99 10.10.1 Handling control characters 99 10.10.2 Processing message output 101 10.10.3 Remove the number 101 at the beginning of the line 10.10.4 Additional text 102 10.10.5 Passing value 102 from shell to sed 10.10.6 Setting shell variables from sed output 102 10.11 Quick one-line commands 102 10.12 Summary 103 Chapter 11 Merger and Split 104 11.1 sort usage 104 11.1.1 Overview 104 11.1.2 sort options 104 11.1.3 Saving output 105 11.1.4 sort startup method 105 11.1.5 How sort refers to domains 105 11.1.6 Whether the file has been classified105 11.1.7 Basic sort 106 11.1.8 sort classification inversion 106 11.1.9 Classification by designated domain 106 11.1.10 Numerical field classification 106 11.1.11 Uniqueness classification 107 11.1.12 Other sort methods using k 108 11.1.13 Using k for classification key sorting 108 11.1.14 Specify sort sequence 108 11.1.15 pos usage 108 11.1.16 Classify output using head and tail 109 11.1.17 awk uses sort to output results 109 11.1.18 Merge two classification files 110 11.2 System sort 110 11.3 uniq usage 111 11.4 join usage 112 11.5 cut usage 114 11.5.1 Using domain separators 115 11.5.2 Cut the specified field 115 11.6 paste usage 116 11.6.1 Specifying columns 116 11.6.2 Using different field delimiters 116 11.6.3 paste command pipeline input 117 11.7 split usage 117 11.8 Summary 118 Chapter 12 tr Usage 119 12.1 About tr 119 12.1.1 Character range 119 12.1.2 Saving output 120 12.1.3 Removing repeated characters 120 12.1.4 Delete empty lines 120 12.1.5 Uppercase to lowercase 121 12.1.6 Lowercase to uppercase 121 12.1.7 Delete specified characters 121 12.1.8 Converting control characters 122 12.1.9 Quick conversion 122 12.1.10 Matching more than one character 123 12.2 Summary 123 Part 3 Login Environment Chapter 13 Login Environment 125 13.1 /etc/profile 125 13.2 User’s $HOME.profile 128 13.3 stty usage 129 13.4 Create .logout file 131 13.5 Summary 131 Chapter 14 Environment and Shell Variables 132 14.1 What are shell variables132 14.2 Local variables 132 14.2.1 Display variables 133 14.2.2 Clear variables 133 14.2.3 Display all local shell variables 133 14.2.4 Combining variable values 134 14.2.5 Testing whether a variable has been set 134 14.2.6 Use variables to save system command parameters 135 14.2.7 Setting read-only variables 135 14.3 Environment variables 136 14.3.1 Setting environment variables 136 14.3.2 Display environment variables 136 14.3.3 Clear environment variables 137 14.3.4 Embedding shell variables 137 14.3.5 Other environment variables 139 14.3.6 set command 140 14.3.7 Exporting variables to child processes 140 14.4 Positional variable parameters 141 14.4.1 Using positional parameters in scripts 142 14.4.2 Passing parameters to system commands 142 14.4.3 Specific variable parameters 143 14.4.4 Final exit status 144 14.5 Summary 145 Chapter 15 Quotes 146 15.1 Necessity of citation 146 15.2 Double quotes 146 15.3 Single quotes 147 15.4 Backticks147 15.5 Backslash 148 15.6 Summary 149 Part 4 Basic Shell Programming Chapter 16 Introduction to Shell Scripts 151 16.1 Reasons to use shell scripts 151 16.2 Script content 151 16.3 Running a script 152 16.4 Summary 153 Chapter 17 Conditional Testing 154 17.1 Test file status 154 17.2 Using logical operators when testing 155 17.3 String testing 155 17.4 Test value 156 17.5 expr usage 157 17.5.1 Incremental counting 158 17.5.2 Numerical testing 158 17.5.3 Pattern matching 158 17.6 Summary 159 Chapter 18 Control Flow Structures 160 18.1 Exit status 160 18.2 Control Structure 160 18.2.1 Flow control 161 18.2.2 Loops 161 18.3 if then else statement 161 18.3.1 Simple if statement 162 18.3.2 Variable value testing 162 18.3.3 grep output inspection 163 18.3.4 Testing grep output with variables 163 18.3.5 File copy output check 164 18.3.6 Current directory test 164 18.3.7 File permission testing 165 18.3.8 Testing parameters passed into scripts 165 18.3.9 Determining whether the script is in interactive mode165 18.3.10 Simple if else statement 166 18.3.11 Variable setting test 166 18.3.12 Detecting the user running the script 166 18.3.13 Pass script parameters into system commands 167 18.3.14 null: command usage 167 18.3.15 Test directory creation results 168 18.3.16 Another copy instance 169 18.3.17 Multiple if statements 169 18.3.18 Testing and setting environment variables 169 18.3.19 Detecting the last command status 170 18.3.20 Adding and checking integer values 171 18.3.21 Simple secure login script 172 18.3.22 elif usage 173 18.3.23 Using elif for multi-condition detection 173 18.3.24 Multiple file location detection 174 18.4 case statement 175 18.4.1 Simple case statement 175 18.4.2 Using match patterns | 176 18.4.3 Prompt to type y or n 177 18.4.4 case and command parameter passing 177 18.4.5 Capturing input and executing an empty command 178 18.4.6 Default variable values 179 18.5 for loop 180 18.5.1 Simple for loop 181 18.5.2 Printing a string list 181 18.5.3 Using the ls command with a for loop 181 18.5.4 Using parameters with for loops 182 18.5.5 Using for loop to connect to the server 183 18.5.6 Using for loop to back up files 183 18.5.7 Multiple file conversion 183 18.5.8 Multiple sed deletion operations 184 18.5.9 Loop Count 184 18.5.10 for loops and local documents 184 18.5.11 for loop embedding 185 18.6 until loop 186 18.6.1 Simple until loop 186 18.6.2 Monitoring files 187 18.6.3 Monitoring disk space 187 18.7 while loop 188 18.7.1 Simple while loop 188 18.7.2 Use while loop to read keyboard input 188 18.7.3 Reading data from a file using a while loop 189 18.7.4 Reading files using IFS 189 18.7.5 File processing with test conditions 190 18.7.6 Scanning file lines for count 191 18.7.7 Reading a pair of records at a time 193 18.7.8 Ignore the # character 193 18.7.9 Processing formatted reports 194 18.7.10 while loops and file descriptors 196 18.8 Using break and continue to control loops 197 18.8.1 break 197 18.8.2 Jump out of case statement 197 18.8.3 continue 197 18.8.4 Browsing file lines 198 18.9 Menu 199 18.10 Summary 201 Chapter 19 Shell Functions 202 19.1 Defining functions in scripts 203 19.2 Using functions in scripts 203 19.3 Passing parameters to functions 203 19.4 Returning 203 from the calling function 19.5 Function return value testing 204 19.6 Using functions in the shell 204 19.7 Creating function files 204 19.8 Locating files 205 19.9 Checking Loading Functions 205 19.10 Executing shell functions 205 19.10.1 Delete shell function 206 19.10.2 Editing shell functions 206 19.10.3 Function examples 207 19.10.4 Grouping functions together 219 19.11 Function calls 219 19.11.1 Calling functions in scripts 219 19.11.2 Calling functions from function files 220 19.12 Locating files isn’t just for functions 222 19.13 Summary 223 Chapter 20 Passing Parameters to Scripts 224 20.1 shift command 225 20.1.1 Simple usage of shift command 225 20.1.2 The last parameter entered on the command line 225 20.1.3 Using shift to process file conversion 226 20.2 getopts 229 20.2.1 getopts script example 229 20.2.2 How to use getopts 231 20.2.3 Use getopts to specify variable values 231 20.2.4 Access value method 232 20.2.5 Using getopts to handle file conversion 233 20.3 Summary 235 Chapter 21 Creating Screen Output 236 21.1 tput usage 236 21.1.1 String output 236 21.1.2 Digital output 237 21.1.3 Boolean output 237 21.2 tput usage 237 21.2.1 Setting the tput command 237 21.2.2 Using Boolean output 237 21.2.3 Using tput in scripts 237 21.2.4 Generating escape sequences 238 21.2.5 Cursor position 239 21.2.6 Displaying text in the center of the screen 240 21.2.7 Finding terminal attributes 240 21.2.8 Using function keys in scripts 241 21.2.9 Using Color 242 21.2.10 Generating Color 243 21.2.11 Create exquisite menu 246 21.3 Summary 251 Chapter 22 Creating screen input 252 22.1 Add records 252 22.2 Delete records 262 22.3 Modification records 266 22.4 View records 270 22.5 Summary 273 Chapter 23 Debugging Scripts 274 23.1 General Errors 274 23.1.1 Loop error 274 23.1.2 Typical missing quotation marks 274 23.1.3 Test Error 274 23.1.4 Character case 275 23.1.5 for loop 275 23.1.6 echo 275 23.2 set command 275 23.3 Summary 276 Chapter 24 Shell Embedding Commands 277 24.1 Complete list of shell embedded commands 277 24.1.1 pwd 277 24.1.2 set 278 24.1.3 times 278 24.1.4 type 278 24.1.5 ulimit 279 24.1.6 wait 279 24.2 Summary 279 Part 5 Advanced Shell Programming Techniques Chapter 25 In-depth Discussion