Linux Shell Scripting Cookbook, Second Edition - Shantanu Tushar - E-Book

Linux Shell Scripting Cookbook, Second Edition E-Book

Shantanu Tushar

0,0
34,79 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

The shell remains one of the most powerful tools on a computer system — yet a large number of users are unaware of how much one can accomplish with it. Using a combination of simple commands, we will see how to solve complex problems in day to day computer usage.Linux Shell Scripting Cookbook, Second Edition will take you through useful real-world recipes designed to make your daily life easy when working with the shell. The book shows the reader how to effectively use the shell to accomplish complex tasks with ease.The book discusses basics of using the shell, general commands and proceeds to show the reader how to use them to perform complex tasks with ease.Starting with the basics of the shell, we will learn simple commands with their usages allowing us to perform operations on files of different kind. The book then proceeds to explain text processing, web interaction and concludes with backups, monitoring and other sysadmin tasks.Linux Shell Scripting Cookbook, Second Edition serves as an excellent guide to solving day to day problems using the shell and few powerful commands together to create solutions.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 498

Veröffentlichungsjahr: 2013

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Linux Shell Scripting Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers and more
Why Subscribe?
Free Access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Shell Something Out
Introduction
Printing in the terminal
How to do it...
How it works...
There's more...
Escaping newline in echo
Printing a colored output
Playing with variables and environment variables
Getting ready
How to do it...
There's more...
Finding the length of a string
Identifying the current shell
Checking for super user
Modifying the Bash prompt string (username@hostname:~$)
Function to prepend to environment variables
How to do it...
How it works...
Math with the shell
Getting ready
How to do it...
Playing with file descriptors and redirection
Getting ready
How to do it...
How it works...
There's more...
Redirection from a file to a command
Redirecting from a text block enclosed within a script
Custom file descriptors
Arrays and associative arrays
Getting ready
How to do it...
There's more...
Defining associative arrays
Listing of array indexes
Visiting aliases
How to do it...
There's more...
Escaping aliases
Grabbing information about the terminal
Getting ready
How to do it...
Getting and setting dates and delays
Getting ready
How to do it...
How it works...
There's more...
Producing delays in a script
Debugging the script
How to do it...
How it works...
There's more...
Shebang hack
Functions and arguments
How to do it...
There's more...
The recursive function
Exporting functions
Reading the return value (status) of a command
Passing arguments to commands
Reading the output of a sequence of commands in a variable
Getting ready
How to do it...
There's more...
Spawning a separate process with subshell
Subshell quoting to preserve spacing and the newline character
Reading n characters without pressing the return key
How to do it...
Running a command until it succeeds
How to do it...
How it works...
There's more...
A faster approach
Adding a delay
Field separators and iterators
Getting ready
How to do it...
Comparisons and tests
How to do it...
2. Have a Good Command
Introduction
Concatenating with cat
How to do it...
How it works…
There's more...
Getting rid of extra blank lines
Displaying tabs as ^I
Line numbers
Recording and playing back of terminal sessions
Getting ready
How to do it...
How it works...
Finding files and file listing
Getting ready
How to do it...
There's more...
Search based on filename or regular expression match
Negating arguments
Search based on the directory depth
Search based on file type
Search on file times
Search based on file size
Deleting based on the file matches
Match based on the file permissions and ownership
Executing commands or actions with find
Skipping specified directories when using the find command
Playing with xargs
Getting ready
How to do it...
How it works…
There's more...
Passing formatted arguments to a command by reading stdin
Using xargs with find
Counting the number of lines of C code in a source code directory
While and subshell trick with stdin
Translating with tr
Getting ready
How to do it...
How it works…
There's more...
Deleting characters using tr
Complementing character set
Squeezing characters with tr
Character classes
Checksum and verification
Getting ready
How to do it...
How it works...
There's more...
Checksum for directories
Cryptographic tools and hashes
How to do it...
Sorting unique and duplicates
Getting ready
How to do it...
How it works…
There's more...
Sorting according to the keys or columns
uniq
Temporary file naming and random numbers
How to do it...
How it works…
Splitting files and data
How to do it...
There's more…
Specifying a filename prefix for the split files
Slicing filenames based on extension
How to do it…
How it works…
Renaming and moving files in bulk
Getting ready
How to do it...
How it works…
Spell checking and dictionary manipulation
How to do it...
How it works...
Automating interactive input
Getting ready
How to do it...
How it works…
There's more...
Automating with expect
Making commands quicker by running parallel processes
How to do it...
How it works...
3. File In, File Out
Introduction
Generating files of any size
How to do it...
The intersection and set difference (A-B) on text files
Getting ready
How to do it...
How it works...
Finding and deleting duplicate files
Getting ready
How to do it...
How it works...
Working with file permissions, ownership, and the sticky bit
How to do it...
There's more...
Changing ownership
Setting sticky bit
Applying permissions recursively to files
Applying ownership recursively
Running an executable as a different user (setuid)
Making files immutable
Getting ready
How to do it...
Generating blank files in bulk
Getting ready
How to do it...
Finding symbolic links and their targets
How to do it...
How it works...
Enumerating file type statistics
Getting ready
How to do it...
How it works...
Using loopback files
How to do it...
How it works...
There's more...
Creating partitions inside loopback images
Quicker way to mount loopback disk images with partitions
Mounting ISO files as loopback
Flush changing immediately with sync
Creating ISO files and hybrid ISO
Getting ready
How to do it...
There's more...
Hybrid ISO that boots off a flash drive or hard disk
Burning an ISO from the command line
Playing with the CD-ROM tray
Finding the difference between files, patching
How to do it...
There's more...
Generating difference against directories
Using head and tail for printing the last or first 10 lines
How to do it...
Listing only directories – alternative methods
Getting ready
How to do it...
How it works...
Fast command-line navigation using pushd and popd
Getting ready
How to do it...
There's more...
Most frequently used directory switching
Counting the number of lines, words, and characters in a file
How to do it...
Printing the directory tree
Getting ready
How to do it...
There's more...
HTML output for tree
4. Texting and Driving
Introduction
Using regular expressions
How to do it...
How it works...
There's more...
Treatment of special characters
Visualizing regular expressions
Searching and mining a text inside a file with grep
How to do it...
There's more...
Recursively search many files
Ignoring case of pattern
grep by matching multiple patterns
Including and excluding files in a grep search
Using grep with xargs with zero-byte suffix
Silent output for grep
Printing lines before and after text matches
Cutting a file column-wise with cut
How to do it...
There's more
Specifying the range of characters or bytes as fields
Using sed to perform text replacement
How to do it…
There's more...
Removing blank lines
Performing replacement directly in the file
Matched string notation (&)
Substring match notation (\1)
Combination of multiple expressions
Quoting
Using awk for advanced text processing
Getting ready...
How to do it…
How it works…
There's more…
Special variables
Passing an external variable to awk
Reading a line explicitly using getline
Filtering lines processed by awk with filter patterns
Setting delimiter for fields
Reading the command output from awk
Using loop inside awk
String manipulation functions in awk
Finding the frequency of words used in a given file
Getting ready
How to do it...
How it works...
See also
Compressing or decompressing JavaScript
Getting ready
How to do it...
How it works...
See also
Merging multiple files as columns
How to do it...
See also
Printing the nth word or column in a file or line
How to do it...
See also
Printing text between line numbers or patterns
Getting ready
How to do it...
See also
Printing lines in the reverse order
Getting ready
How to do it...
How it works...
Parsing e-mail addresses and URLs from text
How to do it...
How it works...
See also
Removing a sentence in a file containing a word
Getting ready
How to do it...
How it works...
See also
Replacing a pattern with text in all the files in a directory
How to do it...
How it works...
There's more...
Text slicing and parameter operations
How to do it...
See also
5. Tangled Web? Not At All!
Introduction
Downloading from a web page
Getting ready
How to do it...
How it works...
There's more...
Restricting the download speed
Resume downloading and continue
Copying a complete website (mirroring)
Accessing pages with HTTP or FTP authentication
Downloading a web page as plain text
How to do it...
A primer on cURL
Getting ready
How to do it…
How it works...
There's more...
Continuing and resuming downloads
Setting the referer string with cURL
Cookies with cURL
Setting a user agent string with cURL
Specifying a bandwidth limit on cURL
Specifying the maximum download size
Authenticating with cURL
Printing response headers excluding the data
See also
Accessing Gmail e-mails from the command line
How to do it...
How it works...
See also
Parsing data from a website
How to do it...
How it works...
See also
Image crawler and downloader
How to do it...
How it works...
See also
Web photo album generator
Getting ready
How to do it...
How it works...
See also
Twitter command-line client
Getting ready
How to do it...
How it works...
See also
Creating a "define" utility by using the Web backend
Getting ready
How to do it...
How it works...
See also
Finding broken links in a website
Getting ready
How to do it...
How it works...
See also
Tracking changes to a website
Getting ready
How to do it...
How it works...
See also
Posting to a web page and reading the response
Getting ready
How to do it...
How it works...
See also
6. The Backup Plan
Introduction
Archiving with tar
Getting ready
How to do it...
How it works...
There's more...
Appending files to an archive
Extracting files and folders from an archive
stdin and stdout with tar
Concatenating two archives
Updating files in an archive with a timestamp check
Comparing files in the archive and file system
Deleting files from the archive
Compression with the tar archive
Excluding a set of files from archiving
Excluding version control directories
Printing total bytes
See also
Archiving with cpio
How to do it...
How it works...
Compressing data with gzip
How to do it...
There's more...
Gzip with tarball
zcat - reading gzipped files without extracting
Compression ratio
Using bzip2
Using lzma
See also
Archiving and compressing with zip
How to do it...
How it works...
Faster archiving with pbzip2
Getting ready
How to do it...
How it works...
There's more...
Manually specifying the number of CPUs
Specifying the compression ratio
Creating filesystems with compression
Getting ready
How to do it...
There's more...
Excluding files while creating a squashfs file
Backup snapshots with rsync
How to do it...
How it works...
There's more...
Excluding files while archiving with rsync
Deleting non-existent files while updating rsync backup
Scheduling backups at intervals
Version control-based backup with Git
Getting ready
How to do it...
Creating entire disk images using fsarchiver
Getting ready
How to do it...
How it works...
7. The Old-boy Network
Introduction
Setting up the network
Getting ready
How to do it...
There's more...
Printing the list of network interfaces
Displaying IP addresses
Spoofing the hardware address (MAC address)
Name server and DNS (Domain Name Service)
DNS lookup
Showing routing table information
See also
Let us ping!
How to do it...
There's more
Round trip time
Limiting the number of packets to be sent
Return status of the ping command
Traceroute
Listing all the machines alive on a network
Getting ready
How to do it...
How it works...
There's more...
Parallel pings
Using fping
See also
Running commands on a remote host with SSH
Getting ready
How to do it...
There's more...
SSH with compression
Redirecting data into stdin of remote host shell commands
Running graphical commands on a remote machine
See also
Transferring files through the network
Getting ready
How to do it...
There's more...
Automated FTP transfer
SFTP (Secure FTP)
The rsync command
SCP (secure copy program)
Recursive copying with SCP
See also
Connecting to a wireless network
Getting ready
How to do it...
How it works...
See also
Password-less auto-login with SSH
Getting ready
How to do it...
Port forwarding using SSH
How to do it...
There's more...
Non-interactive port forward
Reverse port forwarding
Mounting a remote drive at a local mount point
Getting ready
How to do it...
See also
Network traffic and port analysis
Getting ready
How to do it...
How it works...
There's more...
Opened port and services using netstat
Creating arbitrary sockets
Getting ready
How to do it...
There's more...
Quickly copying files over the network
Sharing an Internet connection
Getting ready
How to do it...
Basic firewall using iptables
How to do it...
How it works...
There's more...
8. Put on the Monitor's Cap
Introduction
Monitoring disk usage
Getting ready
How to do it...
There's more...
Displaying disk usage in KB, MB, or Blocks
Displaying the grand total sum of disk usage
Printing files in specified units
Excluding files from the disk usage calculation
Finding the 10 largest size files from a given directory
Disk free information
Calculating the execution time for a command
How to do it...
How it works...
Collecting information about logged in users, boot logs, and boot failures
Getting ready
How to do it...
Listing the top 10 CPU consuming processes in an hour
Getting ready
How to do it...
How it works...
See also
Monitoring command outputs with watch
How to do it...
There's more
Highlighting the differences in the watch output
Logging access to files and directories
Getting ready
How to do it...
How it works...
Logfile management with logrotate
Getting ready
How to do it...
How it works...
Logging with syslog
Getting ready
How to do it...
See also
Monitoring user logins to find intruders
Getting ready
How to do it…
How it works…
Remote disk usage health monitor
Getting ready
How to do it…
How it works…
See also
Finding out active user hours on a system
Getting ready
How to do it…
How it works…
Measuring and optimizing power usage
Getting ready
How to do it...
Monitoring disk activity
Getting ready
How to do it...
Checking disks and filesystems for errors
Getting ready
How to do it...
How it works...
9. Administration Calls
Introduction
Gathering information about processes
Getting ready
How to do it...
How it works...
There's more...
top
Sorting the ps output with respect to a parameter
Finding the process ID when given command names
Filters with ps for real user or ID, effective user or ID
TTY filter for ps
Information about process threads
Specifying output width and columns to be displayed
Showing environment variables for a process
About which, whereis, file, whatis, and load average
See also
Killing processes and send or respond to signals
Getting ready
How to do it...
There's more...
The kill family of commands
Capturing and responding to signals
Sending messages to user terminals
Getting ready
How to do it...
How it works...
Gathering system information
How to do it...
Using /proc for gathering information
How to do it...
Scheduling with cron
Getting ready
How to do it…
How it works...
There's more…
Specifying environment variables
Running commands at system start up/boot
Viewing the cron table
Removing the cron table
Writing and reading the MySQL database from Bash
Getting ready
How to do it…
How it works…
User administration script
How to do it…
How it works…
Bulk image resizing and format conversion
Getting ready
How to do it..
How it works…
See also
Taking screenshots from the terminal
Getting ready
How to do it...
Managing multiple terminals from one
Getting ready
How to do it...
Index

Linux Shell Scripting Cookbook

Second Edition

Linux Shell Scripting Cookbook

Second Edition

Copyright © 2013 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 authors, 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

Second edition: May 2013

Production Reference: 1140513

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham B3 2PB, UK.

ISBN 978-1-78216-274-2

www.packtpub.com

Cover Image by Parag Kadam (<[email protected]>)

Credits

Authors

Shantanu Tushar

Sarath Lakshman

Reviewers

Rajeshwari K.

John C. Kennedy

Anil Kumar

Sudhendu Kumar

Aravind SV

Acquisition Editor

Kartikey Pandey

Lead Technical Editor

Ankita Shashi

Technical Editors

Jalasha D'costa

Amit Ramadas

Lubna Shaikh

Project Coordinator

Shiksha Chaturvedi

Proofreader

Linda Morris

Indexer

Hemangini Bari

Production Coordinator

Shantanu Zagade

Cover Work

Shantanu Zagade

About the Authors

Shantanu Tushar is an advanced GNU/Linux user since his college days. He works as an application developer and contributes to the software in the KDE projects.

Shantanu has been fascinated by computers since he was a child, and spent most of his high school time writing C code to perform daily activities. Since he started using GNU/Linux, he has been using shell scripts to make the computer do all the hard work for him. He also takes time to visit students at various colleges to introduce them to the power of Free Software, including its various tools. Shantanu is a well-known contributor in the KDE community and works on Calligra, Gluon and the Plasma subprojects. He looks after maintaining Calligra Active – KDE's office document viewer for tablets, Plasma Media Center, and the Gluon Player. One day, he believes, programming will be so easy that everybody will love to write programs for their computers.

