What is shell script? Understand and see examples of applications
What is shell script? This may be a fairly common question for those who are starting out on the Linux system.
Linux offers several solutions for the Information Technology area, making life much easier for system administrators.
However, not all IT professionals are aware of all its advantages and the tools that can be incorporated into it.
Among them is the Shell Script, which is very useful in the process of automating tasks. And in this article we are going to explain what it is and practical application examples.
What is a shell script file, after all?
We give the name of shell script to files that will be interpreted by Shell-type programs.
With it, it is possible to create various commands for Linux. In other words, create tasks that will be done from time to time, automating this process, in addition to startup scripts and other functionalities.
Scripts
Whenever an actor goes to make a scene, whether in the cinema, television or theater, he is given a script. A text that contains various information about how it should act during the scene, what are the important points for the narrative to unfold.
Scripting on the computer works in a very similar way. They are a series of instructions that dictate how the device must perform tasks x, always according to the program.
Lines of code, within the programming language, can use a script to, for example, count the number of daily accesses, whether done via desktop or mobile, geolocation, and much more.
Scripts can, of course, go much further than that. With them, it is possible to change the page’s look, colors, layout and even, as mentioned above, add useful functions to the page.
Shell
In general, we can say that Shell is a layer that allows users to access and configure commands in the operating system.
To better understand, imagine a turtle. It has a carapace that surrounds and protects it from the outside world. So her body itself has no major damage.
In a practical way, the Shell works as a kind of intermediate interface between the user and the operating system. It has selective functions and only allows the execution of actions according to the access level of each user.
However, basically, there are two types of Shell: the CLI and the GUI.
CLI
Acronym for “Command Line Interface”, that is, those that have an interface based on a command line. In other words, for any action to be performed, an equivalent command must be typed.
GUI
Acronym for “Graphical User Interface”. In this case, the graphical interface is used so that the same commands do not have to be typed more than once.
In short, whether through a graphical interface or with command lines, the Shell performs basic operating system actions in order to better manage computer data.
This can happen with files that are stored on devices or even when running other applications.
Shell script language structure: main aspects
Now that we know what shell scripting is, let's dive deeper into its language!Like other programming languages, the shell script language has variables in its structure, in addition to decisions and repetition ties. Check out some of them!
Variables
In a basic way, almost all variables in the Shell Script language are of the string type. It is, of course, possible to convert the type of variable depending on the type of operation you want to perform.
There is the possibility to create arrays, perform arithmetic operations, among other functions.
However, in the case of variables that are actually of the string type, it is recommended to use quotes so that there is no risk of the interpreter trying to execute the string as a form of command.
Operators
Mathematical calculations can be performed in Shell Script using several arithmetic operators:
-
- Addition
- — Subtraction
- * Multiplication
- /Division
- % Modulus
- ** Exponentiation
However, doing the operations is not such a simple task. The variables follow the string pattern, which requires some maneuvering to execute the operations.
See below how to perform arithmetic operations.
- variavel=2
- variable2=3
- variable3=“CertificacaLinux”
- Sum=$variavel+$variable2
- echo “Value of variable3 is: $variable3”
- echo “Attempt Amount Sum is: $Sum”
When we talk about comparison operators, Shell Script has characteristics that are little different from other programming languages. See below:
- -eq (equal): Same as;
- -ne (not equal): “Not equal” or Different from;
- -lt (less than): Less than;
- -gt (greater than): Greater than;
- -le (less or equal): Less than or equal to;
Decision structures
Every programming language makes use of decision structures to control system flow through logical decisions.
These decisions are written using logical expressions that make use of comparison operators to assess whether or not that expression is true.
Shell script programming applications
The shell script can implement several very useful applications to the system. We've listed some of them below!Task automation
With the shell script it is possible to automate routine tasks on your machine, such as system updates, upgrades, cleaning, etc.
File manipulation
It is also possible to create, edit, locate, and organize files using a shell script.
Data backup
The shell script can also be used to configure directories for automatic backups and insert utility arguments into them.
That way, dead files can be moved or copied to another location.
Conclusion
Is it clear what shell scripting is? It is an excellent tool that “makes” the user and administrator’s life easier. After all, automating tasks streamlines deliveries and increases process reliability.