Book Image

Linux Command Line and Shell Scripting Techniques

By : Vedran Dakic, Jasmin Redzepagic
Book Image

Linux Command Line and Shell Scripting Techniques

By: Vedran Dakic, Jasmin Redzepagic

Overview of this book

Linux Command Line and Shell Scripting Techniques begins by taking you through the basics of the shell and command-line utilities. You’ll start by exploring shell commands for file, directory, service, package, and process management. Next, you’ll learn about networking - network, firewall and DNS client configuration, ssh, scp, rsync, and vsftpd, as well as some network troubleshooting tools. You’ll also focus on using the command line to find and manipulate text content, via commands such as cut, egrep, and sed. As you progress, you'll learn how to use shell scripting. You’ll understand the basics - input and output, along with various programming concepts such as loops, variables, arguments, functions, and arrays. Later, you’ll learn about shell script interaction and troubleshooting, before covering a wide range of examples of complete shell scripts, varying from network and firewall configuration, through to backup and concepts for creating live environments. This includes examples of performing scripted virtual machine installation and administration, LAMP (Linux, Apache, MySQL, PHP) stack provisioning and bulk user creation for testing environments. By the end of this Linux book, you’ll have gained the knowledge and confidence you need to use shell and command-line scripts.
Table of Contents (19 chapters)

Local and global variables

When it comes to declaring any variable in a script—or for that matter, anywhere at all—one crucial attribute for that variable is its scope. By scope, we mean where the variable has the value we declared. Scope is very important since not understanding how it works means that we can get unexpected results in some cases.

Getting ready

Defining a global scope to our variables is something bash does by default, without any interaction with us. All variables that are defined are global variables; their value is the same in the entire script. If we change the variable value by reassigning it (remember that operations on the value do not change the value itself), this value changes globally, and the old value is lost.

There is another thing we can do when declaring variables, and that is to declare them locally. In simple terms, this means that we are explicitly telling bash that we will use this variable in some limited part of the code...