Shantanu can be reached by e-mail on <[email protected]>, shantanutushar on identi.ca/twitter, or his website http://www.shantanutushar.com.

I would like to thank my friends and family for the support and encouragement they've given me, especially to my sweet sister for her patience when I couldn't get time to talk to her. I am particularly thankful to Sinny Kumari for patiently testing the scripts to make sure they function properly and Sudhendu Kumar for helping me with the recipe on GNU Screen.

I must also thank Krishna, Madhusudan, and Santosh who introduced me to the wonderful world of GNU/Linux and Free Software. Also, a big thanks to all the reviewers of the book for taking the time to painfully go through every minute detail in the book and help me in improving it. I am also thankful to the whole team at Packt Publishing, without whose efforts and experience, this second edition wouldn't have happened.

Sarath Lakshman is a 23 year old who was bitten by the Linux bug during his teenage years. He is a software engineer working in ZCloud engineering group at Zynga, India. He 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. Currently, he is exploring his passion about scalable distributed systems in his spare time. Sarath can be reached via his website http://www.sarathlakshman.com.

About the Reviewers

Rajeshwari K. received her B.E degree (Information Science and Engineering) from VTU in 2004 and M. Tech degree (Computer Science and Engineering) from VTU in 2009. From 2004 to 2007 she handled a set of real-time projects and did some freelancing. Since 2010 she has being working as Assistant Professor at BMS College of Engineering in the department of Information Science and Engineering. She has a total of five years' experience in teaching in Computer Science subjects.

