Book Image

Learn Linux Shell Scripting – Fundamentals of Bash 4.4

By : Sebastiaan Tammer
Book Image

Learn Linux Shell Scripting – Fundamentals of Bash 4.4

By: Sebastiaan Tammer

Overview of this book

Shell scripts allow us to program commands in chains and have the system execute them as a scripted event, just like batch files. This book will start with an overview of Linux and Bash shell scripting, and then quickly deep dive into helping you set up your local environment, before introducing you to tools that are used to write shell scripts. The next set of chapters will focus on helping you understand Linux under the hood and what Bash provides the user. Soon, you will have embarked on your journey along the command line. You will now begin writing actual scripts instead of commands, and will be introduced to practical applications for scripts. The final set of chapters will deep dive into the more advanced topics in shell scripting. These advanced topics will take you from simple scripts to reusable, valuable programs that exist in the real world. The final chapter will leave you with some handy tips and tricks and, as regards the most frequently used commands, a cheat sheet containing the most interesting flags and options will also be provided. After completing this book, you should feel confident about starting your own shell scripting projects, no matter how simple or complex the task previously seemed. We aim to teach you how to script and what to consider, to complement the clear-cut patterns that you can use in your daily scripting challenges.
Table of Contents (24 chapters)
Title Page
About Packt
Contributors
Preface
Free Chapter
1
Introduction
Index

Chapter 7


  1. What do we, by convention, do as the first thing when we learn a new programming or scriping language? We print the string "Hello World".
  2. What is the shebang for Bash? #!/bin/bash
  3. Why is the shebang needed? If we're running the script without specifying which program we should use, the shebang will allow Linux to use the correct one.
  4. In what three ways can we run a script?
    • By using the program which we want to run it with: bash script.sh
    • By setting the executable permission and prefixing the scriptname with ./: ./script.sh

 

 

 

    • By setting the executable permission and using the fully qualified path to the file: /tmp/script.sh
  1. Why do we place such emphasis on readability when creating shell scripts?
    • Scripts are much easier to use if the person using them can easily understand what the script does
    • If anyone other than yourself needs to edit the script (and you can consider yourself 'someone else' too after a few months!) it helps tremendously if it's simple to understand
  2. Why do we use comments? So we can explain things in the script which might not be obvious by just looking at the commands. Furthermore, it also allows us to give some design rationale if that helps clarify the script.
  3. Why do we recommend including a script header for all shell scripts you write? If gives a bit of information on the author, age and description to the script. It helps give context to the script, which can be very helpful when the script is not working as expected, or needs to be modified.
  4. Which three types of verbosity have we discussed?
    • Verbosity in comments
    • Verbosity in commands
    • Verbosity in command output
  5. What is the KISS principle? KISS, which stands for Keep It Simple, Stupid, is a design recommendation which helps us to remember that we should keep things simple, as that often increases usability and readability, while even being the best solution most of the times as well.