Learn Linux Shell Scripting – Fundamentals of Bash 4.4 A comprehensive guide to automating administrative tasks with the Bash shell Sebastiaan Tammer BIRMINGHAM - MUMBAI Learn Linux Shell Scripting – Fundamentals of Bash 4.4 Copyright © 2018 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Commissioning Editor: Vijin Boricha Acquisition Editor: Rohit Rajkumar Content Development Editor: Abhishek Jadhav Technical Editor: Prachi Sawant Copy Editor: Safis Editing Project Coordinator: Jagdish Prabhu Proofreader: Safis Editing Indexer: Priyanka Dhadke Graphics: Tom Scaria Production Coordinator: Tom Scaria First published: December 2018 Production reference: 1281218 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78899-559-7 www.packtpub.com mapt.io Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Mapt is fully searchable Copy and paste, print, and bookmark content PacktPub.com Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. Contributors About the author Sebastiaan Tammer is a Linux enthusiast from the Netherlands. After attaining his BSc in information sciences, he graduated with an MSc in business informatics, both from Utrecht University. His professional career started in Java development, before he pivoted into a Linux opportunity. Because of this dual background, he feels most at home in a DevOps environment. Besides working extensively with technologies such as Puppet, Chef, Docker, and Kubernetes, he has also attained the RHCE and OSCP certificates. He spends a lot of time in and around Bash. Whether it is creating complex scripting solutions or just automating simple tasks, there is hardly anything he hasn't done with Bash! I would like to thank my girlfriend, Sanne, for all the help and support she has given me throughout the years. She has had to endure the late nights studying, me fixing stuff (which I had inevitably broken only hours earlier), and my endless storytelling about all those exciting new technologies. Thanks for the enormous amount of patience and love. I could not have done it without you! About the reviewer Heathe Kyle Yeakley holds degrees in technical communications and network management. He began his IT career in 1999 doing entry-level help desk support. In 2003, he began his first enterprise data center job performing tape backup and recovery for the United States Federal Aviation Administration. He worked in the aerospace sector for several years, during which time he worked on a wide range of products, including HP Tru64 Unix, Red Hat Enterprise Linux, Solaris 10, Legato Networker, Symantec NetBackup, HP and NetApp storage arrays, Spectra Logic and ADIC tape libraries, VMware, and HP Blade servers. He currently works for Agio, where he and his coworkers deliver managed IT services to some of the world's most prestigious companies. Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea. Table of Contents Preface 1 Chapter 1: Introduction 7 What is Linux? 7 What is Bash? 8 Summary 10 Chapter 2: Setting Up Your Local Environment 11 Technical requirements 11 Choosing between a virtual machine and a physical installation 12 Setting up VirtualBox 13 Creating an Ubuntu virtual machine 14 Creating the virtual machine in VirtualBox 14 Installing Ubuntu on the virtual machine 21 Accessing the virtual machine via SSH 29 Summary 30 Questions 30 Further reading 31 Chapter 3: Choosing the Right Tools 32 Technical requirements 32 Using graphical editors for shell scripting 33 Atom 33 Atom installation and configuration 34 Notepad++ 37 Using command-line editors 38 Vim 38 Vim summary 41 .vimrc 41 Vim cheat sheet 42 nano 43 Combining graphical editors with command-line editors when writing shell scripts 45 Summary 46 Questions 47 Further reading 47 Chapter 4: The Linux Filesystem 48 Technical requirements 48 The Linux filesystem explained 49 Table of Contents What is a filesystem? 49 What makes the Linux filesystem unique? 50 Structure of the Linux filesystem 52 Tree structure 52 Overview of top-level directories 57 What about multiple partitions? 58 /bin/, /sbin/, and /usr/ 60 /etc/ 62 /opt/, /tmp/, and /var/ 63 Everything is a file 65 Different types of files 65 Summary 68 Questions 69 Further reading 69 Chapter 5: Understanding the Linux Permissions Scheme 70 Technical requirements 70 Read, write, and execute 71 RWX 71 Users, groups, and others 72 Manipulating file permissions and ownership 75 chmod, umask 75 sudo, chown, and chgrp 81 sudo 81 chown, chgrp 83 Working with multiple users 85 Advanced permissions 89 File attributes 89 Special file permissions 90 Access Control Lists (ACLs) 90 Summary 91 Questions 92 Further reading 92 Chapter 6: File Manipulation 93 Technical requirements 93 Common file operations 93 Copying 94 Removing 96 Renaming, moving, and linking 97 Archiving 100 Finding files 104 locate 104 find 105 Summary 109 [ ii ] Table of Contents Questions 109 Further reading 110 Chapter 7: Hello World! 111 Technical requirements 111 First steps 111 The shebang 113 Running scripts 113 Readability 114 Comments 114 Script header 115 Verbosity 118 Verbosity in comments 118 Verbosity of commands 119 Verbosity of command output 120 Keep It Simple, Stupid (KISS) 121 Summary 122 Questions 123 Further reading 123 Chapter 8: Variables and User Input 124 Technical requirements 124 What is a variable? 125 Why do we need variables? 127 Variables or constants? 129 Variable naming 129 Dealing with user input 131 Basic input 131 Parameters and arguments 134 Interactive versus non-interactive scripts 135 Combining positional arguments and read 137 Summary 141 Questions 142 Further reading 143 Chapter 9: Error Checking and Handling 144 Technical requirements 144 Error checking 145 Exit status 145 Functional checks 147 Test shorthand 149 Variable refresher 151 Bash debugging 152 Error handling 152 if-then-exit 153 if-then-else 157 [ iii ] Table of Contents Shorthand syntax 161 Error prevention 163 Checking arguments 163 Managing absolute and relative paths 167 Dealing with y/n 169 Summary 172 Questions 173 Further reading 174 Chapter 10: Regular Expressions 175 Technical requirements 175 Introducing regular expressions 175 What is a regular expression? 176 grep 176 Greediness 178 Character matching 179 Line anchors 183 Character classes 186 Globbing 188 What is globbing? 188 Similarities with regular expressions 189 More globbing 190 Advanced globbing 192 Disabling globbing, and other options 193 Using regular expressions with egrep and sed 194 Advanced grep 194 Introducing egrep 198 sed, the stream editor 202 Stream editing 203 In-place editing 204 Line manipulation 206 Final remarks 209 Summary 210 Questions 211 Further reading 211 Chapter 11: Conditional Testing and Scripting Loops 212 Technical requirements 212 Advanced if-then-else 213 A recap on if-then-else 213 Using regular expressions in tests 214 The elif condition 217 Nesting 220 Getting help 222 The while loop 223 The until loop 225 [ iv ]