www.it-ebooks.info Linux Shell Scripting Cookbook Solve real-world shell scripting problems with over 110 simple but incredibly effective recipes Sarath Lakshman BIRMINGHAM - MUMBAI www.it-ebooks.info Linux Shell Scripting Cookbook Copyright © 2011 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: January 2011 Production Reference: 1200111 Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 978-1-849513-76-0 www.packtpub.com Cover Image by Charwak A (charwak86@gmail.com) www.it-ebooks.info Credits Author Sarath Lakshman Reviewers Aric Pedersen John L Weatherwax Acquisition Editor Steven Wilding Development Editor Hyacintha D'Souza Technical Editor Hithesh Uchil Indexer Tejal Daruwale Editorial Team Leader Aanchal Kumar Project Team Leader Priya Mukherji Project Coordinator Jovita Pinto Proofreader Lesley Harrison Production Coordinator Kruthika Bangera Cover Work Kruthika Bangera www.it-ebooks.info About the Author Sarath Lakshman is a 21 year old who was bitten by the Linux bug during his teenage years. He is an undergraduate student of Computer Science and Engineering at Model Engineering College, Cochin, Kerala. Sarath is a life hacker who loves to explore innovations. He is a GNU/Linux enthusiast and hactivist of free and open source software. He spends most of his time hacking with computers and having fun with his great friends. Sarath is well known as the developer of SLYNUX (2005)—a user friendly GNU/Linux distribution for Linux newbies. The free and open source software projects he has contributed to are PiTiVi Video editor, SLYNUX GNU/Linux distro, Swathantra Malayalam Computing, School-Admin, Istanbul, and the Pardus Project. He has authored many articles for the Linux For You magazine on various domains of FOSS technologies. He had made a contribution to several different open source projects during his multiple Google Summer of Code projects. He currently lives with his parents in Kerala. Sarath can be reached via his website http://www.sarathlakshman.com. I would like to thank my friends and family for the great support and encouragement they have given me for all my endeavors. I would like to thank my friends Anu Mahadevan and Neenu Jacob for the tireless enthusiasm and patience to read through the chapter developments and providing comments during development. I would also like to thank Mr. Atanu Datta for helping me come up with the chapter titles. I extend my gratitude to the team at Packt Publishing who helped me in making this book happen. www.it-ebooks.info About the Reviewers Aric Pedersen is the author of cPanel User Guide and Tutorial (ISBN 978-1-904811- 92-3) and Web Host Manager Administration Guide (ISBN 978-1-904811-50-3), both written for Packt Publishing. He also served as a reviewer for CUPS Administrative Guide (ISBN 978-1-84719-258-5) and Linux E-mail Second Edition (ISBN 978-1-847198-64-8) published by Packt Publishing. Aric has over nine years of experience working as a System Administrator. He currently works for Hostdime.com, the world-class web host; and also for Netenberg.com, makers of Fantastico, the world's most popular web script installer for cPanel servers. I would like to thank PMUG (http://www.pmug-nj.org/) for years of interesting content and camaraderie. John L Weatherwax has been in love with computers ever since he had access to one. An applied mathematician by trade, he began his study of computer programming with the Pascal programming language on the computer that his father bought way before most people had even thought of using a computer for anything truly practical. As an undergraduate, he obtained dual degrees in physics and mathematics from the University of Missouri Columbia and as a graduate student he obtained a Ph.D. from M.I.T. in Applied Mathematics. After graduating, he worked for many years applying pattern recognition techniques to various problems in electrical engineering before transitioning to mathematical nance. In school, he was strongly inuenced by the UNIX/Linux philosophy of using simpler component programs together to solve larger problems. Many years later, while exploring various trading strategies as a quantitative analysis at a Wall Street rm, he still uses UNIX commands and the UNIX philosophy on a daily basis. Most of the work he does now is related to using mathematical models to predict prices and trade various nancial instruments. He spends his free time working on applied mathematics and pattern recognition (much of which can be found on his website at http://waxworksmath.com/) and spending time with his family. www.it-ebooks.info www.PacktPub.com Support les, eBooks, discount offers, and more You might want to visit www.PacktPub.com for support les and downloads related to your book. Did you know that Packt offers eBook versions of every book published, with PDF and ePub les available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read, and search across Packt's entire library of books. Why Subscribe? • Fully searchable across every book published by Packt • Copy and paste, print, and bookmark content • On demand and accessible via web browser Free Access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access. www.it-ebooks.info www.it-ebooks.info www.it-ebooks.info This book is dedicated to my parents who showed me the bright choices in life. www.it-ebooks.info [...]... the shell The shell environment helps users to interact with and access core functions of the operating system The term scripting is more relevant in this context Scripting is usually supported by interpreter-based programming languages Shell scripts are files in which we write a sequence of commands that we need to perform And the script file is executed using the shell utility www.it-ebooks.info Shell. .. we are dealing with Bash (Bourne Again Shell) , which is the default shell environment for most GNU /Linux systems Since GNU /Linux is the most prominent operating system based on a UNIX-style architecture, most of the examples and discussions are written by keeping Linux systems in mind The primary purpose of this chapter is to give readers an insight about the shell environment and become familiar with... solving the scripting problems explained in the book In order to run and test the examples in the book, an Ubuntu Linux installation is recommended, however, any other Linux distribution is enough for most of the tasks You will find the book to be a straightforward reference to essential shell scripting tasks as well as a learning aid to code real-world efficient scripts Who this book is for If you are... 296 304 307 309 311 312 313 316 321 325 Preface 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 An understanding of shell scripting helps you to have better awareness of the... shell, and so on 2 www.it-ebooks.info Preface What you need for this book Basic user experience with any GNU /Linux platform will help you easily follow the book We have tried to keep all the recipes in the book precise and as simple to follow as possible Your curiosity for learning with the Linux platform is the only prerequisite for the book Step-by-step explanations are provided for solving the scripting. .. basic features that come around the shell Commands are typed and executed in a shell terminal When opened, in a terminal, a prompt is available It is usually in the following format: username@hostname$ Or: root@hostname# Or simply as $ or # $ represents regular users and # represents the administrative user root Root is the most privileged user in a Linux system A shell script is a text file that typically... USER, UID, SHELL, and so on There's more Let's see some more tips associated with regular and environment variables Finding length of string Get the length of a variable value as follows: length=${#var} For example: $ var=12345678901234567890 $ echo ${#var} 20 length is the number of characters in the string Identifying the current shell Display the currently used shell as follows: echo $SHELL Or,... text processing tools well under GNU /Linux with a number of task examples It also has supplementary recipes for giving a detailed overview of regular expressions and commands such as sed and awk This chapter goes through solutions to most of the frequently used text processing tasks in a variety of recipes Chapter 5, Tangled Web? Not At All!, has a collection of shell- scripting recipes that are adherent... manual tasks with a few lines of script, saving you an enormous amount of time Shell scripts can work with many external command-line utilities for tasks such as querying information, easy text manipulation, scheduling task running times, preparing reports, sending mails, and so on There are numerous commands on the GNU /Linux shell, which are documented but hard to understand This book is a collection... tools for fun or serious system administration? This cookbook is for you Start reading! What this book covers Chapter 1, Shell Something Out, has a collection of recipes that covers the basic tasks such as printing in the terminal, performing mathematical operations, arrays, operators, functions, aliases, file redirection, and so on by using Bash scripting This chapter is an introductory chapter for . www.it-ebooks.info Linux Shell Scripting Cookbook Solve real-world shell scripting problems with over 110 simple but incredibly. recipes Sarath Lakshman BIRMINGHAM - MUMBAI www.it-ebooks.info Linux Shell Scripting Cookbook Copyright © 2011 Packt Publishing All rights reserved. No