Book Image

Linux Shell Scripting Cookbook

By : Sarath Lakshman
Book Image

Linux Shell Scripting Cookbook

By: Sarath Lakshman

Overview of this book

<p>GNU/Linux is a remarkable operating system that comes with a complete development environment that is stable, reliable, and extremely powerful. The shell being the native interface to communicate with the operating system is capable of controlling the entire operating system. There are numerous commands on Linux shell which are documented but hard to understand. The man pages are helpful but they are very lengthy and it does not give any clues on key areas where commands can be used. Proper usage of shell commands can easily solve many complex tasks with a few lines of code, but most linux users don't have the right know-how to use the Linux shell to its full potential.<br /><br />Linux Shell Scripting Cookbook is a collection of essential command-line recipes along with detailed descriptions tuned with practical applications. It covers most of the commands on Linux with a variety of usecases accompanied by plenty of examples. This book helps you to perform complex data manipulations involving tasks such as text processing, file management, backups and more with the combination of few commands.<br /><br />Linux Shell Scripting Cookbook shows you how to capitalize on all the aspects of Linux using the shell scripting language. This book teaches you how to use commands to perform simple tasks all the way to scripting complex tasks such as managing large amounts of data on a network.</p> <p>It guides you on implementing some of the most common commands in Linux with recipes that handle any of the operations or properties related with files like searching and mining inside a file with grep. It also shows you how utilities such as sed, awk, grep, cut can be combined to solve text processing related problems. The focus is on saving time by automating many activities that we perform interactively through as browser with a few lines of script.</p> <p>This book will take you from a clear problem description to a fully functional program. The recipes contained within the chapter will introduce the reader to specific problems and provide hands-on solutions.</p>
Table of Contents (16 chapters)
Linux Shell Scripting Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Reading the output of a sequence of commands


One of the best-designed features of shell scripting is the ease of combining many commands or utilities to produce output. The output of one command can appear as the input of another, which passes its output to another command, and so on. The output of this combination can be read in a variable. This recipe illustrates how to combine multiple commands and how its output can be read.

Getting ready

Input is usually fed into a command through stdin or arguments. Output appears as stderr or stdout. While we combine multiple commands, we usually use stdin to give input and stdout for output.

Commands are called as filters. We connect each filter using pipes. The piping operator is "|". An example is as follows:

$ cmd1 | cmd2 | cmd3 

Here we combine three commands. The output of cmd1 goes to cmd2 and output of cmd2 goes to cmd3 and the final output (which comes out of cmd3) will be printed or it can be directed to a file.

How to do it...

Have a look at the following code:

$ ls | cat -n > out.txt

Here the output of ls (the listing of the current directory) is passed to cat -n. cat –n puts line numbers to the input received through stdin. Therefore, its output is redirected to the out.txt file.

We can read the output of a sequence of commands combined by pipes as follows:

cmd_output=$(COMMANDS)

This is called the subshell method. For example:

cmd_output=$(ls | cat -n)
echo $cmd_output

Another method, called back-quotes can also be used to store the command output as follows:

cmd_output=`COMMANDS`

For example:

cmd_output=`ls | cat -n`
echo $cmd_output

Back quote is different from the single quote character. It is the character on the ~ button in the keyboard.

There's more...

There are multiple ways of grouping commands. Let's go through few of them.

Spawning a separate process with subshell

Subshells are separate processes. A subshell can be defined using the ( )operators as follows:

pwd;
(cd /bin; ls);
pwd;

When some commands are executed in a subshell none of the changes occur in the current shell; changes are restricted to the subshell. For example, when the current directory in a subshell is changed using the cd command, the directory change is not reflected in the main shell environment.

The pwd command prints the path of the working directory.

The cd command changes the current directory to the given directory path.

Subshell quoting to preserve spacing and newline character

Suppose we are reading the output of a command to a variable using a subshell or the back-quotes method, we always quote them in double-quotes to preserve the spacing and newline character (\n). For example:

$ cat text.txt
1
2
3

$ out=$(cat text.txt)
$ echo $out
1 2 3 # Lost \n spacing in 1,2,3 

$ out="$(cat tex.txt)"
$ echo $out
1
2
3