Mastering Bash

Book description

Your one stop guide to making the most out of Bash programming

About This Book

  • From roots to leaves, learn how to program in Bash and automate daily tasks, pouring some spice in your scripts
  • Daemonize a script and make a real service of it, ensuring it’s available at any time to process user-fed data or commands
  • This book provides functional examples that show you practical applications of commands

Who This Book Is For

If you're a power user or system administrator involved in writing Bash scripts to automate tasks, then this book is for you. This book is also ideal for advanced users who are engaged in complex daily tasks.

What You Will Learn

  • Understand Bash right from the basics and progress to an advanced level
  • Customise your environment and automate system routine tasks
  • Write structured scripts and create a command-line interface for your scripts
  • Understand arrays, menus, and functions
  • Securely execute remote commands using ssh
  • Write Nagios plugins to automate your infrastructure checks
  • Interact with web services, and a Slack notification script
  • Find out how to execute subshells and take advantage of parallelism
  • Explore inter-process communication and write your own daemon

In Detail

System administration is an everyday effort that involves a lot of tedious tasks, and devious pits. Knowing your environment is the key to unleashing the most powerful solution that will make your life easy as an administrator, and show you the path to new heights. Bash is your Swiss army knife to set up your working or home environment as you want, when you want.

This book will enable you to customize your system step by step, making your own real, virtual, home out of it. The journey will take you swiftly through the basis of the shell programming in Bash to more interesting and challenging tasks. You will be introduced to one of the most famous open source monitoring systems—Nagios, and write complex programs with it in any languages. You’ll see how to perform checks on your sites and applications.

Moving on, you’ll discover how to write your own daemons so you can create your services and take advantage of inter-process communication to let your scripts talk to each other. So, despite these being everyday tasks, you’ll have a lot of fun on the way. By the end of the book, you will have gained advanced knowledge of Bash that will help you automate routine tasks and manage your systems.

Style and approach

This book presents step-by-step instructions and expert advice on working with Bash and writing scripts. Starting from the basics, this book serves as a reference manual where you can find handy solutions and advice to make your scripts flexible and powerful.

