Book Image

Bash Cookbook

By : Ron Brash, Ganesh Sanjiv Naik
Book Image

Bash Cookbook

By: Ron Brash, Ganesh Sanjiv Naik

Overview of this book

In Linux, one of the most commonly used and most powerful tools is the Bash shell. With its collection of engaging recipes, Bash Cookbook takes you through a series of exercises designed to teach you how to effectively use the Bash shell in order to create and execute your own scripts. The book starts by introducing you to the basics of using the Bash shell, also teaching you the fundamentals of generating any input from a command. With the help of a number of exercises, you will get to grips with the automation of daily tasks for sysadmins and power users. Once you have a hands-on understanding of the subject, you will move on to exploring more advanced projects that can solve real-world problems comprehensively on a Linux system. In addition to this, you will discover projects such as creating an application with a menu, beginning scripts on startup, parsing and displaying human-readable information, and executing remote commands with authentication using self-generated Secure Shell (SSH) keys. By the end of this book, you will have gained significant experience of solving real-world problems, from automating routine tasks to managing your systems and creating your own scripts.
Table of Contents (15 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

Formatting your data/output using echo and printf


Sometimes, finding the string or the exact data you are looking for is the easy part of your task, but formatting the output data is tricky. For example, here are some situations that have subtle elements that need to be altered:

  • Echoing output without the newline terminator (\n)
  • Echoing raw hexadecimal (hex) data
  • Printing raw hexadecimal values and printable ASCII characters
  • Concatenating strings
  • Escaping specific characters
  • Aligning text
  • Printing horizontal rules

In addition to tricks, we can also print values to the screen that are also floats as well (in addition to the recipe for math). Wait, what is a hexadecimal number? Yes, another type of data or at least a representation exists. To understand what hexadecimals are, we first need to remember that computers use binary, which consists of 1s and 0s (ones and zeros). However, binary is not very friendly to us humans (we use the decimal format when looking at numbers typically), so other representations...