Hello and welcome back! We’ll start 1
a new and important section on Bash Shell Scripting. 2
This is programming 3
and if you are a complete beginner, it could prove more difficult for you, but not impossible. 4
Give yourself time to deeply understand it and practice as much as possible. 5
Keep in mind that the only way to learn coding is to write code; and when you feel overwhelmed and 6
get stuck, think that no one became an expert overnight. 7
OK, let's get started! 8
As we've already seen at the beginning of the course, a shell is a program that takes commands from 9
the user, through the keyboard, and gives them to the operating system's kernel to get executed. 10
It's also called the command interpreter because it accepts commands from the user and then checks or 11
interprets them to see if they are syntactically correct. 12
And if they are, the shell will send them to the kernel for execution. 13
The shell gets started when the user logs in or starts the terminal. 14
To see what shell has started run in the terminal: echo $0 15
And it's Bash. You'll probably see that Bash is the name of your shell, because this is the most used 16
one on Linux; but you should know that there are also other shells available, like the original Bourne 17
Shell, Dash, C shell or Z shell. 18
You can get a list of all installed shells by displaying the contents of the /etc/ shells. 19
Each user can have another default shell and you can see the default shell of a user in the password 20
file on the last column. 21
This is, for example, the default shell of the student user. 22
OK, let's move on! What is a Shell script and why do we need them? 23
A Shell script is basically an executable text file that contains Shell commands and other specific 24
structures and components like variable, functions, loops and so on that are executed sequentially. 25
That's all! Shell scripting is a very powerful Linux administration tool. 26
Whenever you find yourself doing the same task over and over again, you should use shell scripting. 27
This is known as repetitive task automation. 28
Using shell scripting, you can create your own power tools, customized administrative tasks, or create 29
simple applications. 30
And since the scripts you develop are well tested, the chances of errors are highly reduced. 31
Another advantage of using Shell scripts is that the one that runs them doesn't have to know 32
all the details of Linux and programming. 33
Just imagine that you develop a shell script that makes a backup of an SQL database and the junior sysadmin 34
will just run the script without knowing all the details about how to connect and authenticate to the 35
actual server, what SQL commands to execute and so on. 36
He simply runs the script as any other Linux command and the backup is done. 37
Some practical examples of common tasks, where Shell scripts are used, are monitoring the system, data 38
backup and restoring, creating an email-based alert system 39
when something happens, user administration, security, auditing and many more. 40
Just to make it clear, let's create first a very simple shell script that creates a new directory in the 41
current one and a file inside it and then lists the contents of both the directory and the file. 42
I'm creating a new directory called Scripts where I put all the scripts I create in this section and 43
I'm moving to that director. It's empty! Note that is also a common practice to use a name like bin in 44
the user's home directory for the scripts someone creates. 45
So I'm creating the script as any other text file using my preferred editor, which is vim, so vim 46
first_script.sh 47
By the way, it's common for Shell scripts to have the sh extension; this is only a hint to humans 48
that you are dealing with a Shell script and has absolutely no effect on how the system handles the 49
file. 50
Remember that traditionally Linux does not care about file extensions. 51
Note that some people consider that giving to the executable files an extension is bad practice and 52
they should be kept without any extension like all other Linux commands or official scripts that come 53
with the distribution. 54
So it's up to you if you use the sh extension or not. 55
And I'm writing the commands as I would normally do in the terminal, so mkdir -p dir1 wecho , a string, 56
some text and the right direction to create the file. 57
so dir1/file.txt 58
And I'm listing the contents of both the directory and the file. 59
So ls -l dir1 and cat dir1/file.txt 60
That's all! I'm saving the file and exiting. 61
Normally, a Shell script is an executable file, so I'm adding the execution permission for the owner, 62
chmod 700 and the name of the file, of the script, and I'm executing it using a relative 63
path; it's the current directory so . / and the name of the script. 64
This is the contents of the directory and this is the contents of the file; and both the directory and 65
the file were created. 66
Now, one remark! If you simply try to run the script by its name, you'll get a command not found 67
error like this first_script.sh 68
command not found! That's because the shell is not looking for the script in the current directory, 69
it's looking for commands and scripts only in the directories from the path variable. 70
Let's see the contents of the variable echo$ PATH written in capitals. 71
These are the directories where the shell is looking for executables. 72
So, add the directory that contains the script to the path or execute the script using ./ 73
if you are in the same directoy or its absolute path like this home/ student /scripts/ first_script.sh 74
That's enough for the moment; you now know what a shell script is, why we need them, and how to create 75
and run a basic script. 76
See you in a few seconds!