Introduction to Bash shell scripting

In this section, we will introduce you to the core concept of Bash shell scripting. Another very important feature of Bash shell scripts are functions. We use functions excessively in Bash shell scripts to make reoccurring tasks or commands reusable. Functions encapsulate a task to make it more modular. Functions usually take in data, process it, and return a result. Once a function is written, it can be used over and over again, but we can also work with functions on the command line.

Let's discuss the general syntax of a function by creating one:

$ say_hello90 {
>echo "My name is $1";
>}  

The first word is the function name followed by opening and closing brackets, which are used to define a function, followed by a curly opening bracket; all the commands belonging to a function are defined within the open and closing brackets, which is also called the function body. Functions can have arguments as normal commands, which will be accessible to the function body from outside. To access a certain argument in the function, use the dollar number notation. So $1 is the first argument, $2 would be the second, and so on. Let's take a look at our say_hello function. If we call this function with one argument, the function will be executed with one argument, and this argument will be taken in the function body, where we can access the first argument with the $1 variable, which is nothing more than a normal shell expansion.

Functions can also call other functions in their body. Now, let's learn to put your shell commands in a shell script file. Script files are just plain text files which contain different Linux commands, control structures, loops, and so on. Usually, they are written to solve everyday computer problems and fit your own individual needs instead of having to execute single commands one by one manually. There are two ways to execute a text file as a shell script. The first way is to use it as an argument for the Bash command. Another way to execute it without using it as an argument for the Bash command is to first make the script executable and then put the so-called shebang line at the first line, which tells the command line that this file is a Bash script and should be started with the Bash interpreter. In our example, #!/bin/bash is the shebang line and tells Bash that this is a Bash shell script. Now, to start it with the shebang approach, make it executable and then you can just run it on the command line, as following:

$ vi /tmp/new-script.sh
$ chmod +x /tmp/new-script.sh
/tmp/new-script.sh   

Similar to using functions, we can also access command-line arguments in shell scripts, such as $ vi /tmp/new-script.sh. The first argument can be accessed using $1, the second argument $2, and so on. In shell scripts, you can also access the name of the shell script using the $0. The total number of arguments can be accessed using the $#. So, for example, to write a check that your script needs at least two arguments do the following:

#!/bin/bash
echo "Hello World"
echo "..........."
if [[ $# -lt 2 ]]
then
echo "Usage $0 param1 param2"
echo $1
echo $2
echo $0
echo $#  

So, what this script does is check whether the number of command-line arguments are at least two and, if this is not the case, then a usage format will be printed out stating that you need two parameters, press Enter, and then an exit value of 1 will be returned, which means that this script has thrown an error, because, as we already know, a script will return 0 on successful execution. Let's test this script out:

If we start the script with only one argument, it will print out the usage format. However, if we start it with two arguments, it will correctly work. When it comes to shell scripting, there is much more to learn and we could only show you the very basics to get you started. You can refer to the Bash manual or just start reading the various shell scripts that are shipped with your Cent0S 7 OS for free. Type the following command to get a list of all the .sh files: su -c 'find / -name "*.sh"', which is the default extension for shell script files in your system. Just start by opening one of the available shell script files in your system and try to understand it, for example, /usr/libexec/grepconf.sh.