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

Simple decision paths using command-line lists

We have used command-line lists (|| and &&), both in Chapter 1, The What and Why of Scripting with Bash, and in some of the scripts found in Chapter 2, Creating Interactive Scripts. Lists are one of the simplest conditional statements that we can create, and so we thought that it was appropriate to use them in the earlier examples before fully explaining them here.

Command-line lists are two or more statements that are joined using either the AND or OR notations:

  • &&: AND
  • ||: OR

Where the two statements are joined using the AND notation, the second command only runs if the first command succeeds. Whereas, with the OR notation, the second command will run only if the first command fails.

The decision on the success or failure of a command is taken by reading the exit code from the application. A zero represents a successful application completion and anything other than a zero represents a failure. We can test the success or failure of an application by reading the exit status by means of the system variables $?. This is shown in the following example:

$ echo $? 

If we need to ensure that a script is run from a user's home directory, we can build this into the script's logic. This can be tested from the command line, and it does not have to be in a script. Consider the following command-line example:

$ test $PWD == $HOME || cd $HOME  

The double vertical bars denote an OR Boolean. This ensures that the second statement is only executed when the first statement is not true. In simple terms, if we are not currently in the home directory, we will be by the end of the command-line list. We will see more on the test command soon.

We can build this into almost any command that we want and not just test. For example, we can query to see if a user is logged into the system, and if they are, then we can use the write command to directly message their console. Similar to before, we can test this in the command line prior to adding it to the script. This is shown in the following command-line example:

$ who | grep pi > /dev/null 2>&1 && write pi < message.txt 

Note that you should change the user pi to your username.

If we use this in a script, it is almost certain that we will replace the username with a variable. In general, if we need to refer to the same value more than once, then using a variable is a good idea. In this case, we are searching for the pi user.

When we break the command-line list down, we first use the who command to list the users who are logged on. We pipe the list to grep to search for the desired username. We are not interested in the output from the search, just its success or failure. Bearing this in mind, we redirect all our output to /dev/null. The double ampersand indicates that the second statement in the list runs only if the first returns true. If the pi user is logged on, we use write to message the user. The following screenshot illustrates this command and the output: