Hello everyone.
In this demo I'm going to talk about bash scripting and also I will show you how to run Bash scripting
before I'm going to do the demo or run any command.
Let's understand what is the bash and also what is the shell and what is the script.
So let's start with the Bash Bash is a Unix shell and it's a short for porting against Shell.
Basically, it's the command line interface, the CLI that interacts with the operating system, for
example, the slash pen slash bash.
Next, what is Shell?
Shell runs Linux commands in order to interact with computers.
The terminal that we use in Linux operating system this is the terminal right now is considered a default
shell of the Linux operating system.
Next.
What is the script?
Scripts are used to run a series of commands and can execute the commands interactively, interactively,
one by one.
All the commands that we run from the command line can be used in a bash scripts.
So I hope that you guys get the idea about what is shell and what is bash and what is the script.
Now let's do let's do a demo, an example in order to understand the these concepts.
So I would like to run the following commands in order, so the following commands.
So I'm going to run Echo.
Welcome to our xxA course.
Marked and I hit close the the double quotation hit enter as we can see over here.
After I run this command, it showed me this message.
So the second command also I'm going to run.
Who am I?
And also, if I would like to run the the command date to see what is the date today.
And also, I would like to run the calendar here.
Perfect.
So what I did here, when I run this, this commands why I run these commands, because I would like
to interact with the computer, which is the default shell of the Linux operating system.
So as we can see over here, so this is the default shell of the Linux operating system.
And what we and what is the what the shell did.
So after I run those commands, the shell is going to retrieve those commands.
So the echo home I and the date and the calendar and also is going to display the outcome on the screen,
as we can see here on the terminal.
So if I would like to know what is the default interpreter or the shell for the current user?
So I can run the command echo, then dollar sign, uppercase shell.
So the reason to run this command, because I would like to know what is the current shell or the.
The default interpreter for this current user.
So we're going to use this one, the slash pen slash bash when we're going to write the script.
So I'm going to explain it when we're going to write the script.
So next, if I would like to know where is the bash Interpreter is located?
So I run the command which bash.
I hit enter and is located under the slash user.
Slash pen slash bash.
This is the bash command.
Now let's write the first script.
So first I'm going to create a new directory in order to save all the bash scripts under this directory.
So I'm going to call this directory bash script, CD Bash dash scripts.
We are right now inside this directory.
And next.
Now I would like to create the first script so I can use either the nano editor or the Vim sub to you.
I will go.
For me, I prefer to go with the vim.
So vim then the name of the script.
And it has to be.
Uh, so the extension or the type of this one has to be a shell, a bash script file or a script file.
So it has to end with ssh.
So the name of the script then that is H hit enter.
Then I in order to insert.
Now is the first line that I'm going to write is the shebang line.
And also it's called hash bang line because of the two character with the hash and the bang, then slash
bin slash bash.
As you guys remember what I said about the well, the default interpreter or the shell for the current
user.
So this is the default interpreter or the shell for the current user slash pen slash bash.
Then the second line, this is the best practice usually as a programmer or if you would like to write
a script, you write a comment at the top.
So hash then this is a comment.
So the script, when we're going to run the script, so it's going to ignore this line.
So it's a best practice to write a comment.
What is the purpose of this script?
So I'm going to type this is my first
script.
Perfect.
So this is a comment.
And then what is next?
The next line I'm going to write the Or type the commands that I already run them before.
So Echo.
Echo as you can notice that once we type the comment so that the command it will the and and and the
script here, it will change the color.
Next I'm going to type the message welcome to is a.
Course and exclamation mark.
Then we close with the double quotation.
So as the reason I use the double quotation because I'm going to use the exclamation mark or the bank
over there.
Then after that, I'm going to type the second command.
Who am I?
Date?
Then calendar.
So what I did, I wrote four commands and I'm going to save the file.
So Colon X, then the bank or the exclamation mark hit enter.
Now, after after I wrote the first script, what I'm going to do, I would like to run the script from
the command line.
With like from the command line with the bash interpreter.
So this is the shell.
So what I'm going to do right now, if I will type only the name of the script file and I hit enter
is not going to work.
So in order to solve this problem, we need to type either bash then the name of the.
Script file.
Then hit enter.
Or I can do another option because we are in the current directory here.
This is the Bash Dash scripts and the script is inside this directory so I can do the dot and the slash.
Because we are in the current directory right now.
So we are allowed to do that in order to run the script.
But if even though if I will do that and I type or I hit enter.
Also is going to show me an error.
Permission denied.
So hit.
Enter.
Permission denied.
Because I need to change the permission in order.
Why?
I need to change the permission of this file.
So ls.
Let me see.
LSD Welcome.
As we can see over here, we need to change the permission.
In order to allow the script to be executed executable for this user.
We don't have an execute permission for this script, so it's easy.
So the only thing is change mode plus X.
So I'm going to do give an execute permission to to the user and the group and the other then the name
of the script file hit Enter lSl Welcome, hit Enter.
As we can see here, we assign an execute permission to this script file.
And also we can notice that the color.
Has changed here to green.
It means this is a script file.
Now, if I will do like that bash.
Welcome.
Hit enter.
As we can see here, remember the first message cat?
So that echo.
Will come to our CSR course and the whom I join.
Let me show the content of this file.
So as I said, this is the shebang line.
The shell or the default for the default interpreter for this current user.
This is the comment, as I said, that when we run the script is going to ignore that.
It's just it's a good practice to to to do it in order to see what is the purpose of your script.
And this is the first line.
Welcome to our course.
Then who am I?
I'm user John.
Then here is the date and here is the calendar.
Perfect.
So far, so good.
Now let's move on to.
Well, I will show you another.
We use what bash we can do also the another one.
What?
I said that because we are right now in the current directory.
Since we are here inside this directory, we can run the bash script by typing the dot and slash and
hit enter as we can see over here.
This is the outcome of the script file.
As I mentioned before, just pay attention to it.
So if I if I try to run the script by only typing, only by, by by typing only, the name is not going
to work.
However, there is a way to do it.
So I can avoid that so I don't have to type either.
Bash Then the name of the script or if I do.
Dot, then slash then the name of the script file.
There is a way to do it.
I can add the path of the Bash Scripts directory to the set of directories that are saved in the path
variables.
So what I do like if I type the command echo, then dollar sign uppercase path hit enter.
So as we can see over here.
The colon here separates the basi.
This is directory.
And so here is the colon.
This is separates the directory.
So it's going to separate the path of every directory.
And this is the second directory here.
And this one here is the third, fourth, fifth, sixth.
And the colon is going to separate the path of every directory.
So whenever we run a command line from anywhere in the system, for example, Cat or Echo or fdisk whatever,
we're going to run.
Anywhere in the terminal.
The system is going to search for the executable files that are stored in the bin directories.
Here is the bin directory here in order to run those commands.
So basically, the path of all the all the commands here.
It's it's under that what under the bin directories so it doesn't matter if you guys notice when we
are inside directories or we are for example if I go back here, it doesn't matter.
So here right now, under slash home slash zone, if I write for example, that so so the what is going
to do so the system is going to search for the executable files that are stored in the bin directories.
Here is the bin directories in order to run those commands.
Let's go inside the file again.
Hit enter.
So as I said, if I would like to run the bash scripts from anywhere, so so far I only have one bash
script here or bash file a script file.
So if I would like to run the bash script or the script file from anywhere in the terminal, I can add
the path.
BWT.
The path.
This is the path.
Of the bash script directory.
This is the bash script to the.
I just said Echo.
Where is it?
Here.
To where?
To here.
To the bath variables.
So I can do that as I said that BWT.
So I'm going to copy this one and then I'm going to type the command export bath.
So this is the command that we need to run in order to save the bath of this batch directory to.
To the to the to the bath variables over here.
So in order to avoid, uh, you know, typing the bash before the script file or the dot slash before
the name of the script file, then after that equal dollar sign and uppercase bath then.
Calling, then we're going to we're going to copy this here, the bath here.
I'm going to just delete this one here.
Perfect.
So as we can see here, the command export.
So the command here, the the command, the export command is going to add the path of the bash script.
So bath equal.
Dollar sign, bath, colon, then slash home slash join slash bash scripts and I hit enter.
Now, if I would type welcome
that is it hit enter.
As we can see here, we can see the magic over here.
We don't have to write the bash.
Then the name of the script or script file or we do do that slash.
But there is.
Okay, there is a quick note.
So I kind of written about it.
So we need to run this command again.
Every like.
So what?
I mean, like we need like if I exit from the terminal and I open a new terminal.
I need to run this command again.
So in order to avoid that, I need to save it permanently.
I may like to save this line permanently.
In the file.
Let me show you.
What is the file?
CD.
Go back here.
Clear.
LS ls dash la.
So even is going to show the hidden directories so you can save it over here if you want to use it for
this term or for this user.
So in this file VM that bash each enter, we can save it over here.
Type here
and escape.
Save the file.
Perfect.
After we do that, we also we need to run in order to update that.
In order to update this file, we need to type source dot bash args.
And if I were going to see, okay, so we are not even inside that the bash that Dash Scripts directory.
So if I'm going to type welcome that it hit enter as we can see here, here is the magic over here.
We just typed only the name.
And the system or the interpreter show us the outcome.
Okay, let's conclude our session.
Thank you and see you in the next lesson.