Table of contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Downloading the color images of this book
      3. Errata
      4. Piracy
      5. Questions
  2. Let's Start Programming
    1. I/O redirection
    2. Messing around with stdin, stdout, and stderr
    3. Time for the interpreter: the sha-bang
      1. Calling your script
      2. Something went wrong, let's trace it
    4. Variables
      1. Assigning a variable
      2. Keep the variable name safe
      3. Variables with limited scope
      4. Environment variables
      5. Variable expansion
      6. Pattern matching against variables
      7. Special variables
    5. Summary
  3. Operators
    1. Arithmetic operators
      1. The + operator
      2. The - operator
      3. The * operator
      4. The / operator
      5. The % operator
      6. The ** operator
    2. Assignment operators
      1. The += operator
      2. The -= operator
      3. The *= operator
      4. The /= operator
      5. The %= operator
      6. The ++ or -- operators
    3. Bitwise operators
      1. Left shift (<<)
      2. Right shift (>>)
      3. Bitwise AND
      4. Bitwise OR (|)
      5. Bitwise XOR (^)
      6. Bitwise NOT (~)
    4. Logical operators
      1. Logical NOT (!)
      2. Logical AND
      3. Logical OR (||)
    5. Comma operator (,)
    6. Operators evaluation order and precedence in decreasing relevance
    7. Exit codes
      1. Exiting a script
    8. Summary
  4. Testing
    1. What if...else
    2. Test command recap
      1. Testing files
      2. Testing integers
      3. Testing strings
      4. More on tests
    3. Summary
  5. Quoting and Escaping
    1. Special characters
      1. The hash character (#)
      2. The semicolon character (;)
      3. The double semicolon character (;;)
      4. The case terminator (;;&) and (;&))
      5. The dot character (.)
      6. The double quotes ("...")
      7. The single quotes ('...')
      8. The comma character (,)
      9. The ,, and , () case modificators
      10. The ^^ and ^ () case modificators
      11. The backslash (\)
      12. The forward slash (/)
      13. '...'
      14. The colon character (:)
      15. The exclamation (!)
      16. Keywords
      17. The asterisk (*)
      18. The double asterisk (**)
      19. Test operators (?)
      20. The substitution ($)
      21. The parameter substitution (${})
      22. The quoted string expansion ($'...')
      23. The positional parameters ($* and $")
      24. The exit status ($?)
      25. The process ID ($$)
      26. Grouping the command (command1 ; command2 ; commandn)
      27. Braces ({})
      28. The full path ({} \;)
      29. Expression ([])
      30. Expression ([[]])
      31. The array index ([])
      32. Characters range ([])
      33. Integer expansion ($[…])
      34. Integer expansion (((..)))
      35. DEMO
        1. The here document (<<)
        2. The here string (<<<)
        3. The ASCII comparison operators (<) and (>)
      36. Delimiters (\< and \>)
      37. The pipe character (|)
      38. The force redirection (>|)
      39. The logical OR (||)
      40. DEMO
      41. Logical AND
      42. The dash character (-)
      43. The double dash (--)
      44. Operator =
      45. Operator +
      46. The modulo operator (%)
      47. Operator ~
      48. Operator ~+
      49. Operator ~-
      50. Operator ~=
      51. Operator ^
      52. The control characters (^ and ^^)
    2. Quoting and escaping
      1. The backslash (\)
      2. Double quotes ("")
      3. Single quotes (')
    3. Summary
  6. Menus, Arrays, and Functions
    1. The case statement
    2. Arrays
    3. Functions
    4. Summary
  7. Iterations
    1. The for loop
    2. Let's do something while, until…
    3. Exiting the loop with break and continue
    4. Time to give our client a menu
    5. CLI, passing the arguments to the command line
    6. Summary
  8. Plug into the Real World
    1. What is Nagios?
    2. Active and passive checks
      1. Active checks
      2. Passive checks
    3. Returning code and thresholds
    4. Command and service definitions
    5. Our first Nagios plugin
    6. Summary
  9. We Want to Chat
    1. The Slack messaging service
    2. Slack WebHooks
    3. What is a JSON?
    4. Do you like cURLing?
    5. Formatting our messages
    6. Message attachments
    7. Our wee chatty script for Slack
    8. Summary
  10. Subshells, Signals, and Job Controls
    1. What is a subshell?
    2. Background processes
    3. Signals
    4. Job controls
    5. Subshells and parallel processing
    6. Summary
  11. Lets Make a Process Chat
    1. Pipes
    2. Redirection to a file
    3. The command substitution
    4. The process substitution
    5. Environment variables
    6. Coprocesses
    7. /dev/tcp and /dev/udp
    8. Netcat
    9. Summary
  12. Living as a Daemon
    1. What is a daemon?
    2. DEMO
      1. nohup
    3. disown
    4. Double fork and setsid
    5. Becoming a daemon
    6. Trapping a daemon
    7. Going dark with the daemon
    8. Summary
  13. Remote Connections over SSH
    1. What is SSH?
    2. Configuration files
    3. The sshd_config file
    4. ssh_config
    5. Passwordless connections
      1. Configuring the server
      2. Preparing the remote account
      3. Configuring the client
    6. Proxies and tunnels
    7. Summary
  14. Its Time for a Timer
    1. One shot at it
    2. The cron scheduler
      1. cron
    3. Summary
  15. Time for Safety
    1. The restricted shell
      1. Restricted shells for OpenSSH
    2. Restricted sftp sessions with OpenSSH
    3. Summary

Product information

  • Title: Mastering Bash
  • Author(s): Giorgio Zarrelli
  • Release date: June 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781784396879