Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Linux Shell Scripting

You're reading from   Mastering Linux Shell Scripting Master the complexities of Bash shell scripting and unlock the power of shell for your enterprise

Arrow left icon
Product type Paperback
Published in Dec 2015
Publisher
ISBN-13 9781784396978
Length 198 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Andrew Mallett Andrew Mallett
Author Profile Icon Andrew Mallett
Andrew Mallett
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. What and Why of Scripting with Bash FREE CHAPTER 2. Creating Interactive Scripts 3. Conditions Attached 4. Creating Code Snippets 5. Alternative Syntax 6. Iterating with Loops 7. Creating Building Blocks with Functions 8. Introducing sed 9. Automating Apache Virtual Hosts 10. Awk Fundamentals 11. Summarizing Logs with Awk 12. A Better lastlog with Awk 13. Using Perl as a Bash Scripting Alternative 14. Using Python as a Bash Scripting Alternative Index

Preparing text editors for scripting

Throughout the book, I will be working on the command line of Raspberry Pi and this will include the creation and editing of the scripts. You, of course, can choose the way you wish to edit your script and may prefer to make use of a graphical editor and I will show some settings in gedit. I will make one excursion to a Red Hat system to show screenshots of gedit in this chapter.

To help make the command line editor easier to use, we can enable options and we can persist with these options through hidden configuration files. The gedit and other GUI editors and their menus will provide similar functionality.

Configuring vim

Editing the command line is often a must and is a part of my everyday life. Setting up common options that make life easier in the editor give us the reliability and consistency you need, a little like scripting itself. We will set some useful options in the vi or vim editor file, $HOME/.vimrc.

The options we set are detailed in the following list:

  • showmode: Ensures we see when we are in insert mode
  • nohlsearch: Does not highlight the words that we have searched for
  • autoindent: We indent our code often; this allows us to return to the last indent level rather than the start of a new line on each carriage return
  • tabstop=4: Sets a tab to be four spaces
  • expandtab: Converts tabs to spaces, which is useful when the file moves to other systems
  • syntax on: Note that this does not use the set command and is used to turn on syntax highlighting

When these options are set, the $HOME/.vimrc file should look similar to this:

setshowmodenohlsearch
setautoindenttabstop=4
setexpandtab
syntax on

Configuring nano

The nano text edit is increasing in importance and it is the default editor in many systems. Personally, I don't like the navigation or the lack of navigation features that it has. It can be customized in the same way as vim. This time we will edit the $HOME/.nanorc file. Your edited file should look something like the following:

setautoindent
settabsize 4
include /usr/share/nano/sh.nanorc

The last line enables syntax highlighting for shell scripts.

Configuring gedit

Graphical editors, such as gedit, can be configured using the preferences menu and are pretty straight forward.

Enabling tab spacing to be set to 4 spaces and expanding tabs to spaces can be done using the Preference | Editor tab, as shown in the following screenshot:

Configuring gedit

Tip

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. 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.

Another very useful feature is found on the Preferences | Plugins tab. Here, we can enable the Snippets plugin that can be used to insert code samples. This is shown in the following screenshot:

Configuring gedit

For the rest of the book, we will be working on the command line in and in vim; feel free to use the editor that you work with best. We have now laid the foundations to create good scripts and although whitespace, tabs, and spaces in bash scripts are not significant; a well laid out file with consistent spacing is easy to read. When we look at Python later in the book, you will realize that in some languages the whitespace is significant to the language and it is better to adopt the good habits early.

You have been reading a chapter from
Mastering Linux Shell Scripting
Published in: Dec 2015
Publisher:
ISBN-13: 9781784396978
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime