The web is full of terminology that could be confusing to non-IT people. Some of these terms can be source code, scripts or script, snippets, etc. Well, in terms of source code, it is all that text or information written using a programming language and respecting a certain syntax to form an algorithm or specific function that will make up the program.
Thus, source code is a somewhat more generic term and broad, being able to include the other terms as well, since a snippet is a fragment of source code that usually does not become too complex. For example, when we dedicate ourselves to web design or we need to insert a banner on our website, normally the ad providers such as Google Adsense or Amazon Affiliates give us a snippet with the HTML code to be able to insert it on our website. Therefore it is not a complete program, but rather small reusable pieces of code.
Interpreted language vs compiled language:
If you are wondering what is a script or scriptYou should know that in programming this term is used to refer to source code written in some type of interpreted language (almost always). And what difference is there to traditional compiled programming languages? Well, unlike these, the source code is not compiled once and transformed into a binary, but rather an interpreter is needed to act as an intermediary and each time the program is to be executed, the interpreter must translate the code for the machine to understand. That is, the summarized steps would be:
- Write the source code of the program using some programming language to compile such as C, BASIC, C ++, Ada, ALGOL, D, COBOL, GO, Fortran, G, Lisp, Pascal, Swift, Visual Basic, etc. The code can be written in any text editor or using a more complete development environment or IDE.
- We compile the code source using some compiler, such as GNU GCC. With this we manage to transform these commands into a high-level language that only programmers and compilers understand into a machine or binary language that is understandable or executable by the CPU.
- El binary can be executed as many times as we need without going through the previous steps again. In fact, most software vendors send us the binary directly to run on our computer. It will be the operating system that manages the processes necessary for this, syscalls, etc.
- We write the script or source code using any programming language. We can also use an IDE or just a text editor.
- In this case, it is not compiled, but can be directly executed with the help of the interpreter. That is, if we use Bash, we need it installed on the system to be able to execute our script. If we use a Python script, we need the Python interpreter installed, etc.
- When we run the script, It will be the interpreter who interprets the words or language that is in it (hence its name), that is, these instructions do not pass directly to the operating system and CPU, since it would not recognize them without the help of the intermediary or translator who is the interpreter.
Having said this, we see that in one case or another there is advantages and disadvantages. In the case of compiled files, they do not need compilation at runtime, therefore, once compiled the first time, we can execute the binary as many times as we want without wasting resources on it. This is not the case in scripts, which will need to allocate resources for the interpreter as well, therefore it will generally run slower.
Creating our first script on Linux:
In our example we are going to use Bash's own interpreted programming language, and therefore our interpreter will be Bash. First of all we must know that many script files have headings known as shebang according to the interpreter used. For example, in Linux we can find different interpreters, in the case of Bash, the shibang is #! / Bin / bash, but in any other case it will be the binary that points to the interpreter or shell in this case. Also, in the case of Unix and Linux, the script file usually has the extension .sh.
In the script we can use interpreter commands, operands, constants, etc. For example, we can create a simple script to make backup copies on our system and create a record with the date creating a file called backup.sh with our favorite text editor. Its content will be:
<div> <pre><span class="com">#<span class="simbol">!</span>/bin/bash </span></pre> <pre>tar cvf /backup/copia<span class="simbol">.</span>tar /home/usuario</pre> <pre>date <span class="simbol">></span> /backup/log_copia</pre> </div>
For example, in the example above you will create a backup copy of the / home / user directory and pack it into a tarball called copy.tar, then write a date log. To execute it, we will have to give it execution permissions, for example:
chmod +x backup.sh ./backup.sh
A simple example I think that for the most novices what a script is. Which is a fairly recurring question lately ...