Skip to main content

2023 | Buch

Pro Bash

Learn to Script and Program the GNU/Linux Shell

insite
SUCHEN

Über dieses Buch

Learn how to effectively utilize the Bash shell in your programming. This refreshed and expanded third edition has been updated to Bash 5.2, and many scripts have been rewritten to make them more idiomatically Bash, taking better advantage of features specific to Bash. It is easy to read, understand, and will teach you how to get to grips with Bash programming without drowning you in pages and pages of syntax.

Using this book you will be able to use the shell efficiently, make scripts run faster using expansion and external commands, and understand how to overcome many common mistakes that cause scripts to fail. This book is perfect for all beginning Linux and Unix system administrators who want to be in full control of their systems, and really get to grips with Bash programming.

The Bash shell is a complete programming language, not merely a glue to combine external Linux commands. By taking full advantage of Shell internals, Shell programs can perform as snappily as utilities written in C or other compiled languages. And you will see how, without assuming UNIX lore, you can write professional Bash programs through standard programming techniques.

What You'll Learn

Use the Bash shell to write utilities and accomplish most programming tasksReplace many external commands with shell parameter expansion making scripts very fastAvoid many common mistakes that cause scripts to failSee how Bash’s read line and history libraries can save typing when getting user inputBuild shell scripts that get information from the Web

Who This Book Is For

Developers, programmers, and open source enthusiasts who want to write scripts using Bash on multiple platforms

Inhaltsverzeichnis

Frontmatter
Chapter 1. Hello, World: Your First Shell Program
Abstract
A shell script is a file containing one or more commands that you would type on the command line. This chapter describes how to create such a file and make it executable. It also covers some other issues surrounding shell scripts, including what to name the files, where to put them, and how to run them.
Jayant Varma
Chapter 2. Input, Output, and Throughput
Abstract
Two of the commands we used in Chapter 1 are workhorses of the shell scripter’s stable: echo and printf. Both are bash built-in commands. Both print information to the standard output stream, but printf is much more powerful, and echo has its problems.
Jayant Varma
Chapter 3. Looping and Branching
Abstract
At the heart of any programming language are iteration and conditional execution. Iteration is the repetition of a section of code until a condition changes. Conditional execution is making a choice between two or more actions (one of which may be to do nothing) based on a condition.
Jayant Varma
Chapter 4. Command-Line Parsing and Expansion
Abstract
One of the things that shell programming language does is the parsing of command-line arguments and the expansion it performs on words in the line. Let us look in detail what happens when a command I called with arguments, what does the shell do before it invokes the command.
Jayant Varma
Chapter 5. Parameters and Variables
Abstract
Variables have been part of the Unix shell since its inception more than 30 years ago, but their features have grown over the years. The standard Unix shell now has parameter expansions that perform sophisticated manipulations on their contents. bash adds even more expansion capabilities as well as indexed and associative arrays.
Jayant Varma
Chapter 6. Shell Functions
Abstract
A shell function is a compound command that has been given a name. It stores a series of commands for later execution. The name becomes a command and can be used in the same way as any other command. Its arguments are available in the positional parameters, just as in any other script. Like other commands, it sets a return code.
Jayant Varma
Chapter 7. String Manipulation
Abstract
In the Bourne shell, very little string manipulation was possible without resorting to external commands. Strings could be concatenated by juxtaposition, they could be split by changing the value of IFS, and they could be searched with case, but anything else required an external command.
Jayant Varma
Chapter 8. File Operations and Commands
Abstract
Because the shell is an interpreted language, it is comparatively slow. Many operations on files are best done with external commands that implicitly loop over the lines of a file. At other times, the shell itself is more efficient. This chapter looks at how the shell works with files – both shell options that modify and extend file name expansion and shell options that read and modify the contents of files. Several external commands that work on files are explained, often accompanied by examples of when not to use them.
Jayant Varma
Chapter 9. Reserved Words and Built-In Commands
Abstract
There are almost 60 built-in commands and more than 20 reserved words in bash. Some of them are indispensable, and some are rarely used in scripts. Some are used primarily at the command line, and some are seldom seen anywhere. Some have been discussed already, and others will be used extensively in future chapters.
Jayant Varma
Chapter 10. Writing Bug-Free Scripts and Debugging the Rest
Abstract
A programmer that has never written a buggy program has either not written any code or is more imaginary than Santa Claus or the Easter Bunny. Bugs are the bane of a programmer’s existence. These range from simple typing errors (causing syntax errors) to more complex bad code to faulty logic. These errors can be fixed, some easily while others can take hours or days to identify, trace, and fix.
Jayant Varma
Chapter 11. Programming for the Command Line
Abstract
This book is about programming with the shell, not about using it at the command line. The information provided here is about editing the command line, creating a command prompt (the PS1 variable), or retrieving commands from the interactive history. This chapter is about scripts that will mostly be useful at the command line rather than in other scripts.
Jayant Varma
Chapter 12. Runtime Configuration
Abstract
When I download my e-mail from three or four different POP3 servers, I don’t use a different script for each one. When I open a terminal to ssh to a remote computer (half a dozen of them) with a different background color for each, I use the same script for every connection. To upload files to my websites, I use the same script for all of them.
Jayant Varma
Chapter 13. Data Processing
Abstract
Data manipulation includes a wide range of actions, far more than can be adequately covered in a single chapter. However, most actions are just the application of techniques already covered in earlier chapters. Arrays are a basic data structure, and although the syntax was covered in Chapter 5 and they were used in the fifteen puzzle code in Chapter 11, we haven’t yet explored their uses. Parameter expansion has been used in several chapters, but its application to parsing data structures has not been discussed.
Jayant Varma
Chapter 14. Scripting the Screen
Abstract
Unix purists will shake their heads over this chapter. Traditionally, screen manipulation is done through the termcap or terminfo database that supplies the information necessary to manipulate any of dozens or even hundreds of types of terminals. The shell interface to the database is an external command, tput.
Jayant Varma
Chapter 15. Entry-Level Programming
Abstract
The preference for bash over any other POSIX shell stems to a great extent from its extensions that enhance interactive programming. The extended options to the read built-in command (which were described in Chapter 9), combined with the history and readline libraries, add functionality that no other shell can match.
Jayant Varma
Backmatter
Metadaten
Titel
Pro Bash
verfasst von
Jayant Varma
Copyright-Jahr
2023
Verlag
Apress
Electronic ISBN
978-1-4842-9588-5
Print ISBN
978-1-4842-9587-8
DOI
https://doi.org/10.1007/978-1-4842-9588-5

Premium Partner