Table of Contents for
Mastering Linux Shell Scripting,

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Mastering Linux Shell Scripting, by Andrew Mallett Published by Packt Publishing, 2018
  1. Mastering Linux Shell Scripting - Second Edition
  2. Title Page
  3. Copyright and Credits
  4. Mastering Linux Shell Scripting Second Edition
  5. Packt Upsell
  6. Why subscribe?
  7. PacktPub.com
  8. Contributors
  9. About the authors
  10. About the reviewer
  11. Packt is searching for authors like you
  12. Table of Contents
  13. Preface
  14. Who this book is for
  15. What this book covers
  16. To get the most out of this book
  17. Download the example code files
  18. Download the color images
  19. Conventions used
  20. Get in touch
  21. Reviews
  22. The What and Why of Scripting with Bash
  23. Technical requirements
  24. Types of Linux shells
  25. What is bash scripting?
  26. The bash command hierarchy
  27. Command type
  28. Command PATH
  29. Preparing text editors for scripting
  30. Configuring vim
  31. Configuring nano
  32. Configuring gedit
  33. Creating and executing scripts
  34. Hello World!
  35. Executing the script
  36. Checking the exit status
  37. Ensuring a unique name
  38. Hello Dolly!
  39. Running the script with arguments
  40. The importance of correct quotes
  41. Printing the script name
  42. Declaring variables
  43. User-defined variables
  44. Environment variables
  45. Variable scope
  46. Command substitution
  47. Debugging your scripts
  48. Summary
  49. Questions
  50. Further reading
  51. Creating Interactive Scripts
  52. Technical requirements
  53. Using echo with options
  54. Basic script using read
  55. Script comments
  56. Enhancing scripts with read prompts
  57. Limiting the number of entered characters
  58. Controlling the visibility of the entered text
  59. Passing options
  60. Passing parameters with options
  61. Read options values
  62. Try to be standard
  63. Enhancing learning with simple scripts
  64. Backing-up with scripts
  65. Connecting to a server
  66. Version 1 – ping
  67. Version 2 – SSH
  68. Version 3 – MySQL/MariaDB
  69. Reading files
  70. Summary
  71. Questions
  72. Further reading
  73. Conditions Attached
  74. Technical requirements
  75. Simple decision paths using command-line lists
  76. Verifying user input with lists
  77. Using the test shell built-in
  78. Testing strings
  79. Testing integers
  80. Testing file types
  81. Creating conditional statements using if
  82. Extending if with else
  83. Test command with the if command
  84. Checking strings
  85. Checking files and directories
  86. Checking numbers
  87. Combining tests
  88. More conditions with elif
  89. Creating the backup2.sh using elif
  90. Using case statements
  91. Recipe – building a frontend with grep
  92. Summary
  93. Questions
  94. Further reading
  95. Creating Code Snippets
  96. Technical requirements
  97. Abbreviations
  98. Using code snippets
  99. Bringing color to the Terminal
  100. Creating snippets using VS Code
  101. Summary
  102. Questions
  103. Further reading
  104. Alternative Syntax
  105. Technical requirement
  106. Recapping the test command
  107. Testing files
  108. Adding logic
  109. Square brackets as not seen before
  110. Providing parameter defaults
  111. Variables
  112. Special parameters
  113. Setting defaults
  114. When in doubt – quote!
  115. Advanced tests using [[
  116. White space
  117. Other advanced features
  118. Pattern matching
  119. Regular expressions
  120. Regular expression script
  121. Arithmetic operations using ((
  122. Simple math
  123. Parameter manipulation
  124. Standard arithmetic tests
  125. Summary
  126. Questions
  127. Further reading
  128. Iterating with Loops
  129. Technical requirement
  130. for loops
  131. Advanced for loops
  132. The IFS
  133. Counting directories and files
  134. C-style for loops 
  135. Nested loops
  136. Redirecting loop output
  137. Controlling the loop
  138. while loops and until loops
  139. Reading input from files
  140. Creating operator menus
  141. Summary
  142. Questions
  143. Further reading
  144. Creating Building Blocks with Functions
  145. Technical requirements
  146. Introducing functions
  147. Passing parameters to functions
  148. Passing arrays
  149. Variable scope
  150. Returning values from functions
  151. Recursive functions
  152. Using functions in menus
  153. Summary
  154. Questions
  155. Further reading
  156. Introducing the Stream Editor
  157. Technical requirements
  158. Using grep to display text
  159. Displaying received data on an interface
  160. Displaying user account data
  161. Listing the number of CPUs in a system
  162. Parsing CSV files
  163. The CSV file
  164. Isolating catalog entries
  165. Understanding the basics of sed
  166. The substitute command
  167. Global replacement
  168. Limiting substitution
  169. Editing the file
  170. Other sed commands
  171. The delete command
  172. The insert and append commands
  173. The change command
  174. The transform command
  175. Multiple sed commands
  176. Summary
  177. Questions
  178. Further reading
  179. Automating Apache Virtual Hosts
  180. Technical requirements
  181. Apache name-based Virtual Hosts
  182. Creating the virtual host template
  183. First steps
  184. Isolating lines
  185. sed script files
  186. Automating virtual host creation
  187. Prompting for data during site creation
  188. Summary
  189. Questions
  190. Further reading
  191. AWK Fundamentals
  192. Technical requirements
  193. The history behind AWK
  194. Displaying and filtering content from files
  195. AWK variables
  196. User-defined variables
  197. Conditional statements
  198. The if command
  199. while loops
  200. for loops
  201. Formatting output
  202. Further filtering to display users by UID
  203. AWK control files
  204. Built-in functions
  205. Summary
  206. Questions
  207. Further reading
  208. Regular Expressions
  209. Technical requirements
  210. Regular expression engines
  211. Defining BRE patterns
  212. Anchor characters
  213. The dot character
  214. The character class
  215. Ranges of characters
  216. Special character classes
  217. The asterisk
  218. Defining ERE patterns
  219. The question mark
  220. The plus sign
  221. Curly braces
  222. The pipe character
  223. Expression grouping
  224. Using grep
  225. Summary
  226. Questions
  227. Further reading
  228. Summarizing Logs with AWK
  229. Technical requirements
  230. The HTTPD log file format
  231. Displaying data from web logs
  232. Selecting entries by date
  233. Summarizing 404 errors
  234. Summarizing HTTP access codes
  235. Resources hits
  236. Identify image hotlinking
  237. Displaying the highest ranking IP address
  238. Displaying the browser data
  239. Working with email logs
  240. Summary
  241. Questions
  242. Further reading
  243. A Better lastlog with AWK
  244. Technical requirements
  245. Using AWK ranges to exclude data
  246. The lastlog command
  247. Horizontally filtering rows with AWK
  248. Counting matched rows
  249. Conditions based on the number of fields
  250. Manipulating the AWK record separator to report on XML data
  251. Apache Virtual Hosts
  252. XML catalog
  253. Summary
  254. Questions
  255. Further reading
  256. Using Python as a Bash Scripting Alternative
  257. Technical requirements
  258. What is Python?
  259. Saying Hello World the Python way
  260. Pythonic arguments
  261. Supplying arguments
  262. Counting arguments
  263. Significant whitespace
  264. Reading user input
  265. Using Python to write to files
  266. String manipulation
  267. Summary
  268. Questions
  269. Further reading
  270. Assessments
  271. Chapter 1
  272. Chapter 2
  273. Chapter 3
  274. Chapter 4
  275. Chapter 5
  276. Chapter 6
  277. Chapter 7
  278. Chapter 8
  279. Chapter 9
  280. Chapter 10
  281. Chapter 11
  282. Chapter 12
  283. Chapter 13
  284. Chapter 14
  285. Other Books You May Enjoy
  286. Leave a review - let other readers know what you think

Displaying and filtering content from files

Now, of course we all want to be able to print a little more than just Hello World. The awk command can be used to filter content from files and, if needed, very large files. We should begin by printing the complete file before filtering it. In this way, we will get a feel for the syntax of the command. Later, we will see how we can add this control information into awk files to ease the command line. Using the following command, we will print all the lines from the /etc/passwd file:

$ awk ' { print } ' /etc/passwd  

This is equivalent to using the $0 variable with the print statement:

$ awk ' { print $0 }' /etc/passwd 

AWK provides us with some ready-to-use variables to extract data such as:

  • $0 for the entire line
  • $1 for the first field
  • $2 for the second field
  • $3 for the third field and so on

However, we will need to specify that in this file the field separator used is a colon, since it's the field separator in /etc/passwd file. The awk default delimiter is a space or any amount of spaces or tabs and newlines. There are two ways to specify the input delimiter; these are displayed in the following examples.

The first example is easy and simple to use. The -F option works well, especially where we do not need any additional header information:

$ awk -F":" '{ print $1 }' /etc/passwd  

We could also do this within the BEGIN block; this is useful when we want to use the BEGIN block to display header information:

$ awk ' BEGIN { FS=":" } { print $1 } ' /etc/passwd  

We can see this clearly in the preceding example, in which we named the BEGIN block and all of the code within it is corralled by the brace brackets. The main block has no name and is enclosed within the brace brackets.

After seeing the BEGIN block and the main code blocks, we will now look at the END code block. This is often used to display summary data. For example, if we want to print the total lines in the passwd file, we can make use of the END block. The code with the BEGIN and END blocks is processed just once, whereas the main block is processed for each line. The following example adds to the code we have written so far to include the total line count:

$ awk ' BEGIN { FS=":" } { print $1 } END { print NR } ' /etc/passwd  

The awk internal variable NR maintains the number of processed lines. If we want, we can add some additional text to this. This can be used to annotate the summary data. We can also make use of the single quotes that are used with the AWK language; they will allow us to spread the code across multiple lines. Once we have opened the single quotes, we can add newlines to the command line right until we close the quote. This is demonstrated in the next example where we have extended the summary information:

$ awk ' BEGIN { FS=":" }
> { print $1 }
> END { print "Total:",NR } ' /etc/passwd 

If we do not wish to end our AWK experience here, we can easily display a running line count with each line, as well as the final total. This is shown in the following example:

$ awk ' BEGIN { FS=":" }
> { print NR,$1 }
> END { print "Total:",NR } ' /etc/passwd  

The following screenshot captures this command and shows a partial output:

In the first example with BEGIN, we saw that there is no reason why we cannot use the END code block in isolation without a main code block. If we need to emulate the wc -l command, we can use the following awk statement:

$ awk ' END { print NR }' /etc/passwd 

The output will be the line count from the file. The following screenshot shows both the use of the awk command and the wc command to count the lines in the /etc/passwd file:

As we can see, the output does tally with 28 lines and our code has worked.

Another feature that we can practice with is working on selected lines only. For example, if we want to print only the first five lines, we will use the following statement:

$ awk ' NR < 6 ' /etc/passwd  

If we want to print lines 8 through to 12, we can use the following code:

$ awk ' NR==8,NR==12 ' /etc/passwd

We can also use regular expressions to match the text in the lines. Take a look at the following example where we look at the lines that end in the word bash:

$ awk ' /bash$/ ' /etc/passwd 

The example and the output it produces are shown in the following screenshot:

So if you want to use a regex pattern, you should use two slashes and write the pattern between them, /bash$/.