Mastering Linux Shell Scripting A Practical Guide To Linux Command Line Bash Scripting And Shell Programming Second Edition Mokhtar Ebrahim Andrew Mallett pdf download
Mastering Linux Shell Scripting A Practical Guide To Linux Command Line Bash Scripting And Shell Programming Second Edition Mokhtar Ebrahim Andrew Mallett pdf download
https://ebookbell.com/product/mastering-linux-shell-scripting-second-
edition-2nd-edition-mokhtar-ebrahim-38624096
https://ebookbell.com/product/mastering-linux-shell-scripting-master-
the-complexities-of-bash-shell-scripting-and-unlock-the-power-of-
shell-for-your-enterprise-andrew-mallett-5470908
https://ebookbell.com/product/mastering-linux-shell-scripting-andrew-
mallett-96341792
https://ebookbell.com/product/mastering-linux-shell-scripting-andrew-
mallett-mokhtar-ebrahim-232309938
Mastering Linux Security And Hardening A Practical Guide To Protecting
Your Linux System From Cyber Attacks 3rd Edition 3rd Donald A Tevault
https://ebookbell.com/product/mastering-linux-security-and-hardening-
a-practical-guide-to-protecting-your-linux-system-from-cyber-
attacks-3rd-edition-3rd-donald-a-tevault-47936950
https://ebookbell.com/product/mastering-linux-administration-take-
your-sysadmin-skills-to-the-next-level-by-configuring-and-maintaining-
linux-systems-2nd-ed-2nd-alexandru-balog-calcatinge-julian-56998844
Mastering Linux Security And Hardening Secure Your Linux Server And
Protect It From Intruders Malware Attacks And Other External Threats
Tevault
https://ebookbell.com/product/mastering-linux-security-and-hardening-
secure-your-linux-server-and-protect-it-from-intruders-malware-
attacks-and-other-external-threats-tevault-20640198
https://ebookbell.com/product/mastering-linux-device-driver-
development-write-custom-device-drivers-to-support-computer-
peripherals-in-linux-operating-systems-1st-edition-john-
madieu-23274980
https://ebookbell.com/product/mastering-linux-device-driver-
development-john-madieu-30717172
Mastering Linux Shell Scripting
Second Edition
"QSBDUJDBMHVJEFUP-JOVYDPNNBOEMJOF#BTITDSJQUJOHBOE
4IFMMQSPHSBNNJOH
Mokhtar Ebrahim
Andrew Mallett
BIRMINGHAM - MUMBAI
Mastering Linux Shell Scripting
Second Edition
Copyright a 2018 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 or its dealers and distributors, will be held liable for any damages caused or alleged to
have been 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.
ISBN 978-1-78899-055-4
XXXQBDLUQVCDPN
NBQUJP
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as
well as industry leading tools to help you plan your personal development and advance
your career. For more information, please visit our website.
Why subscribe?
Spend less time learning and more time coding with practical eBooks and Videos
from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
PacktPub.com
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 XXX1BDLU1VCDPN and as a
print book customer, you are entitled to a discount on the eBook copy. Get in touch with us
at TFSWJDF!QBDLUQVCDPN for more details.
At XXX1BDLU1VCDPN, 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.
Contributors
I would like to thank my wife for helping me with all her efforts to finish this book. Thank
you, Doaa, for being a part of that. Also, I would like to thank everyone at Packt for
working with me to make sure the book is released. Last but not least, I'd like to thank
Brian Fox, the author of the bash shell, for creating such an awesome piece of software;
without it, such a book would not exist.
Andrew Mallett is the owner of The Urban Penguin, and he is a comprehensive provider of
professional Linux software development, training, and services. Having always been a
command-line fan, he feels that so much time can be saved through knowing command-line
shortcuts and scripting. TheUrbanPenguin YouTube channel, maintained by Andrew, has
well over 800 videos to support this, and he has authored four other Packt titles.
About the reviewer
Sebastiaan Tammer is a Linux enthusiast from The Netherlands. After attaining his BSc in
Information Sciences, he graduated with MSc in Business Informatics, both from Utrecht
University. His professional career started in Java development before he pivoted into
Linux.
He has worked on number of technologies, such as Puppet, Chef, Docker, and Kubernetes.
He spends a lot of time in and around his terminal of choice: bash. Whether it is creating
complex scripting solutions or just automating simple tasks, there is hardly anything he
hasn't done with bash!
I would like to thank my girlfriend, Sanne, for all the help and support she has given me
throughout the years. She has had to endure the late nights studying, me fixing stuff
(which I had inevitably broken only hours earlier), and my endless storytelling about all
those exciting new technologies. Thanks for the enormous amount of patience and love, I
could not have done it without you!
[ ii ]
Table of Contents
[ iii ]
Table of Contents
Questions 119
Further reading 120
Chapter 7: Creating Building Blocks with Functions 121
Technical requirements 121
Introducing functions 122
Passing parameters to functions 125
Passing arrays 128
Variable scope 129
Returning values from functions 130
Recursive functions 132
Using functions in menus 133
Summary 135
Questions 135
Further reading 136
Chapter 8: Introducing the Stream Editor 137
Technical requirements 138
Using grep to display text 138
Displaying received data on an interface 138
Displaying user account data 139
Listing the number of CPUs in a system 140
Parsing CSV files 143
The CSV file 143
Isolating catalog entries 145
Understanding the basics of sed 146
The substitute command 147
Global replacement 148
Limiting substitution 150
Editing the file 150
Other sed commands 151
The delete command 151
The insert and append commands 152
The change command 153
The transform command 153
Multiple sed commands 154
Summary 154
Questions 155
Further reading 156
Chapter 9: Automating Apache Virtual Hosts 157
Technical requirements 158
Apache name-based Virtual Hosts 158
Creating the virtual host template 159
First steps 160
[ iv ]
Table of Contents
[v]
Table of Contents
Questions 210
Further reading 210
Chapter 12: Summarizing Logs with AWK 211
Technical requirements 211
The HTTPD log file format 212
Displaying data from web logs 213
Selecting entries by date 213
Summarizing 404 errors 215
Summarizing HTTP access codes 215
Resources hits 217
Identify image hotlinking 218
Displaying the highest ranking IP address 219
Displaying the browser data 220
Working with email logs 221
Summary 221
Questions 222
Further reading 222
Chapter 13: A Better lastlog with AWK 223
Technical requirements 223
Using AWK ranges to exclude data 224
The lastlog command 224
Horizontally filtering rows with AWK 225
Counting matched rows 225
Conditions based on the number of fields 226
Manipulating the AWK record separator to report on XML data 228
Apache Virtual Hosts 228
XML catalog 229
Summary 231
Questions 232
Further reading 232
Chapter 14: Using Python as a Bash Scripting Alternative 233
Technical requirements 233
What is Python? 234
Saying Hello World the Python way 236
Pythonic arguments 236
Supplying arguments 237
Counting arguments 237
Significant whitespace 239
Reading user input 240
Using Python to write to files 241
String manipulation 242
[ vi ]
Table of Contents
Summary 243
Questions 244
Further reading 244
Assessments 245
Other Books You May Enjoy 252
Index 255
[ vii ]
Preface
First, you'll learn about Linux shells and why we chose the bash shell. Then, you'll learn
how to write a simple bash script and how to edit your bash script using Linux editors.
Following this, you will learn how to define a variable and the visibility of a variable. After
this, you will learn how to store command execution output into a variable, which is called
command substitution. Also, you will learn how to debug your code using bash options
and Visual Studio Code. You will learn how to make your bash script interactive to the user
by accepting input from the user using the read command. Then, you will learn how to read
options and its values if the user passed them to the script. Following this, you will learn
how to write conditional statements such as if statements and how to use case statements.
After this, you will learn how to create code snippets using vim and Visual Studio Code.
For repetitive tasks, you will see how to write for loops, how to iterate over simple values,
and how to iterate over directory content. Also, you will learn how to write nested loops.
Along with this, you will write while and until loops. Then, we will move on to functions,
the reusable chunks of code. You will learn how to write functions and how to use them.
After this, you will be introduced to one of the best tools in Linux, which is Stream Editor.
As we are still talking about text processing, we will introduce AWK, one of the best text
processing tools in Linux that you will ever see.
After this, you will learn how to empower your text processing skills by writing better
regular expressions. Finally, you will be introduced to Python as an alternative to bash
scripting.
$IBQUFS, Creating Interactive Scripts, covers how to read input from the user using SFBE
command, how to pass options to your script, how to control the visibility of the entered
text, and how to limit the number of entered characters.
$IBQUFS, Conditions Attached, will introduce the JG statement, the DBTF statement, and
other testing command such as FMTF and FMJG.
$IBQUFS, Creating Code Snippets, covers creating and using code snippets using editors,
such as vim and Visual Studio Code.
$IBQUFS, Alternative Syntax, will discuss advanced testing using << and how to perform
arithmetic operations.
$IBQUFS, Iterating with Loops, will teach you how to use GPS loops, XIJMF loops, and
VOUJM loops to iterate over simple values and complex values.
$IBQUFS, Creating Building Blocks with Functions, will introduce functions and explains
how to create a function, list builtin functions, pass parameters to functions, and writing
recursive functions.
$IBQUFS, Introducing the Stream Editor, will introduce the basics of sed tool to manipulate
files, such as adding, replacing deleting, and transforming text.
$IBQUFS, Automating Apache Virtual Hosts, contains a practical example of sed and
explains how to create virtual hosts automatically using sed.
$IBQUFS, AWK Fundamentals, will discuss AWK and how to filter file content using it.
Also, we will discuss some AWK programming basics.
$IBQUFS, Regular Expressions, covers regular expressions, their engines, and how to use
them with sed and AWK to empower your script.
$IBQUFS, Summarizing Logs with AWK, will show how to process the IUUQEDPOG
Apache log file using AWK and extract useful well-formatted data.
[2]
Preface
$IBQUFS, A Better lastlog with AWK, will show you how to use AWK to output beautiful
reports using the lastlog command by filtering and processing the lastlog output.
$IBQUFS, Using Python as a Bash Scripting Alternative, will discuss Python programming
language basics and explains how to write some Python scripts as a bash script alternative.
You should know some Linux basics such as the basic commands such as MT, DE, and
XIJDI.
Once the file is downloaded, please make sure that you unzip or extract the folder using the
latest version of:
The code bundle for the book is also hosted on GitHub at IUUQTHJUIVCDPN
1BDLU1VCMJTIJOH.BTUFSJOH-JOVY4IFMM4DSJQUJOH4FDPOE&EJUJPO. In case there's an
update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available
at IUUQTHJUIVCDPN1BDLU1VCMJTIJOH. Check them out!
[3]
Preface
Conventions used
There are a number of text conventions used throughout this book.
$PEF*O5FYU: Indicates code words in text, database table names, folder names, filenames,
file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an
example: "Edit your script so that it reads like the following complete code block
for )0.&CJOIFMMPTI"
Bold: Indicates a new term, an important word, or words that you see onscreen. For
example, words in menus or dialog boxes appear in the text like this. Here is an example:
"Another very useful feature is found on the Preferences | Plugins tab"
[4]
Preface
Get in touch
Feedback from our readers is always welcome.
General feedback: Email GFFECBDL!QBDLUQVCDPN and mention the book title in the
subject of your message. If you have questions about any aspect of this book, please email
us at RVFTUJPOT!QBDLUQVCDPN.
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes
do happen. If you have found a mistake in this book, we would be grateful if you would
report this to us. Please visit XXXQBDLUQVCDPNTVCNJUFSSBUB, selecting your book,
clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the Internet, we
would be grateful if you would provide us with the location address or website name.
Please contact us at DPQZSJHIU!QBDLUQVCDPN with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in
and you are interested in either writing or contributing to a book, please visit
BVUIPSTQBDLUQVCDPN.
Reviews
Please leave a review. Once you have read and used this book, why not leave a review on
the site that you purchased it from? Potential readers can then see and use your unbiased
opinion to make purchase decisions, we at Packt can understand what you think about our
products, and our authors can see your feedback on their book. Thank you!
[5]
1
The What and Why of Scripting
with Bash
Welcome to the what and why of bash scripting. In this chapter, you will discover the types
of shells in Linux and why we chose bash. You will learn what bash is, how to write your
first bash script, and how to run it. Also, you will see how to configure Linux editors, such
as vim and nano, in order to type your code.
Like in any other scripting language, variables are the basic blocks of coding. You will learn
how to declare variables such as integers, strings, and arrays. Furthermore, you will learn
how to export these variables and extend their scope outside the running process.
Finally, you will see how to visually debug your code using Visual Studio Code.
Technical requirements
You'll need a running Linux box. It doesn't matter which distribution you use, since all
Linux distributions are shipped nowadays with the bash shell.
Download and install Visual Studio Code, which is free from Microsoft. You can download
it from here: IUUQTDPEFWJTVBMTUVEJPDPN.
You can use VS Code as an editor instead of vim and nano; it's up to you.
We prefer to use VS Code because it has a lot of features such as code completion,
debugging, and many more besides.
Install CBTIEC, which is a required package for the bash debug plugin. If you are using a
Red Hat-based distribution, you can install it like this:
$ sudo yum install bashdb
If you are using a Debian-based distribution, you can install it like this:
$ sudo apt-get install bashdb
The shell translates your commands and sends them to the system. Most Linux
distributions are shipped with many shells.
[7]
The What and Why of Scripting with Bash Chapter 1
Every shell has its own features, and some of them are very popular among developers
today. These are some of the popular ones:
Sh shell: This is called the Bourne shell, this was developed at AT&T labs in the
70s by a guy named Stephen Bourne. This shell offers many features.
Bash shell: Also called the Bourne again shell, this is very popular and
compatible with sh shell scripts, so you can run your sh scripts without changing
them. We are going to use this shell in this book.
Ksh shell: Also called the Korn shell, this is compatible with sh and bash. Ksh
offers some enhancements over the Bourne shell.
Csh and tcsh: Linux was built using the C language and that drove developers at
Berkeley University to develop a C-style shell in which the syntax is similar to the
C language. Tcsh adds some minor enhancements to csh.
Now we know the types of shells and we know that we are going to use bash, so what is
bash scripting?
As you might know, you can run multiple commands from the shell by separating them
with semi colons ():
ls ; pwd
The first command runs, followed by the result of the second command.
Every keyword you type in bash scripting is actually a Linux binary (program), even the JG
statement, or FMTF or XIJMF loops. All are Linux executables.
You can say that the shell is the glue that binds these commands together.
[8]
The What and Why of Scripting with Bash Chapter 1
Command type
For example, if we type and enter MT to list files, it is reasonable to think that we were
running the command. It is possible, but we often will be running an alias. Aliases exist in
memory as a shortcut to commands or commands with options; these aliases are used
before we even check for the file. Bash's built-in UZQF command can come to our aid here.
The UZQF command will display the type of command for a given word entered at the
command line. The types of command are listed as follows:
Alias
Function
Shell built-in
Keyword
File
This list is also representative of the order in which they are searched. As we can see, it is
not until the very end where we search for the executable file MT.
We can extend this further to display all the matches for the given command:
$ type -a ls
ls is aliased to 'ls --color=auto'
ls is /bin/ls
[9]
The What and Why of Scripting with Bash Chapter 1
If we need to just type in the output, we can use the U option. This is useful when we need
to test the command type from within a script and only need the type to be returned. This
excludes any superfluous information, and thus makes it easier for us humans to read.
Consider the following command and output:
$ type -t ls
alias
The output is clear and simple, and is just what a computer or script requires.
The built-in UZQF can also be used to identify shell keywords such as JG, and DBTF. The
following command shows UZQF being used against multiple arguments and types:
$ type ls quote pwd do id
You can also see that the function definition is printed when we stumble across a function
when using UZQF.
Command PATH
Linux will check for executables in the 1"5) environment only when the full or relative
path to the program is supplied. In general, the current directory is not searched unless it is
in the 1"5). It is possible to include our current directory within the 1"5) by adding the
directory to the 1"5) variable. This is shown in the following command example:
$ export PATH=$PATH:.
[ 10 ]
The What and Why of Scripting with Bash Chapter 1
This appends the current directory to the value of the 1"5) variable; each item in the 1"5)
is separated using a colon. Now your 1"5) has been updated to include the current
working directory and, each time you change directories, the scripts can be executed easily.
In general, organizing scripts into a structured directory hierarchy is probably a great idea.
Consider creating a subdirectory called CJO within your home directory and add the scripts
into that folder. Adding )0.&CJO to your 1"5) variable will enable you to find the
scripts by name and without the file path.
The following command-line list will only create the directory, if it does not already exist:
$ test -d $HOME/bin || mkdir $HOME/bin
Although the preceding command-line list is not strictly necessary, it does show that
scripting in bash is not limited to the actual script, and we can use conditional statements
and other syntax directly at the command line. From our viewpoint, we know that the
preceding command will work whether you have the CJO directory or not. The use of the
)0.& variable ensures that the command will work without considering your current
filesystem context.
As we work through the book, we will add scripts into the )0.&CJO directory so that
they can be executed regardless of our working directory.
Also, we will use Visual Studio Code as a modern GUI editor to edit and debug our scripts.
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. Gedit and other GUI editors,
and their menus, will provide similar functionality.
[ 11 ]
The What and Why of Scripting with Bash Chapter 1
Configuring vim
Editing the command line is often a must and is part of a developer's everyday life. Setting
up common options that make life easier in the editor give us the reliability and consistency
we need, a little like scripting itself. We will set some useful options in the vi or vim editor
file, )0.&WJNSD.
When these options are set, the )0.&WJNSD file should look similar to this:
TFUTIPXNPEF
TFUOPIMTFBSDI
TFUBVUPJOEFOU
TFUUBCTUPQ
TFUFYQBOEUBC
TZOUBYPO
Configuring nano
The nano text editor 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 )0.&OBOPSD file. Your
edited file should look something like the following:
TFUBVUPJOEFOU
TFUUBCTJ[F
JODMVEFVTSTIBSFOBOPTIOBOPSD
[ 12 ]
The What and Why of Scripting with Bash Chapter 1
Configuring gedit
Graphical editors, such as gedit, can be configured using the preferences menu, and are
pretty straightforward.
Enabling tab spacing to be set to 4 spaces and expanding tabs to spaces can be done using
the Preferences | Editor tab, as shown in the following screenshot:
You can download the example code files from your account at
IUUQXXXQBDLUQVCDPN for all the Packt Publishing books you have
purchased. If you purchased this book elsewhere, you can visit
IUUQXXXQBDLUQVCDPNTVQQPSU and register to have the files e-mailed
directly to you.
[ 13 ]
The What and Why of Scripting with Bash Chapter 1
Another very useful feature is found on the Preferences | Plugins tab. Here, we can enable
the Snippets plugin, which can be used to insert code samples. This is shown in the
following screenshot:
For the rest of the book, we will be working on the command line 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 good habits early on.
[ 14 ]
The What and Why of Scripting with Bash Chapter 1
Hello World!
As you know, it is almost obligatory to begin with a )FMMP8PSME script and we will not
disappoint as far as this is concerned. We will begin by creating a new
script, )0.&CJOIFMMPTI. The contents of the file should read as in the following
screenshot:
We hope that you haven't struggled with this too much; it is just three lines, after all. We
encourage you to run through the examples as you read to really help you instill the
information with good hands-on practice.
CJOCBTI: Normally, this is always the first line of the script and is known as
the shebang. The shebang starts with a comment, but the system still uses this
line. A comment in a shell script has the symbol. The shebang instructs the
interpreter of the system to execute the script. We use bash for shell scripts, and
we may use PHP or Perl for other scripts, as required. If we do not add this line,
then the commands will be run within the current shell; it may cause issues if we
run another shell.
FDIP)FMMP8PSME: The FDIP command will be picked up in a built-in shell
and can be used to write a standard output, 45%065; this defaults to the screen.
The information to print is enclosed in double quotes; there will be more on
quotes later.
FYJU: The FYJU command is a built-in shell, and is used to leave or exit the
script. The FYJU code is supplied as an integer argument. A value of anything
other than will indicate some type of error in the script's execution.
[ 15 ]
The What and Why of Scripting with Bash Chapter 1
We should be rewarded with the )FMMP8PSME text being displayed on our screens. This is
not a long-term solution, as we need to have the script in the )0.&CJO directory,
specifically, to make running the script easy from any location without typing the full path.
We need to add in the execute permissions as shown in the following code:
$ chmod +x $HOME/bin/hello1.sh
We should now be able to run the script simply, as shown in the following screenshot:
In the preceding example, DPNNBOE is executed only if DPNNBOE fails in some way. To be
specific, DPNNBOE will run if DPNNBOE exits with a status code other than .
Similarly, in the following extract, we will only execute DPNNBOE if DPNNBOE succeeds
and issues an exit code of :
$ command1 && command2
To read the exit code from our script explicitly, we can view the variable, as shown in the
following example:
$ hello1.sh
$ echo $?
The expected output is , as this is what we have added to the last line of the file and there
is precious little else that can go wrong to cause the failure to reach that line.
[ 16 ]
The What and Why of Scripting with Bash Chapter 1
Adding the TI suffix to the file does not guarantee the name will be unique, but, in Linux,
where we do not use file extensions, the suffix is part of the filename. This helps you to
provide a unique identity to your script. Additionally, the suffix is used by the editor to
help you identify the file for syntax highlighting. If you recall, we specifically added the
syntax highlighting file TIOBOPSD to the nano text editor. Each of these files is specific to a
suffix and subsequent language.
Referring back to the command hierarchy within this chapter, we can use a UZQF to
determine the location and type of file IFMMPTI:
$ type hello1.sh #To determine the type and path
$ type -a hello1.sh #To print all commands found if the name is NOT unique
$ type -t hello1.sh ~To print the simple type of the command
Hello Dolly!
It is possible that we might need a little more substance in the script than a simple fixed
message. Static message content does have its place, but we can make this script much more
useful by building in some flexibility.
In this chapter, we will look at the positional parameters or arguments that we can supply
to the script and in the next chapter, we will see how we can make the script interactive and
also prompt the user for input at runtime.
[ 17 ]
The What and Why of Scripting with Bash Chapter 1
The script will still run and will not produce an error. The output will not change either and
will print )FMMP8PSME:
Argument
Description
Identifier
The name of the script itself, which is often used in usage statements.
A positional argument, which is the first argument passed to the script.
Where two or more digits are needed to represent the argument
\^ position. Brace brackets are used to delimit the variable name from any
other content. Single value digits are expected.
The argument count is especially useful when we need to set the amount
of arguments needed for correct script execution.
Refers to all arguments.
For the script to make use of the argument, we can change its content a little. Let's first copy
the script, add in the execute permissions, and then edit the new IFMMPTI:
$ cp $HOME/bin/hello1.sh $HOME/bin/hello2.sh
$ chmod +x $HOME/bin/hello2.sh
We need to edit the IFMMPTI file to make use of the argument as it is passed at the
command line. The following screenshot shows the simplest use of command-line
arguments, now allowing us to have a custom message:
[ 18 ]
The What and Why of Scripting with Bash Chapter 1
Run the script now; we can provide an argument as shown in the following:
$ hello2.sh fred
The output should now say )FMMPGSFE. If we do not provide an argument, then the
variable will be empty and will just print )FMMP. You can refer to the following screenshot
to see the execution argument and output:
If we adjust the script to use , all the arguments will print. We will see )FMMP and then a
list of all the supplied arguments. Edit the script and replace the FDIP line as follows:
echo "Hello $*"
And this will result in the output shown in the following screenshot:
If we want to print )FMMPOBNF , with each name on a separate line, we will need to wait
a little until we cover looping structures. A GPS loop is a good way to achieve this.
In the first script, it does not matter if we use single or double quotes. FDIP)FMMP
8PSME will be exactly the same as FDIP )FMMP8PSME .
[ 19 ]
The What and Why of Scripting with Bash Chapter 1
However, this is not the case in the second script, so it is very important to understand the
quoting mechanisms available in bash.
The idea of the quotes is to protect special characters, such as a space between the two
words; both quotes protect the space from being misinterpreted. The space is normally read
as a default field, separated by the shell. In other words, all characters are read by the shell
as literals with no special meaning. This has the knock-on effect of the symbol printing its
literal format rather than allowing bash to expand its value. The bash shell is prevented
from expanding the variable's value as it is protected by the single quotes.
This is where the double quote comes to our rescue. The double quote will protect all the
characters except the , allowing bash to expand the stored value.
If we ever need to use a literal within the quoted string, along with variables that need to
be expanded, we can use double quotes, but escape the desired with the backslash (=). For
example, FDIP64&3FBSOT= would print as 'SFEFBSOT if the current user
were Fred.
Try the following examples at the command line using all quoting mechanisms. Feel free to
up your hourly rate as required:
$ echo "$USER earns $4"
$ echo '$USER earns $4'
$ echo "$USER earns \$4"
[ 20 ]
The What and Why of Scripting with Bash Chapter 1
Edit your script so that it reads like the following complete code block for
)0.&CJOIFMMPTI:
CJOCBTI
FDIP:PVBSFVTJOH
FDIP)FMMP
FYJU
If we prefer not to print the path and only want the name of the script to show, we can use
the CBTFOBNF command, which extracts the name from the path. Adjust the script so that
the second line now reads as follows:
echo "You are using $(basename $0)"
The syntax is used to evaluate the output of the inner command. We first run
CBTFOBNF and feed the result into an unnamed variable represented by the .
It is possible to achieve the same results using back quotes; this is less easy to read, but we
have mentioned this as you might need to understand and modify the scripts that have
been written by others. The alternative to the syntax is shown in the following
example:
echo "You are using 'basename $0'"
[ 21 ]
The What and Why of Scripting with Bash Chapter 1
Please note that the characters used are back quotes and NOT single quotes. On UK and US
keyboards, these are found in the top-left corner next to the number 1 key.
Declaring variables
Just like in any programming language, you can declare variables in bash scripts. So, what
are these variables and what are the benefits of using them?
Well, a variable is like a placeholder where you store some value for later use in your code.
There are two kinds of variables you can declare in your script:
User-defined variables
Environment variables
User-defined variables
To declare a variable, just type the name you want and set its value using the equals sign
().
As you can see, to print the variable's value, you should use the dollar sign () before it.
Note that there are no spaces between the variable name and the equals sign, or between
the equals sign and the value.
If you forget and type a space in between, the shell will treat the variable as if it were a
command, and, since there is no such command, it will show an error.
[ 22 ]
The What and Why of Scripting with Bash Chapter 1
Another useful type of user-defined variable is the array. An array can hold multiple
values. So, if you have tens of values you want to use, you should use arrays instead of
filling your script with variables.
To declare an array, just enclose its elements between brackets, like this:
CJOCBTI
NZBSS POFUXPUISFFGPVSGJWF
To access a specific array element, you can specify its index like this:
CJOCBTI
NZBSS POFUXPUISFFGPVSGJWF
FDIP\NZBSS<>^QSJOUTUXPXIJDIJTUIFTFDPOEFMFNFOU
To print the array elements, you can use an asterisk, like this:
CJOCBTI
NZBSS POFUXPUISFFGPVSGJWF
FDIP\NZBSS< >^
To remove a specific element from the array, you can use the VOTFU command:
CJOCBTI
NZBSS POFUXPUISFFGPVSGJWF
VOTFUNZBSS<>5IJTXJMMSFNPWFUIFTFDPOEFMFNFOU
VOTFUNZBSS5IJTXJMMSFNPWFBMMFMFNFOUT
Environment variables
So far, we have used variables that we didn't define, such as #"4)@7&34*0/, )0.&,
1"5), and 64&3. You might wonder, as we didn't declare these variables, where did they
come from?
[ 23 ]
The What and Why of Scripting with Bash Chapter 1
These variables are defined by the shell for your use and they are called environment
variables.
There are many environment variables. If you want to list them, you can use the QSJOUFOW
command.
Also, you can print a specific environment variable by specifying it to the QSJOUFOW
command:
$ printenv HOME
Note that all environment variables are written in capital letters, so you can declare your
variables as lower case to make it easy to differentiate your variables from environment
variables. This is not required, but is preferable.
Variable scope
Once you have declared your variable, it will be available for use in your entire bash script
without any problems.
Let's assume this scenario: you have divided your code into two files and you will execute
one of them from inside the other, like this:
5IFGJSTUTDSJQU
CJOCBTI
OBNF.PLIUBS
TDSJQUTI5IJTXJMMSVOUIFTFDPOETDSJQU
Suppose that you want to use the OBNF variable in the second script. If you try to print it,
nothing will show up; this is because a variable's scope is only limited to the process that
creates it.
To use the OBNF variable, you can export it using the FYQPSU command.
[ 24 ]
The What and Why of Scripting with Bash Chapter 1
Now if you run the first script, it will print the name that came from the first script file.
Keep in mind that the second process or TDSJQUTI only makes a copy of the variable and
never touches the original one.
To prove this, try to change that variable from the second script and try to access that
variable value from the first script:
5IFGJSTUTDSJQU
CJOCBTI
OBNF.PLIUBS
FYQPSUOBNF
TDSJQUTI
FDIPOBNF
If you run the first script, it will print the modified OBNF from the second script and then it
will print the original OBNF from the first script. So, the original variable remains as it is.
Command substitution
So far, we have seen how to declare variables. These variables can hold integers, strings,
arrays, or floats, as we have seen, but this is not everything.
[ 25 ]
The What and Why of Scripting with Bash Chapter 1
As you might know, the QXE command prints the current working directory. So, we will see
how to store its value in a variable.
Using the first method, we just surround the command between two backticks:
CJOCBTI
DVS@EJS QXE
FDIPDVS@EJS
The output coming from commands can be further processed and actions can be made
based on that output.
If we want to look at the verbose output from our script and the detailed information about
the way the script is evaluated line by line, we can use the W option. This can be within the
shebang, but it is often easier to run the script directly with bash:
$ bash -v $HOME/bin/hello2.sh fred
[ 26 ]
The What and Why of Scripting with Bash Chapter 1
This is especially useful in this example as we can see how each element of the embedded
CBTFOBNF command is processed. The first step is removing the quotes and then the
parentheses. Take a look at the following output:
The Y option, which displays the commands as they are executed, is more commonly used.
It's useful to know the decision branch that has been chosen by the script. The following
shows this in action:
$ bash -x $HOME/bin/hello2.sh fred
We again see that the CBTFOBNF is evaluated first, but we do not see the more detailed steps
involved in running that command. The screenshot that follows captures the command and
output:
[ 27 ]
The What and Why of Scripting with Bash Chapter 1
The previous method might be hard for beginners or people who have a programming
background in which they debugged their code visually.
Another modern way of debugging shell scripts is by using Visual Studio Code.
There is a plugin called bash debug that enables you to debug bash scripts visually, as is
the case for any other programming language.
You can step into, step over, add watches, and do all the other usual debugging stuff you
know.
After installing the plugin, from the File menu, open your TIFMMTDSJQUT folder. Then
you can configure the debugging process by pressing Ctrl + Shift + P and typing the
following:
%FCVHPQFOMBVODIKTPO
[ 28 ]
The What and Why of Scripting with Bash Chapter 1
[ 29 ]
The What and Why of Scripting with Bash Chapter 1
Now insert a breakpoint and press F5, or start debugging from the Debug menu; it will
show you the list of TI files:
[ 30 ]
The What and Why of Scripting with Bash Chapter 1
Select the one you want to debug, and set a breakpoint on any line to test it, as shown in the
following screenshot:
[ 31 ]
The What and Why of Scripting with Bash Chapter 1
You can add watches to watch variables' values while stepping over your lines of code:
Note that your script MUST start with the bash shebang, CJOCBTI.
Now you can enjoy the visual method of debugging. Happy coding!
[ 32 ]
The What and Why of Scripting with Bash Chapter 1
Summary
This marks the end of this chapter and you have no doubt found this useful. Especially for
those making a start with bash scripting, this chapter will have established a firm
foundation on which you can build your knowledge.
We began by ensuring that bash is secure and not susceptible to shell-shock from embedded
functions. With bash secured, we considered the execution hierarchy where aliases,
functions, and so on are checked before the command; knowing this can help us plan a
good naming structure and a path to locate the scripts.
Then we went on to illustrate the types of Linux shells and we found out what bash
scripting is.
Soon, we were writing simple scripts with static content, but we saw how easy it was to add
flexibility using arguments. The exit code from the script can be read with the variable
and we can create a command-line list using ]] and , which depends on the success or
failure of the preceding command in the list.
Then we saw how to declare variables and how to use environment variables. We identified
the variables' scope and saw how to export them to another process.
Also, we saw how to store commands' output in variables, which is called command
substitution.
Finally, we closed the chapter by looking at debugging the script using bash options and VS
Code. It's not really required when the script is trivial, but it will be useful later when
complexity is added.
In the next chapter, we will create interactive scripts that read the user's input during script
execution.
[ 33 ]
The What and Why of Scripting with Bash Chapter 1
Questions
1. What is the problem with the following code? And how do we fix it?
CJOCBTI
WBS8FMDPNFUPCBTITDSJQUJOH
FDIPWBS
3. What is the problem with the following code? And how do we fix it?
CJOCBTI
GJMFT MTMB
FDIPGJMFT
Further reading
Please see the following for further reading relating to this chapter:
IUUQUMEQPSH)0850#BTI1SPH*OUSP)0850IUNM
IUUQUMEQPSH-%1BCTIUNMWBSBTTJHONFOUIUNM
IUUQUMEQPSH-%1BCTIUNMEFDMBSFSFGIUNM
[ 34 ]
2
Creating Interactive Scripts
In $IBQUFS, The What and Why of Scripting with Bash, we learned how to create a script and
use some of its basic elements. These included optional parameters that we can pass
through to the script when it is executed. In this chapter, we will extend this by using
the shell's built-in SFBE command to allow for interactive scripts. Interactive scripts are
scripts that prompt for information during the script's execution.
Technical requirements
The source code for this chapter can be downloaded from here:
IUUQTHJUIVCDPN1BDLU1VCMJTIJOH.BTUFSJOH-JOVY4IFMM4DSJQUJOH4FDPOE
&EJUJPOUSFFNBTUFS$IBQUFS
Creating Interactive Scripts Chapter 2
To gain help on the built-in command, we can use man bash and search for FDIP; however,
the FDIP command is identical to the internal command, so we recommend that you use
man echo in most cases in order to display the command options.
The basic use of FDIP that we have seen so far will produce a text output and a new line.
This is often the desired response, so we don't need to be concerned that the next prompt
will append to the end of the echoed text. The new line separates the script output from the
next shell prompt. If we do not supply any text string to print, FDIP will print only the new
line to 45%065. We can test this with the following command, directly from the command
line. We do not need to run FDIP or, in fact, any other command from a script. To run FDIP
from the command line, we simply enter the command as shown:
$ echo
The output will show a clear new line between the command we issued and the subsequent
prompt. We can see this in the following screenshot:
If we want to suppress the new line, which is especially useful if we are to prompt users, we
can do this in the following two ways, with the help of FDIP:
$ echo -n "Which directory do you want to use? "
$ echo -e "Which directory do you want to use? \c"
The result will be to suppress the line feed. In the initial example, the O option is used to
suppress the line feed. The second example uses the more generic F option, which allows
escape sequences to be added to the text string. To continue on the same line, we use =D as
the escape sequence.
[ 36 ]
Creating Interactive Scripts Chapter 2
This does not look great as the final part of the script or when it is run from the command
line, as the command prompt will follow. This is illustrated in the following screenshot:
As we execute the script, we will be greeted and prompted with whatever is typed. This is
echoed using the 3&1-: variable in the echo statement. As we have not yet supplied a
variable name to the read built-in command, the default 3&1-: variable is used. The script
execution and output are shown in the following screenshot. Take some time to practice the
script on your own system.
[ 37 ]
Other documents randomly have
different content
The Project Gutenberg eBook of The Pillars of
the House; Or, Under Wode, Under Rode, Vol.
2 (of 2)
This ebook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this ebook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.
Title: The Pillars of the House; Or, Under Wode, Under Rode,
Vol. 2 (of 2)
Language: English
BY
CHARLOTTE M. YONGE,
AUTHOR OF 'THE HEIR OF REDCLYFFE,' ETC.
IN TWO VOLUMES.
VOL II.
London:
1875.
CHAPTER XXIV.
FAMILY GHOOLS.
ebookbell.com