BMS College of Engineering, Bangalore is one of the autonomous colleges running under VTU with high acclamation nationwide.

Her research interests include operating systems and system-side programming.

John C. Kennedy has been administering Unix and Linux servers and workstations since 1997. He has experience with Red Hat, SUSE, Ubuntu, Debian, Solaris, and HP-UX. John is also experienced in Bash shell scripting and is currently teaching himself Python and Ruby. John has also been a Technical Editor for various publishers for over 10 years specializing in books related to open source technologies.

When John is not geeking out in front of either a home or work computer, he helps out with a German Shepherd Rescue in Virginia by fostering some great dogs or helping with their IT needs.

I would like to thank my family (my wonderful wife, Michele, my intelligent and caring daughter Denise, and my terrific and smart son, Kieran) for supporting the (sometimes) silly things and not so silly things I do. I'd also like to thank my current foster dogs for their occasional need to keep their legs crossed a little longer while I test things out from the book and forget they are there.

Anil Kumar is a software developer. He received his Computer Science undergraduate degree from BITS Pilani. He has work experience of more than two years in the field of Web Development and Systems. Besides working as a software developer, Anil is an open source evangelist and a blogger. He currently resides in Bangalore. He can be contacted at <[email protected]>.

Sudhendu Kumar has been a GNU/Linux user for more than five years. Presently being a software developer for a networking giant, in free time, he also contributes to KDE.

