Hello the beautiful people and welcome to this very exciting section of the course all about bash scripts.
And in the next few videos you're going to learn how you can create your own bash scripts, which are
incredibly useful tools that will allow you to automate your workflows.
Now, learning about Bash script is a massive field in and of itself, and it would require its own
course to cover properly.
But by the end of the next few videos, you're going to have the fundamental understanding required
about how batch scripts work and how to create them so that you can begin automating your workflows
using the commands that you've learned so far in the course.
So this is going to be an incredible and incredible piece of learning for you to let you understand,
like what bash scripts are, how to use them, and how they basically work.
So it's going to be very, very exciting.
I hope you're ready to gain some serious superpowers.
So let's not let's not mess around anymore.
Let's go jump right in and start making our very first batch script.
All right.
So let's go ahead and make a simple batch script.
Now, to do that, all we need to do is just make a text file.
So to do that, let's go ahead and open up Nano on our desktop and make a file called as script dot
S-H.
Okay, so all I'm doing here, when I give the file the dot S-H file file extension is just making it
easier for us to see that this is a bash script with S-H being short for for the bash shell.
Okay.
So if I open that and press control and oh, to save it, you'll see that the script has popped up on
the left.
And if I just close that and clear the screen, we've now got this file.
Now let's take a closer look at this file.
Let's see if it's anything special to the shell using the file command.
And if I do file our scripts page, we see that it's not it's nothing special as far as the shell is
concerned.
It's just an empty file.
It doesn't matter whether this was dot stage or dot pablum or whatever, it's just an empty file.
Okay.
So with that piece of information, let's go ahead and open our script and let's do the most important
thing that you need to do whenever you write a new bash script, you need to tell it that this is actually
a special file and that this file isn't just a text file.
It needs to be read.
Using a certain interpreter.
And an interpreter is something that will read a file and interpret each of the lines in that file as
a certain type of code.
So instead of just reading Cat, okay, if I told this that this is a bash script, it would read this
as the cat command instead of just cat as in, you know, the animal.
If I read, if I typed echo, it would interpret this file as the echo command.
It wouldn't just interpret it as an echo that you hear in, you know, when you're in a cave, when
you show it into into a hall or somewhere like made of stone.
Okay.
So this is the important part.
We need to tell it that this isn't a normal text file.
It needs to be interpreted as code and more specifically as a bash script.
So to do that, you need to first type what's called the shebang.
Now the shebang is a hash, so and then a bang, an exclamation mark and a hash symbol.
Then an exclamation mark is called the shebang.
And then you type the path to the interpreter.
Now the bash interpreter is at slash bin slash bash.
So this is our first line.
We type the shebang, hash bang.
Okay, then slash bin slash bash.
Now that that slash bin slash bash is the path to our bash interpreter.
And we can see that if I close out of Nano by using the which command and typing bash and we see that
we get slash B and slash bash as the path to the the bash shell.
Okay.
So what we're saying here is, hey, this isn't a normal text file.
You need to interpret this file using the bash, the bash shell interpreter, run this as a bash shell
script.
And the key is this line needs to be the first line.
It cannot be the second line.
It has to be the absolute first line.
And there can't be any spaces on the line.
So you finish the line right away.
And when we save that now if we look at the script with the with the file command, we see that it is
now a shell script, in fact, a born again shell script.
Now the born again shell is.
BASH So you've got born again shell.
Born again shell is.
BASH okay.
So this is saying it's a bash shell script and it knows that because it's got this this shebang line.
Now, if you know anything about Python programming, you'll know that the Python programming language
is also an interpreted programming language and the command to run something as a Python script on,
on, on Linux is Python three for Python three.
So if we type A which Python three, it'll tell us the path to the, to the Python three interpreter
Just like it did for the bash.
Interpreter And if I copy that, open up our script again and I write the shebang line again.
I write shebang, so shebang.
And then I paste in there the path to the python interpreter.
So slash user slash B and slash python three and save the.
If I now look at our script, you can see that the shell sees that this is now a Python script.
So that's how you would do this kind of stuff if you wanted to write a script in the Python programming
language.
But because we want to write a bash script, we're just going to give it the path to the to the bash
interpreter, which is that slash pin, slash bash, save that.
And then when we look, we see that it's again a born again shell script, a bash script.
Okay.
So with that important concept, understood.
Let's go back to editing our bash script in Nano.
So let's clear the screen and edit in Nano.
Now, anything that we write in here with this special line added, anything that we write below, it
will now be interpreted as they do in the normal shell.
So for example, if I write Echo and then something like Hello World, this line will be interpreted
as a command.
It's as if I just typed echo hello world in my shell.
So let's see this in action.
Okay, let's save the file using control and.
Oh, and now if we type bash our script, we see that it's run hello world.
How cool is that?
Right.
So it ran the commands that are inside there.
Now we can do whatever we want in our script, so let's just try and add more stuff.
Okay, let's, let's add, let's open it up with Nano and now let's clear out this echo thing.
Let's just get rid of this using the cut command, the control k I just got rid of the whole line.
So that's a nice little shortcut for deleting a line.
Now let's actually use in here.
Let's, let's try and create a file a folder called Magic on our desktop.
Now inside the magic folder, we're going to make 100 files and then we'll use the LS command to get
information about each of those files and save it to save all that information to a log file on our
desktop.
So what command?
Here's a test for you.
Now what command would we need to create a folder called Magic on our desktop?
Well the mkdir command write the mkdir command.
So let's make a directory, let's type mkdir on our desktop, let's make a file called Magic.
So that's going to be our magic directory.
Now we need to make 100 files in it.
So how how might we do that?
Well, we might move into the directory, so we might move into that magic directory using CD.
Then we might touch, I guess, 100 files of file 1 to 100.
That might be what we do.
So far, so good.
So we've created a file called a folder called Magic.
We've gone into the folder and then we've created 100 files.
Now let's, let's like create, let's use the LS command to get data about each of those files.
So if we use the LS command and we, let's say give it, let's say we want to use long form information
and human readable information and save all that to a file on our desktop called Magic Log.
And let's leave it there.
Okay.
So what we've done, we've used the Mac, we've made a directory on our desktop, we have changed into
that directory.
We've created 100 files and then we use the ls l command on our I should actually.
Good, good thing I checked, I should check our magic folder and then redirect all that information
into a file called Magic Dot log.
So if I save that there.
Close it.
And now we we can see the contents of this file using the cat command just like any other file.
We can just cap that out and we can see the contents.
But now let's go ahead and actually run it.
So if we try to bash our our script.
Wow.
Did you see that?
We managed to create a folder on the desktop.
Inside there there are now 100 files.
And if we look in this magic log file.
We see detailed information about every single one of the files inside that folder, just like we told
the script to do.
So isn't that absolutely awesome?
So this is how bad scripts work.
You basically type in there the commands that you want it to do, just as if you were typing them in
in the in the normal shell.
But the benefit is we now have this file that we can keep and do stuff with whenever we want.
So you can actually save that for later.
Now, this should be a moment of amazement for you.
So think about this for a moment.
In Linux, you can control anything using the command line, whatever you want.
What if you want to change, whether it be your desktop background, whether it be your font color,
whether it be, you know, messages that pop up when you log in, you can change anything using the
command line and you can write scripts to hold command line commands.
So therefore you can write scripts that can do anything on your computer and save them for later so
that you can run them whenever you want.
And batch scripts are therefore part of what gives you complete customizability over your Linux machine.
And they are incredibly, incredibly powerful tools.
So you've seen in this video how to make a bash script, but in the next video, what we're going to
do is we're going to do it all over again and we're going to show you how you can write a batch script
to make regular backups for you.
So that's going to be very, very cool.
And we're going to show you how to run it in a much more convenient way as well.
So I'm going to break the video here.
And in the next video we're going to pick it up again by showing you how to make a batch script to do
backups.