Basic Bash Scripting free guide

BASH SCRIPTING



Hello guys, I'm so exited to present this course, which will cover the basics of the "BASH" scripting language.

Let's start with the introduction of this very interesting course, which will carry out several exercises and with clear examples that I will be in charge of using myself, we go there with the table of contents:


Why this guide ?

The first reason why I decided to write this little guide aimed at beginners, be careful, pay attention that this guide is totally aimed at beginners as I mentioned earlier. And for this reason I give this educational guide in which you will learn many new things for your day to day as a user of this beautiful operating system, called Linux.

Many people will wonder why I decide to write this guide and it is that it is more than obvious that I want them to learn the basic commands of linux and be able to move freely without any problem through the linux console, and I will be teaching you according to how I learned to move on linux, and in another article in this section.


Who should read this guide ?

People who have or do not have direct contact with Linux, that is, anyone who wants to learn a little about Bash can read this guide as a starting point reference.

But, the most important thing is that people who use UNIX in their day to day and who want to learn about this beautiful scripting language that UNIX offers us.

As we progress in the different topics, we will develop small scripts that will help us better understand what we are seeing.

Content Table

        1. Variables
        2. Data types
        3. Conditionals
        4. While loops
        5. For loops
        6. Functions
        7. Common shell program
        8. Type of commands in Linux

Variables

The variables in the programming, they are not going to crucify me that yes, I know that Bash is only a scripting language and that it is not cataloged as a programming language, but it is to have a more direct approach with this and to accommodate the use of the words, so without further ado let's start by giving the example:

Imagine you have a box in which you can store things, imagine that you have 3 boxes and you have to store 3 types of data in each of the boxes, let's see:

VAR1='Strings' or "Strings"
VAR2=Int(any number ex: 5) without quotes
VAR3=Boolean(True or False) depending

Cool, right? Now we now that we can store differents types of data on each of the boxes depending the type of the data. One caveat to note is that Bash doesn't like spaces between the equals symbol (=).

Data Types

As we mentioned in the previous section when we talked about variables, we also mentioned the types of data that there are, obviously this is super easy to know, so we will not spend so much time explaining how to use them because with the simple and clear example of the previous section I think it is more than enough.

But, I will give you some more examples just in case you are too lazy to go up to see the previous examples:

Int >>> is a number like (1, 5, 5544, or 8.9)

Strings >>> is a sequence of strings or plain text within quotes
Boolean >>> It is a type of data a little different since it is the type of data that is practically most used in the comparison or conditionals that we will see better later ...


Conditionals


The conditionals are the ones that will help us to make comparisons, that is, imagine that we have a number, any and we also have a list of numbers from 1 to 10 for example, for us to know if the first number we have, imagine that The first number is 6, then, to know if the number 6 exists or not in this list that we have, we would use the conditionals. 

Let's see an example from the console.


As we can see here we use a for loop, which we have not explained yet, but do not worry, since the next section of this article is about the for loop, we are going to explain the syntax of the conditional

First we initialize the script with the Bash tag "#!/bin/bash"

Then we see the for loop that we will not explain in this section, but it is basically to take each value of the array and store it in the temporary variable $ i, then we see the conditional "if; then; fi".

What this condition is doing is that it is telling the computer that if the $number=6 exists in the for loop stored in the variable $i, which if it is True (Boolean data type), then it returns the number of the array that matches our first number stored in the variable $number, the "then statement", is used as an instruction to tell the program what to do after the comparison is made and the "fi statement" is always used the comparison is completed.


The "else statement" is used to tell you that if the first condition is not met, then it executes the following, which in our case would be to print the number 6.

While Loops

The bash while loop is a control flow statement that allows code or commands to be executed repeatedly based on a given condition. For example, run echo command 5 times or read text file line by line or evaluate the options passed on the command line for a script.


What this little script does is execute the sequence of numbers that is in the "$ echo" command. But, it does it in an infinite way since there is no condition that is executed after the while loops to end or break the flow. We could end the while loops with the "Break" statement.

We can also control the while loops with the conditions inside brackets, as follows:


With this we finish this section, I hope everything has been clear, otherwise you can leave your comments in the box below to answer your questions or you can consult directly from the contact section in the navigation menu.

For Loops

A for loop is one of the main declarations in various programming languages. Here, we will explain how it is used in the bash programming language, hence the name, "for loop for bash - bash for loop". Get ready to add a new tool to your developer arsenal!

A for loop is an iteration statement, which means that you can execute code repeatedly. Let's say you want to execute an instruction 5 times. Instead of writing five separate codes, you can write a for (for loop) loop syntax only once. Let's get into the details, shall we?

The following content would be the basic syntax of the for loop, it is not difficult to understand, you just have to take the time and trouble to memorize it and practice, especially practice, because if we do not practice we will not carry or remember what we have learned.


In the real world this script would look like this ...


You are probably wondering why I write the commands in text format and I did not put them in the command console, it is simple, I got tired of writing it and then cropping the image, so that's why I write it directly and change the color and use it. I save a few more seconds or minutes ...;)

Functions

The functions in bash work in a very peculiar and easy way.

To declare a function, just place the name of the function with the keyword function before the name of the function that we have chosen and at the end of the declaration of the function two parentheses, one that opens and one that closes, within these parentheses we can define variables.
Once the parenthesis is closed, we can place the keys inside which all the instructions that we want to execute will go, and to call the function we will only have to put its name in any part of the program that we want.

Let's proceed to create a small script with everything we have seen and learned so far, which simply shows us a specific part of the results table of a command executed in bash...


As we can see, the function as we mentioned earlier, begins with the function keyword, and then continues with the name of the function that we want to give it.

And that's how they declare and are called functions in bash and that is how they work, easy, right? I know, and it is that when I began to study what is informatics in a deep way when I started hacking, I did not know that it was going to be so easy, fun and entertaining.


Common Shell Programs

The shell is the command interpreter in an operating system such as Unix or GNU / Linux, it is a program that executes other programs. It provides the user of a computer with an interface to the Unix / GNU Linux system so that the user can execute different commands or utilities / tools with some input data.

When the shell has finished executing a program, it sends output to the user on the screen, which is the standard output device. For this reason, it is known as the "shell".

  • Bourne shell (sh)
  • C shell (csh)
  • TC shell (tcsh)
  • Korn shell (ksh)
  • Bourne Again SHell (bash)
  • Zsh Shell

Type Of Commands In Linux

ls - List directories and files.
cd - Access a directory.
mkdir - Create a new folder.
touch - Create a new file.
grep - Match a word or multiple words in command or file output.
find - Find files.
which - Know the full path of a command.
locate - Locate a file or any directory with the specified name.
clear - Clear the console input
echo - It is used to print any message that we pass after the command.

¡Don't forget to comment and share the content!

Comments here, cracks!

Post a Comment

Comments here, cracks!

Post a Comment (0)

Previous Post Next Post