Learn Linux Shell Scripting – Fundamentals of Bash 4.4: A comprehensive guide to automating administrative tasks with the Bash shell [1 ed.] 9781788995597

Create and maintain powerful Bash scripts for automation and administration. Key FeaturesGet up and running with Linux s

1,644 375 6MB

English Pages 454 [440] Year 2018

Report DMCA / Copyright

DOWNLOAD FILE

Learn Linux Shell Scripting – Fundamentals of Bash 4.4: A comprehensive guide to automating administrative tasks with the Bash shell [1 ed.]
 9781788995597

Table of contents :
Cover
Title Page
Copyright and Credits
About Packt
Contributors
Table of Contents
Preface
Chapter 1: Introduction
What is Linux?
What is Bash?
Summary
Chapter 2: Setting Up Your Local Environment
Technical requirements
Choosing between a virtual machine and a physical installation
Setting up VirtualBox
Creating an Ubuntu virtual machine
Creating the virtual machine in VirtualBox
Installing Ubuntu on the virtual machine
Accessing the virtual machine via SSH
Summary
Questions
Further reading
Chapter 3: Choosing the Right Tools
Technical requirements
Using graphical editors for shell scripting
Atom
Atom installation and configuration
Notepad++
Using command-line editors
Vim
Vim summary
.vimrc
Vim cheat sheet
nano
Combining graphical editors with command-line editors when writing shell scripts
Summary
Questions
Further reading
Chapter 4: The Linux Filesystem
Technical requirements
The Linux filesystem explained
What is a filesystem?
What makes the Linux filesystem unique?
Structure of the Linux filesystem
Tree structure
Overview of top-level directories
What about multiple partitions?
/bin/, /sbin/, and /usr/
/etc/
/opt/, /tmp/, and /var/
Everything is a file
Different types of files
Summary
Questions
Further reading
Chapter 5: Understanding the Linux Permissions Scheme
Technical requirements
Read, write, and execute
RWX
Users, groups, and others
Manipulating file permissions and ownership
chmod, umask
sudo, chown, and chgrp
sudo
chown, chgrp
Working with multiple users
Advanced permissions
File attributes
Special file permissions
Access Control Lists (ACLs)
Summary
Questions
Further reading
Chapter 6: File Manipulation
Technical requirements
Common file operations
Copying
Removing
Renaming, moving, and linking
Archiving
Finding files
locate
find
Summary
Questions
Further reading
Chapter 7: Hello World!
Technical requirements
First steps
The shebang
Running scripts
Readability
Comments
Script header
Verbosity
Verbosity in comments
Verbosity of commands
Verbosity of command output
Keep It Simple, Stupid (KISS)
Summary
Questions
Further reading
Chapter 8: Variables and User Input
Technical requirements
What is a variable?
Why do we need variables?
Variables or constants?
Variable naming
Dealing with user input
Basic input
Parameters and arguments
Interactive versus non-interactive scripts
Combining positional arguments and read
Summary
Questions
Further reading
Chapter 9: Error Checking and Handling
Technical requirements
Error checking
Exit status
Functional checks
Test shorthand
Variable refresher
Bash debugging
Error handling
if-then-exit
if-then-else
Shorthand syntax
Error prevention
Checking arguments
Managing absolute and relative paths
Dealing with y/n
Summary
Questions
Further reading
Chapter 10: Regular Expressions
Technical requirements
Introducing regular expressions
What is a regular expression?
grep
Greediness
Character matching
Line anchors
Character classes
Globbing
What is globbing?
Similarities with regular expressions
More globbing
Advanced globbing
Disabling globbing, and other options
Using regular expressions with egrep and sed
Advanced grep
Introducing egrep
sed, the stream editor
Stream editing
In-place editing
Line manipulation
Final remarks
Summary
Questions
Further reading
Chapter 11: Conditional Testing and Scripting Loops
Technical requirements
Advanced if-then-else
A recap on if-then-else 
Using regular expressions in tests
The elif condition
Nesting
Getting help
The while loop
The until loop
Creating an interactive while loop
The for loop
Globbing and the for loop
Loop control
Breaking the loop
The continue keyword
Loop control and nesting
Summary
Questions
Further reading
Chapter 12: Using Pipes and Redirection in Scripts
Technical requirements
Input/output redirection
File descriptors
Redirecting output
stdout
stderr
Redirect all output
Special output redirection
/dev/null
/dev/zero
Input redirection
Generating a password
Advanced redirecting
Redirecting redirections
Command substitution
Process substitution
Pipes
Binding stdout to stdin
Practical examples
Yet another password generator
Setting passwords in a script
tee
Here documents
Heredocs and variables
Using heredocs for script input
Here strings
Summary
Questions
Further reading
Chapter 13: Functions
Technical requirements
Functions explained
Hello world!
More complexity
Variable scopes
Practical examples
Error handling
Augmenting functions with parameters
Colorful
Returning values
Function libraries
Source
More practical examples
Current working directory
Type checking
Yes-no check
Summary
Questions
Further reading
Chapter 14: Scheduling and Logging
Technical requirements
Scheduling with at and cron
at
Time syntax
The at queue
at output
cron
crontab
Syntax for the crontab
Logging script results
Crontab environment variables
PATH
SHELL
MAILTO
Logging with redirection
Final logging considerations
A note on verbosity
Summary
Questions
Further reading
Chapter 15: Parsing Bash Script Arguments with getopts
Technical requirements
Positional parameters versus flags
Using flags on the command line
The getopts shell builtin
The getopts syntax
Multiple flags
Flags with arguments
Combining flags with positional arguments
Summary
Questions
Further reading
Chapter 16: Bash Parameter Substitution and Expansion
Technical requirements
Parameter expansion
Parameter substitutions – recap
Default values
Input checking
Parameter length
Variable manipulation
Pattern substitution
Pattern removal
Case modification
Substring expansion
Summary
Questions
Further reading
Chapter 17: Tips and Tricks with Cheat Sheet
Technical requirements
General tips and tricks
Arrays
The history command
Creating your own aliases
Command-line shortcuts
Fun with exclamation marks
Running commands from the history
Keyboard shortcuts
Copying and pasting from the terminal
Reverse search
Cheat sheet for interactive commands
Navigation
cd
ls
pwd
File manipulation
cat
less
touch
mkdir
cp
rm
mv
ln
head
tail
Permissions and ownership
chmod
umask
chown
chgrp
sudo
su
useradd
groupadd
usermod
Summary
Final words
Assessments
Other Books You May Enjoy
Index

Polecaj historie