Book Image

Linux Shell Scripting Essentials

Book Image

Linux Shell Scripting Essentials

Overview of this book

Shell scripting is a quick method to prototype complex applications or problems. Shell scripts are a collection of commands to automate tasks, usually those for which the user has a repeated need, when working on Linux-based systems. Using simple commands or a combination of them in a shell can solve complex problems easily. This book starts with the basics, including essential commands that can be executed on Linux systems to perform tasks within a few nanoseconds. You’ll learn to use outputs from commands and transform them to show the data you require. Discover how to write shell scripts easily, execute script files, debug, and handle errors. Next, you’ll explore environment variables in shell programming and learn how to customize them and add a new environment. Finally, the book walks you through processes and how these interact with your shell scripts, along with how to use scripts to automate tasks and how to embed other languages and execute them.
Table of Contents (10 chapters)
9
Index

Chapter 1. The Beginning of the Scripting Journey

Unix, Unix-like, or Linux-based OS provide a lot of powerful features to work upon. Among them, the most powerful and important feature is executing a wide range of commands to perform a task quickly and easily; for example, ls, cat, sort, grep, and so on. We will come to know about a subset of commands and usages throughout this book. In order to run a command, we need an interface that is widely known as shell.

Shell is a program that acts as an interface between the users (we) and the OS kernel (Linux, Unix, and so on). Understanding in terms of Windows OS, shell serves a similar purpose DOS does. Different shells are available for Unix, Unix-like, or Linux OS. Some of the popular shells are Bourne shell (sh), C shell (csh), Korn shell (ksh), Bourne Again shell (bash), and Z shell (zsh).

In this book, we will be using Linux OS and Bourne Again shell, popularly known by its acronym bash. Linux-based systems generally have bash already installed. In case bash is not installed, try installing the bash package from your distribution's package manager. In order to know which shell currently your Linux console is using, run the following command in terminal:

$ ps -p $$

The output is as follows:

  PID TTY          TIME CMD
12578 pts/4    00:00:00 bash

In the preceding ouput, we see that the CMD column has value bash. This means, we are currently using bash shell in our current console.

If your console is not using the bash shell, then you can run the following command:

$ bash

Also, your shell will be bash now. To make bash as a default login shell, run the following command:

$ chsh -s /bin/bash

The output obtained is as follows:

Changing shell for user.
Password:******
Shell changed.

We are now set with bash shell and ready to learn shell scripting in detail. Shell scripts are nothing but plain text files with a series of commands that are run by bash in a specified order. Writing shell scripts is very useful when you have to perform a series of tasks by running various commands, as bash will read each line from a script file and run it without any need of user intervention. The general file extension used for shell scripts are .sh, .bash, .zsh, .ksh, and so on. Rather than using a file extension for shell scripts, it's preferred to keep a filename without extension and let an interpreter identify the type by looking into shebang (#!). Shebang is used in scripts to indicate an interpreter for execution. It is written in the first line of a script file, for example:

#! /bin/bash

It means use the bash shell to execute a given script. To run a shell script, make sure it has execute permission. To provide execute permission to an owner of a file, run the following command:

$ chmod u+x foo

Here, foo is the shell script file. After running this command, foo will have execute permission for the owner of the file.

Now, we are ready to proceed further on learning shell scripting concepts in detail. Each topic and subtopic covered in the chapters with examples will lead us progressively towards a good shell script programmer.

In this chapter, we will talk broadly about the following topics:

  • Hello World in shell
  • Define variables of choice
  • Builtin shell variables
  • Operators
  • Shell expansions
  • Construct commands using eval
  • Make bash behave using set