I would like to thank the publishers for giving me this opportunity to review the book. I hope readers find the book useful and they enjoy reading it.

Aravind SV has worked with various Unix-like systems and shells over many years. You can contact him at <[email protected]>.

www.PacktPub.com

Support files, eBooks, discount offers and more

You might want to visit www.PacktPub.com for support files and downloads related to your book.

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files 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 <[email protected]> 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 PacktCopy and paste, print and bookmark contentOn 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.

Dedicated to my parents who taught me how to think and reason, and to be optimistic in every situation in life

—Shantanu Tushar

Preface

GNU/Linux is one of the most powerful and flexible operating systems in the world. In modern computing, there is absolutely no space where it is not used—from servers, portable computers, mobile phones, tablets to supercomputers, everything runs Linux. While there are beautiful and modern graphical interfaces available for it, the shell still remains the most flexible way of interacting with the system.

In addition to executing individual commands, a shell can follow commands from a script, which makes it very easy to automate tasks. Examples of such tasks are preparing reports, sending e-mails, performing maintenance, and so on. This book is a collection of chapters which contain recipes to demonstrate real-life usages of commands and shell scripts. You can use these as a reference, or an inspiration for writing your own scripts. The tasks will range from text manipulation to performing network operations to administrative tasks.

As with everything, the shell is only as awesome as you make it. When you become an expert at shell scripting, you can use the shell to the fullest and harness its true power. Linux Shell Scripting Cookbook shows you how to do exactly that!

What this book covers

Chapter 1, Shell Something Out, is an introductory chapter for understanding the basic concepts and features in Bash. We discuss printing text in the terminal, doing mathematical calculations, and other simple functionalities provided by Bash.

Chapter 2, Have a Good Command, shows commonly used commands that are available with GNU/Linux. This chapter travels through different practical usage examples that users may come across and that they could make use of. In addition to essential commands, this second edition talks about cryptographic hashing commands and a recipe to run commands in parallel, wherever possible.

Chapter 3, File In, File Out, contains a collection of recipes related to files and filesystems. This chapter explains how to generate large-size files, installing a filesystem on files, mounting files, and creating ISO images. We also deal with operations such as finding and removing duplicate files, counting lines in a file collecting details about files, and so on.

Chapter 4, Texting and Driving, has a collection of recipes that explains most of the command-line 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. It is an essential read for any serious task.

Chapter 5, Tangled Web? Not At All!, has a collection of shell-scripting recipes that talk to services on the Internet. This chapter is intended to help readers understand how to interact with the Web using shell scripts to automate tasks such as collecting and parsing data from web pages. This is discussed using POST and GET to web pages, writing clients to web services. The second edition uses new authorization mechanisms such as OAuth for services such as Twitter.

Chapter 6, The Backup Plan, shows several commands used for performing data back up, archiving, compression, and so on. In addition to faster compression techniques, this second edition also talks about creating entire disk images.

