What is a script?

Script

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:

Interpreted vs compiled

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:

Related article:
CentOS 7 installation guide step by step

  1. 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.
  2. 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.
  3. 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.

On the other hand, the scripts do not follow these steps and interpreted languages ​​are used. There are many interpreted languages, such as the one used in the Bash interpreter, which will be the most used in GNU / Linux and other Unix, as well as well-known languages ​​such as Perl, Python, Ruby, JavaScript, etc. With them you can write the code that will make up the script that will be nothing more than a command file or batch processing. Obviously depending on the language used, the syntax will vary. In the case of an interpreted language, the order would change to:

  1. We write the script or source code using any programming language. We can also use an IDE or just a text editor.
  2. 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.
  3. 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:

Bash logo

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.

Related article:
How to create a bootable usb from the terminal in any distribution

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 ...


The content of the article adheres to our principles of editorial ethics. To report an error click here!.

3 comments, leave yours

Leave a Comment

Your email address will not be published.

*

*

  1. Responsible for the data: AB Internet Networks 2008 SL
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   them said

    How do I modify the desktop.ini
    regards

  2.   antonio said

    Well, thank goodness you have explained it in a simple way. I have it all very clear ... only that I will now have to look for what is a "bash" ,, "snippets", syscalls, etc etc etc, for the rest, everything is very clear. Just one little thing, nothing more; If I get a window that gives me three options (cancel and two more, I don't remember) about a script, what should I do normally? Can I introduce some kind of virus? Because I do not remember wanting to lower anything so that the window comes out. Explain to a clumsy one, please

  3.   Sebastian Morales placeholder image said

    It has helped me a lot, however the example code is very confusing since I do not know most of the labels, I wish you had placed what each thing you put in the code is for and thus understand it well, I also had the doubt that is it a .sh file?

bool (true)