📜 ⬆️ ⬇️

The book "Command shell scripts. Linux, OS X and Unix. 2nd edition »

image Command shell scripts help system administrators and programmers automate routine tasks since the first computers appeared. Since the release of the first edition of this book in 2004, much has changed, but the bash shell has only strengthened its leading position. Therefore, the ability to use all its capabilities becomes an urgent need for system administrators, engineers and enthusiasts. This book describes typical problems that can be encountered, for example, when building software or coordinating other programs. And solutions are given so that they can easily be taken as a basis and extrapolated to other similar tasks.

The purpose of this book is to demonstrate practical programming techniques for bash scripts and introduce the most common utilities for short and compact examples, without going into unnecessary details. Experiment with these scenarios - break, fix and adapt them to your needs in order to understand how they work. Only in this way you can solve the most complex tasks.

What disappeared in the second edition


This book describes typical difficulties that can be encountered when trying to write a portable automated solution, for example, to build software or coordinate other programs, and how to overcome them. The solutions in the book are submitted so that you can take them as a basis and extrapolate to other similar problems. For example, in Chapter 1, we will write a portable version of the echo program in the form of a small wrapper script. Many system administrators may find this particular scenario useful, but the basic idea is to create a wrapper script that guarantees uniformity of behavior across platforms. Later in the book, we will examine some interesting features of bash scripts and typical utilities that are available on Unix systems and give us the most extensive features.

This book is for you if ...


Bash remains the main tool for anyone working with servers or workstations running Unix-like operating systems, including web developers (many of which are developing on OS X and deploying their applications on Linux servers) , analysts, mobile application developers and programmers. In addition, there are more and more enthusiasts who run Linux on their open-architecture microcomputers, such as Raspberry Pi, to automate home appliances. The shell scripts are great for all of these cases.
')
The scripts presented in the book will certainly be useful to those who wish to expand the already considerable experience of owning bash by studying practical examples, and to those who use the terminal or shell scripts only occasionally. If you belong to the second camp, you will probably need to refresh your knowledge or supplement it by reading the introduction to the advanced features of bash.

This book is not a tutorial! Our goal is to demonstrate practical scripting programming techniques in bash and introduce common utilities in (in most) short and compact examples, but we do not describe them line by line. We explain only the most basic parts, and experienced script creators can themselves understand how the rest of the code works by reading it. We hope that you, dear reader, will experiment with these scenarios — breaking them down, correcting and adapting them to your needs — in order to understand how they work. Our main goal is to show how to solve typical tasks, such as managing a network or synchronizing files that confront any technical specialist.

Book structure


This second edition includes updated 12 original chapters and 3 new chapters. Each chapter demonstrates new features or use cases for shell scripts, and together they cover the full range of scripting capabilities for easier operation in Unix. Most of the scripts presented in the book will work on both Linux and OS X. In other cases, we will write about it directly.

Chapter 0: A Brief Introduction to Command Shell Scripts
This is a completely new chapter that appeared in the second edition, which will serve novice Unix users with a brief introduction to the syntax of the bash command shell language and features of its use. This chapter will quickly and without lyrical digressions tell you everything you need to successfully read Chapter 1: from simply defining shell scripts to creating and executing plain examples.

Chapter 1: Missing Library
Programming languages ​​commonly used in the Unix environment, such as C, Perl, and Python, have extensive libraries of various functions and utilities for checking number formats, calculating the time intervals between dates, and solving many other problems. But, working with the command shell, we almost have to cope with everything ourselves, so this chapter describes the tools and techniques that will make the shell scripts more friendly. Everything that you learn in the first chapter will help you read the scripts you meet in this book and write your own. We have included various input validation functions, a simple and powerful interface to bc, a tool to quickly add commas to improve readability of large numbers, a trick for Unix variants in which the echo command does not support the useful -n flag, and a script to use ANSI color definition sequences in scripts.

Chapters 2 and 3: Improving User Commands and Building Utilities
These two chapters represent new commands that complement and extend the standard Unix toolkit. After all, continuous development and improvement is one of the hallmarks of Unix. We are also involved in this process and in Chapters 2 and 3 we offer scenarios that implement: a friendly interactive calculator, a file removal tool that does not erase them from disk, two reminder systems and event tracking, an improved version of the locate command, the date command with support for several time zones and the new version of the ls command, which adds additional data to the directory listing lists.

Chapter 4: Tweaking Unix
It may sound like a heresy, but some aspects of Unix look incomplete even after decades of development. If you happen to use different versions of Unix, such as switching from freely distributable Linux distributions to commercial versions of Unix, such as OS X, Solaris or Red Hat, you will encounter missing flags and commands, contradictory behavior of some commands, and other similar problems. Therefore, this chapter will introduce reworked versions and interfaces to Unix commands that make them slightly friendlier or more consistent with other Unix flavors. Among other things, it describes how to add long GNU-style flags to commands that are not GNU commands. Here you will find a couple of intelligent scripts that simplify working with different file compression utilities.

Chapters 5 and 6: System Administration: User Management and System Maintenance
If you are interested in our book, it is likely that you have administrator privileges and you are responsible for administering one or more Unix systems, even if it is just a personal computer with Ubuntu or BSD. These two chapters contain several scenarios that will help you in administration, including: utilities for analyzing disk space usage, disk quota system that automatically notifies users by e-mail about exceeding their allocated disk space, improved implementation of the killall command, verification script crontab, a log file rotation tool and a couple of backup utilities.

