In this post, I will help to introduce you the basics of shell scripting files along with various advantages they come with. I recommend you to thoroughly read this post before skipping to the other ones. Also, it is recommended to experiment the given code samples in your Linux machine. There is no better way to learn Linux more than doing yourselves in a Linux system. Do a simple research about different Linux distros and settle with the one that you feel most comfortable with. From a simple Arch Linux system to a complex and visually stunning Debian based Linux distro you have everything to chose from.
WHAT IS SHELL?
Shell is a very easy and powerful programming language. Many Linux system maintainers often use Shell scripts in their work, but not everyone is good at writing Shell scripts. Once you master the rules and skills of writing Shell scripts, your work will be easier and more efficient in the future!
WRITING FORMAT OF SCRIPT FILE
Before talking about the importance of Shell scripting and how they work, it is better if you have a good grasp of the format of a Shell scripting file. This is why we are introducing it in this chapter with detailed examples.
What is a Shell script file?
To briefly explain in layman terms, the commands of Linux or UNIX-like system are written into a file, which is popularly known as a Shell script file. It is written in a way that the Shell script file we write must run in Linux or UNIX-like operating systems. There are also a ton of tweaks to run shell files in windows or macOS operating systems.
Note: Before starting the fundamentals, we remind you that the operating system platform used in this book is Linux Mint. Linux mint is not only a Debian based light-weight operating system but also can perform tons of complex operations.
After giving the script file the necessary execution authority ( most commonly called as root privileges) and running the script file, the computer will execute the commands in the script file content from top to bottom. While running, it can look in other shell files or programs according to the instructions. Shell files are an easy and recommended way to automate operations that can define your work.
Compared with manually executing system commands on the command line, the advantage of script file is that once written, all commands in script file can be automatically completed later (with higher efficiency).Moreover, the same script file can be called and executed repeatedly, avoiding unnecessary manual and repeated input of commands.
As understood by the previous section, script is a file. So what tools do we use to create this file?
It needs to be understood by a Linux enthusiast that a script file is just an ordinary text file. For this reason you can create a script file by using any text editor software such as vim, gedit, Emacs, Notepad\++, Sublime, Atom and other tools. You can do a simple research to find out the text editors for your use case.
We use VIM editor in the cases in the following chapters.It is recommended to use .sh as the file extension when creating a new file, so that people can see that the file is a Shell script file at an instance. If you are worried you can first just create the program code in a .txt extension and afterwards can export into a .sh extension simply by renaming the file. Until you change the file extension and provide execution permissions the script file cannot be run.
Here is the command:
root@server : vim sample.sh
// This will create a shell file
WHAT ARE THE WRITING FORMAT REQUIREMENTS FOR SCRIPT FILES?
First, the first line of the script file requires shebang(#!) symbol which specifies an interpreter for a script, such as #!/bin/bash, #/bin/sh, #!/usr/bin/env python, etc.
This line is annotated by #, so it will not be executed as a command, but the computer knows what interpreter should be used to interpret all valid codes in the whole script file through this annotation information (the interpreter used in this case is /bin/bash).
Secondly, the script file uses # or < < symbol to realize single-line or multi-line annotation. It needs to be understood that the annotated keywords or codes will not be executed, and the annotation is mainly for people to see!
By reading the notes, we can quickly understand the function, version and author contact information of the script file. The core function is to explain the function of the script file or code block. Finally, the most important content is the code part.
Generally, a line of code is a command, and all valid code commands in the script file are executed from top to bottom.
Let’s write the first script file and look at the composition of the script file.
#!/bin/bash
<< COMMENT
This is a comment
( Mostly multi-line)
Written for Linux for Hackers
COMMENT
# Now we will show an echo command
echo “ This is an example program”
Note that the keyword behind the < < symbol can be any string, but the same keyword must be used when ending the comment.If you start commenting from < < ABC, you must also use ABC (letters are case sensitive) when you finish commenting information.
In the next section of this chapter, we will have a brief discussion about the various execution modes of shell or for that matter any script files.
VARIOUS EXECUTION MODES OF SCRIPT FILE
After the script file is written, the next step is to execute it.There are many ways to execute script files, including those that require execution permission, those that do not, those that open subprocesses, and those that do not. We will explain each of these instances with examples.
1) If the script file itself has no executable permission
In this case , the default script cannot be executed directly, but an interpreter like bash or sh can use the script file as a parameter (read the contents of the script file) to execute the script file.
Here are the commands:
root@server : ./sample.sh
// Will display errors because there is no permission
root@server : bash sample.sh
root@server : sh sample.sh
// Other main ways to execute but without errors
From the output information of the above three commands, we can see that when the sample.sh script file under./(current directory) is executed without execution permission, an error message will appear, while when bash and sh are used to execute the sample.sh script as parameters, the correct message “This is a sample” will be displayed as output.
2) If the script file has an executable permission
The execution permission can be assigned to the script file through the chmod command. Once the script file has the execution permission, it can be executed using absolute path or relative path.
The following example assumes that a script file has the absolute path of /root/sample.sh, and the effect of executing the script file is as follows.
Here are the commands :
root@server : chmod +x sample.sh
// This will give executable permissions
root@server : ./sample.sh
// Will display output without errors
3) How to open a sub-process for execution
Regarding to make a descision whether to open or not a sub-process we must first understand what a sub-process is. Generally, we can view the process tree through the pstree command to understand the relationship between processes.
Here is the command:
root@sample : pstree
From the above command output, we can see that the first process started by the computer is systemd, and then N subprocesses are started under this process, such as NetworkManager, atd, chronyd and sshd, all of which are systemd subprocesses.Under the sshd process, there are two sub-processes of sshd. Under the two sub-processes of sshd, the bash interpreter sub-process is started, and a pstree command is executed under one of the bash processes.
Your system may have different other sub processes. All you need to do is verify using the pstree command. As we said just now, whether the script is executed directly or by using an interpreter such as bash or sh, the subprocesses will be started.
The following example demonstrates the effect through a script file.
First, open a command terminal, write a script file in the command terminal, and execute the script file.Then, open a command terminal, and observe the process tree through pstree command in this terminal.
Here are commands:
// sleep.sh
#!/bin/bash
Sleep 5000
root@server :chmod +x sleep.sh
rooot@server : ./sleep.sh
root@server : pstree
It can be seen from the output that a subprocess script file is opened under the bash terminal, and a sleep command is executed through the script file.Back to the first terminal, use Ctrl+C to terminate the script file executed before, and use bash command to execute the script again.
root@server : bash sleep.sh
At last, use the pstree command on the second terminal to observe the experimental results.The result is similar, a bash subprocess is opened under the bash process, and a sleep command is executed under the bash subprocess.
4) Execution mode without opening subprocesses
Next, let’s take a look at the case of execution mode without opening subprocesses.
Similar to the previous experiment, we need to open two command terminals.First, open the first terminal, and this time use the source or .(dot) command to execute the script file.
Or then, we can open the second terminal and observe the results through the pstree command.It can be seen from the experimental results that the sleep command in the script file is directly executed under the bash terminal.Finally, we can write a special script file with the following contents.For this script file, use open subprocess and do not open subprocess respectively.
Here are the command:
root@server : . sleep.sh
root@server : bash sleep.sh
root@server : source sleep.sh
With this, we have completed a brief introduction about the shell scripting execution capabilities. As linux system deals with lot of users and subgroups it is important to not run a script file that is not supposed to run. So, with these extra abilities to run programs we can now start learning about input and output statements in shell programming.