Chapter 7, The Old-boy Network, has a collection of recipes that talks about networking on Linux and several commands useful for writing network-based scripts. The chapter starts with an introductory basic networking primer and goes on to cover usages of ssh – one of the most powerful commands on any modern GNU/Linux system. We discuss advanced port forwarding, setting up raw communication channels, configuring the firewall, and much more.

Chapter 8, Put on the Monitor's Cap, walks through several recipes related to monitoring activities on the Linux system and tasks used for logging and reporting. The chapter explains tasks such as calculating disk usage, monitoring user access, and CPU usage. In this second edition, we also learn how to optimize power consumption, monitor disks, and check their filesystems for errors.

Chapter 9, Administration Calls, has a collection of recipes for system administration. This chapter explains different commands to collect details about the system and user management using scripting. We also discuss bulk image resizing and accessing MySQL databases from the shell. New in this edition is that we learn how to use the GNU Screen to manage multiple terminals without needing a window manager.

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 problems explained in the book. In order to run and test the examples in the book, a Ubuntu/Debian 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 a beginner, or an intermediate user, who wants to master the skill of quickly writing scripts to perform various tasks without reading entire man pages, this book is for you. You can start writing scripts and one-liners by simply looking at a similar recipe and its descriptions without any working knowledge of shell scripting or Linux. Intermediate or advanced users, as well as system administrators or developers and programmers, can use this book as a reference when they face problems while coding.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "We create a function called repeat that has an infinite while loop, which attempts to run the command passed as a parameter (accessed by $@) to the function."

A block of code is set as follows:

if [ $var -eq 0 ]; then echo "True"; fi can be written as if test $var -eq 0 ; then echo "True"; fi

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

while read line; do something done < filename

Any command-line input or output is written as follows:

# mkdir /mnt/loopback# mount -o loop loopbackfile.img /mnt/loopback

New terms and important words are shown in bold.

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to <[email protected]>, and mention the book title via the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the erratasubmissionform link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.

Chapter 1. Shell Something Out

In this chapter, we will cover:

Printing in the terminalPlaying with variables and environment variablesFunction to prepend to environment variablesMath with the shellPlaying with file descriptors and redirectionArrays and associative arrayVisiting aliasesGrabbing information about the terminalGetting and setting dates and delaysDebugging the scriptFunctions and argumentsReading output of a sequence of commands in a variableReading n characters without pressing the return keyRunning a command until it succeedsField separators and iteratorsComparisons and tests

Introduction

Unix-like systems are amazing operating system designs. Even after many decades, Unix-style architecture for operating systems serves as one of the best designs. One of the important features of this architecture is the command-line interface, or 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 are executed using the shell utility.

In this book 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 on 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 into the shell environment and become familiar with the basic features that the shell offers. Commands are typed and executed in a shell terminal. When a terminal is opened, a prompt is available which usually has 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.

Note