Chapter 7: Internet Users
This chapter includes a package of truly interesting shell scripts that demonstrate some of the great and simple ways to use the Unix command line to work with resources on the Internet. These include: a tool for retrieving URLs from any web page, a tool for obtaining a weather forecast, a tool for searching video databases and a tool for detecting changes on a website that automatically reports them by email.

Chapter 8: Webmaster Tools
If you are a webmaster and maintain a website operating on your own Unix system or on a remote server somewhere on the web, in this chapter you will find very interesting tools for designing web pages on the fly, creating web albums with photos and even logging web search results.

Chapters 9 and 10: Web Server Administration and Web Server Administration
These two chapters describe how to solve problems that are often faced by server administrators who have access to the Internet. Here you will find two scenarios that analyze different aspects of logging web server traffic, tools for identifying invalid internal or external links on the website, as well as a handy tool for managing passwords on the Apache web server, which simplifies support for .htaccess files. In addition, the techniques of mirroring directories and entire websites are explored.

Chapter 11: Scripting for OS X
OS X, with its commercially successful and attractive graphical user interface, was a huge step forward in transforming Unix into a friendly operating system. More importantly, OS X is a full-fledged Unix operating system, hidden behind a nice interface, which means you can write many useful and instructive scripts for it. This is what is discussed in this chapter. In addition to a tool for automating image capture on the screen, this chapter presents scripts that help you explore the structure of the iTunes music library, change the Terminal window titles, and refine the open command.

Chapter 12: Scenarios for Fun and Games
What is this book about programming, if it does not have at least a pair of toys? Chapter 12 combines many of the ideas and techniques presented earlier and describes the creation of six fun and fairly complex games. Although the chapter is written to entertain you, the code for each game is very instructive. Especially noteworthy is the game "Hangman", which demonstrates some of the tricks and unusual script programming techniques.

Chapter 13: Working in the Cloud
Since the release of the first edition of this book, the Internet has occupied more and more space in our daily life. Especially important for us is the topic of synchronizing devices and files with cloud services such as iCloud, Dropbox and Google Drive. The chapter demonstrates shell scripts that make full use of these services and ensure timely synchronization and copying of files and directories. In addition, here you will find a couple of scenarios that use OS X features for working with photos and text sound.

Chapter 14: ImageMagick and Image File Processing
Command line applications can process not only text data, but also graphics. This chapter focuses on the identification and processing of images from the command line using a set of tools for working with graphics, including the open-source software ImageMagick. The scenarios in this chapter implement typical operations with images, from defining their types to framing and adding watermarks, plus a few other uses.

Chapter 15: Days and Dates
The final chapter demonstrates techniques that simplify operations with dates and times: how many days separate two dates, on which day of the week the number falls, or how many days are left before it. We will solve these problems using easy-to-use shell scripts.

Appendix A: Installing Bash on Windows 10
While we were working on the second edition, Microsoft significantly changed its attitude towards open source software, and in 2016 even released a full-fledged bash system for Windows 10. Although the examples from the book were not tested in this version of bash, many ideas and solutions will not be difficult transfer to it. In the application, we describe the installation of bash in Windows 10, so you can try your hand at scripting on a Windows computer!

Appendix B: Additional Scenarios
Any good scout knows that there should always be a backup plan! Working on this book, we created backup scripts in case we need to replace one of the main ones. As a result, we did not need backup scripts, but it would be ugly for us to keep them secret from you, our friends. This application includes three additional scenarios: for mass renaming of files, for mass execution of commands and for calculating the phases of the moon - which we could not hide after we showed you 101 scenarios.

About the authors


Dave Taylor has been working in the computer industry since 1980. Participated in the creation of BSD 4.4 UNIX, its programs are included in all major UNIX distributions. An eminent speaker and author of thousands of articles for magazines and newspapers. He has written over 20 books, including Learning Unix for OS X (O'Reilly Media), Solaris 9 for Dummies (Wiley Publishing) and Sams Teach Your Unix in 24 Hours (Sams Publishing). The popular columnist of the journal “Linux Journal” and the founder of the website askdavetaylor.com, where it provides technical support to users and puts reviews of new gadgets.

Brandon Perry began writing applications in C # with the release of the open implementation of .NET - Mono. In his free time, he likes to write modules for the Metasploit framework, examine binaries and test all sorts of things.

About the science reviewer


Jordi Gutierrez Hermoso (Jordi Gutiérrez Hermoso) is a programmer, mathematician and free hacker. Since 2002, it has been using Debian GNU / Linux exclusively, not only at home, but also at work. Jordi is involved in the development of GNU Octave, a free computing environment that is largely compatible with Matlab, as well as Mercurial, a distributed version control system. He is fond of pure and applied mathematics, skating, swimming and knitting. Recently, a lot of thinking about the problems of greenhouse gas emissions and is involved in actions to preserve rhinos.

»More information about the book can be found on the publisher's website.
» Table of Contents
» Excerpt

Hubrozhiteley 25% coupon discount - Shell Scripts

Source: https://habr.com/ru/post/331734/


All Articles