Book Image

Learning Linux Shell Scripting

By : Ganesh Sanjiv Naik
Book Image

Learning Linux Shell Scripting

By: Ganesh Sanjiv Naik

Overview of this book

Linux is the one of the most powerful and universally adopted OSes. Shell is a program that gives the user direct interaction with the operating system. Scripts are collections of commands that are stored in a file. The shell can read this file and act on the commands as if they were typed on the keyboard. Shell scripting is used to automate day-to-day administration, and for testing or product development tasks. This book covers Bash, GNU Bourne Again SHell, preparing you to work in the exciting world of Linux shell scripting. We start with an introduction to the Shell environment and explain basic commands used in Shell. Next we move on to check, kill, and control the execution of processes in Linux OS. Further, we teach you about the filter tools available in Linux and explain standard output and standard errors devices. Then we will ensure you understand Shell’s interpretation of commands and get a firmer grasp so you use them in practice. Next, you’ll experience some real-world essentials such as debugging and perform Shell arithmetic fluently. Then you’ll take a step ahead and learn new and advanced topics in Shell scripting, such as starting up a system and customizing a Linux system. Finally, you’ll get to understand the capabilities of scripting and learn about Grep, Stream Editor, and Awk.
Table of Contents (20 chapters)
Learning Linux Shell Scripting
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

The IFS and loops


The shell has one environment variable, which is called the Internal Field Separator (IFS). This variable indicates how the words are separated on the command line. The IFS variable is, normally or by default, a white space (' '). The IFS variable is used as a word separator (token) for the for command. In many documents, IFS can be any one of the white space, ':', '|', ': ' or any other desired character. This will be useful while using commands such as read, set, for, and so on. If we are going to change the default IFS, then it is a good practice to store the original IFS in a variable.

Later on, when we have done our required tasks, then we can assign the original character back to IFS.

In the following script for_16.sh, we are using ":" as the IFS character:

#/bin/bash
cities=Delhi:Chennai:Bangaluru:Kolkata
old_ifs="$IFS"           # Saving original value of IFS
IFS=":"
for place in $cities
do
      echo  The name of city is $place
done

Let's test the program:

$ chmod...