It is usually a bad idea to directly use the shell as the root user (administrator) to perform tasks. This is because typing errors in your commands have the potential to do more damage when your shell has more privileges. So, it is recommended to log in as a regular user (your shell will denote that as $ in the prompt, and # when running as root), and then use tools such as `sudo' to run privileged commands. Running a command such as sudo <command> <arguments> will run it as root.

A shell script is a text file that typically begins with a shebang, as follows:

#!/bin/bash

Shebang is a line on which #! is prefixed to the interpreter path. /bin/bash is the interpreter command path for Bash.

Execution of a script can be done in two ways. Either we can run the script as a command-line argument to bash or we can grant execution permission to the script so it becomes executable.

The script can be run with the filename as a command-line argument as follows (the text that starts with # is a comment, you don't have to type it out):

$ bash script.sh # Assuming script is in the current directory.

Or:

$ bash /home/path/script.sh # Using full path of script.sh.

If a script is run as a command-line argument for bash, the shebang in the script is not required.

If required, we can utilize the shebang to facilitate running the script on its own. For this, we have to set executable permissions for the script and it will run using the interpreter path that is appended to #! to the shebang. This can be set as follows:

$ chmod a+x script.sh

This command gives the script.sh file the executable permission for all users. The script can be executed as:

$ ./script.sh #./ represents the current directory

Or:

$ /home/path/script.sh # Full path of the script is used

The kernel will read the first line and see that the shebang is #!/bin/bash. It will identify /bin/bash and execute the script internally as:

$ /bin/bash script.sh

When a shell is started, it initially executes a set of commands to define various settings such as prompt text, colors, and much more. This set of commands are read from a shell script at ~/.bashrc (or ~/.bash_profile for login shells) located in the home directory of the user. The Bash shell also maintains a history of commands run by the user. It is available in the ~/.bash_history file.

Note

~ denotes your home directory, which is usually /home/user where user is your username or /root for the root user.

A login shell is the shell which you get just after logging in to a machine. However, if you open up a shell while logged in to a graphical environment (such as GNOME, KDE, and so on), then it is not a login shell.

In Bash, each command or command sequence is delimited by using a semicolon or a new line. For example:

$ cmd1 ; cmd2

This is equivalent to:

$ cmd1$ cmd2

Finally, the # character is used to denote the beginning of unprocessed comments. A comment section starts with # and proceeds up to the end of that line. The comment lines are most often used to provide comments about the code in the file or to stop a line of code from being executed.

Now let us move on to the basic recipes in this chapter.

Function to prepend to environment variables

Environment variables are often used to store a list of paths of where to search for executables, libraries, and so on. Examples are $PATH, $LD_LIBRARY_PATH, which will typically look like this:

PATH=/usr/bin;/binLD_LIBRARY_PATH=/usr/lib;/lib

This essentially means that whenever the shell has to execute binaries, it will first look into /usr/bin followed by /bin.

A very common task that one has to do when building a program from source and installing to a custom path is to add its bin directory to the PATH environment variable. Let's say in this case we install myapp to /opt/myapp, which has binaries in a directory called bin and libraries in lib.

How to do it...

A way to do this is to say it as follows:

export PATH=/opt/myapp/bin:$PATHexport LD_LIBRARY_PATH=/opt/myapp/lib;$LD_LIBRARY_PATH

PATH and LD_LIBRARY_PATH should now look something like this:

PATH=/opt/myapp/bin:/usr/bin:/binLD_LIBRARY_PATH=/opt/myapp/lib:/usr/lib;/lib

However, we can make this easier by adding this function in .bashrc-:

prepend() { [ -d "$2" ] && eval $1=\"$2':'\$$1\" && export $1; }

This can be used in the following way:

prepend PATH /opt/myapp/binprepend LD_LIBRARY_PATH /opt/myapp/lib

How it works...

We define a function called prepend(), which first checks if the directory specified by the second parameter to the function exists. If it does, the eval expression sets the variable with the name in the first parameter equal to the second parameter string followed by : (the path separator) and then the original value for the variable.

However, there is one caveat, if the variable is empty when we try to prepend, there will be a trailing : at the end. To fix this, we can modify the function to look like this:

prepend() { [ -d "$2" ] && eval $1=\"$2\$\{$1:+':'\$$1\}\" && export $1 ; }

Note

In this form of the function, we introduce a shell parameter expansion of the form:

${parameter:+expression}

This expands to expression if parameter is set and is not null.

With this change, we take care to try to append : and the old value if, and only if, the old value existed when trying to prepend.

Math with the shell

Arithmetic operations are an essential requirement for every programming language. In this recipe, we will explore various methods for performing arithmetic operations in shell.

Getting ready

The Bash shell environment can perform basic arithmetic operations using the commands let, (( )), and []. The two utilities expr and bc are also very helpful in performing advanced operations.

How to do it...

A numeric value can be assigned as a regular variable assignment, which is stored as a string. However, we use methods to manipulate as numbers:
#!/bin/bashno1=4;no2=5;
The let command can be used to perform basic operations directly. While using let, we use variable names without the $ prefix, for example:
let result=no1+no2echo $result
Increment operation:
$ let no1++
Decrement operation:
$ let no1--
Shorthands:
let no+=6 let no-=6

These are equal to let no=no+6 and let no=no-6 respectively.

Alternate methods:

The [] operator can be used in the same way as the let command as follows:

result=$[ no1 + no2 ]

Using the $ prefix inside [] operators are legal, for example:

result=$[ $no1 + 5 ]

(( )) can also be used. $ prefixed with a variable name is used when (( )) operator is used, as follows:

result=$(( no1 + 50 ))

exprcan also be used for basic operations:

result=`expr 3 + 4` result=$(expr $no1 + 5)

All of the preceding methods do not support floating point numbers, and operate on integers only.

bc, the precision calculator is an advanced utility for mathematical operations. It has a wide range of options. We can perform floating point operations and use advanced functions as follows:
echo "4 * 0.56" | bc2.24no=54; result=`echo "$no * 1.5" | bc`echo $result81.0

Additional parameters can be passed to bc with prefixes to the operation with semicolon as delimiters through stdin.

Decimal places scale with bc: In the following example the scale=2 parameter sets the number of decimal places to 2. Hence, the output of bc will contain a number with two decimal places:
echo "scale=2;3/8" | bc 0.37
Base conversion with bc: We can convert from one base number system to another one. Let us convert from decimal to binary, and binary to octal:
#!/bin/bash Desc: Number conversion no=100 echo "obase=2;$no" | bc 1100100 no=1100100 echo "obase=10;ibase=2;$no" | bc 100
Calculating squares and square roots can be done as follows:
echo "sqrt(100)" | bc #Square root echo "10^10" | bc #Square

Playing with file descriptors and redirection

File descriptors are integers that are associated with file input and output. They keep track of opened files. The best-known file descriptors are stdin, stdout, and stderr. We even can redirect the contents of one file descriptor to another. This recipe shows examples on how to manipulate and redirect with file descriptors.

Getting ready

While writing scripts we use standard input (stdin), standard output (stdout), and standard error (stderr) frequently. Redirection of an output to a file by filtering the contents is one of the essential things we need to perform. While a command outputs some text, it can be either an error or an output (nonerror) message. We cannot distinguish whether it is output text or an error text by just looking at it. However, we can handle them with file descriptors. We can extract text that is attached to a specific descriptor.

File descriptors are integers associated with an opened file or data stream. File descriptors 0, 1, and 2 are reserved as follows:

0: stdin (standard input)1: stdout (standard output)2: stderr (standard error)

How to do it...

Redirecting or saving output text to a file can be done as follows:
$ echo "This is a sample text 1" > temp.txt

This would store the echoed text in temp.txt by truncating the file, the contents will be emptied before writing.

To append text to a file, consider the following example:
$ echo "This is sample text 2" >> temp.txt
You can view the contents of the file as follows:
$ cat temp.txtThis is sample text 1This is sample text 2
Let us see what a standard error is and how you can redirect it. stderr messages are printed when commands output an error message. Consider the following example:
$ ls +ls: cannot access +: No such file or directory

Here + is an invalid argument and hence an error is returned.

Tip

Successful and unsuccessful commands

When a command returns after an error, it returns a nonzero exit status. The command returns zero when it terminates after successful completion. The return status can be read from special variable $? (run echo $? immediately after the command execution statement to print the exit status).

The following command prints the stderr text to the screen rather than to a file (and because there is no stdout output, out.txt will be empty):

$ ls + > out.txt ls: cannot access +: No such file or directory

In the following command, we redirect stderr to out.txt:

$ ls + 2> out.txt # works

You can redirect stderr exclusively to a file and stdout to another file as follows:

$ cmd 2>stderr.txt 1>stdout.txt

It is also possible to redirect stderr and stdout to a single file by converting stderr to stdout using this preferred method:

$ cmd 2>&1 output.txt

Or the alternate approach:

$ cmd &> output.txt
Sometimes, the output may contain unnecessary information (such as debug messages). If you don't want the output terminal burdened with the stderr details then you should redirect the stderr output to /dev/null, which removes it completely. For example, consider that we have three files a1, a2, and a3. However, a1 does not have the read-write-execute permission for the user. When you need to print the contents of files starting with a, we use the cat command. Set up the test files as follows:
$ echo a1 > a1 $ cp a1 a2 ; cp a2 a3;$ chmod 000 a1 #Deny all permissions

While displaying contents of the files using wildcards (a*), it will show an error message for file a1 as it does not have the proper read permission:

$ cat a*cat: a1: Permission denieda1a1

Here, cat: a1: Permission denied belongs to the stderr data. We can redirect the stderr data into a file, whereas stdout remains printed in the terminal. Consider the following code:

$ cat a* 2> err.txt #stderr is redirected to err.txta1a1$ cat err.txtcat: a1: Permission denied

Take a look at the following code:

$ cmd 2>/dev/null

When redirection is performed for stderr or stdout, the redirected text flows into a file. As the text has already been redirected and has gone into the file, no text remains to flow to the next command through pipe (|), and it appears to the next set of command sequences through stdin.

However, there is a way to redirect data to a file, as well as provide a copy of redirected data as stdin for the next set of commands. This can be done using the tee command. For example, to print stdout in the terminal as well as redirect stdout into a file, the syntax for tee is as follows:
command | tee FILE1 FILE2

In the following code, the stdin data is received by the tee command. It writes a copy of stdout to the out.txt