Hello the beautiful people.
And welcome back to the second part of our discussion about making your very own batch script.
And in this video, we're going to make another batch script that's going to allow us to back up all
the folders in our home directory.
And we're also going to show you how you can set up your batch script so they can be run in a more convenient
way.
So let's make another batch script.
We're going to call it up on our desktops.
So we're going to type Nano, then back up a dot H and in there we're going to add as the very first
line, as shebang line and our shebang line tells the shell to not read this code, not read this file
as a text file, but instead to interpret it as a bash script.
So the way we do that is we type a hash, then a bang, so a hash and then an exclamation mark.
And then the path to the interpreter that we want this file to be interpreted with.
And that is going to be our bash shell, which is located at slash bin slash bash.
And we can check that if we're close out of that.
And you go back to our command line by typing which and then bash and we can see slash bin.
Slash bash is where the bash interpreter is located.
So with that, if we open back up our backup script, we can now underneath here start writing commands
that will make up our script.
Now we want this script to to compress and archive all of the folders inside our home directory, or
at least the downloads, documents, desktop pictures and videos folders because they're the best.
So let's just let's see how we might do that.
Okay.
Now, if you remember from what we learnt at the end of the last section to make this happen, we'll
need to use the tar command and we need to give it a few options.
So for example, we'll give it the C option to create a new archive, the the V option to make it speak
to us and the F option to tell it that the next argument is going to be our is going to be our archive
file and we're going to call it back up to GZ.
But to make it compressed using GZIP, we actually need to give it the Z option as well.
So we're going to compress it through GZIP into a file called backup to GZ.
But now we need to tell it what we want to actually store.
And this is what we're going to tell it.
We're going to say, Hey, I want you to put in there.
We want we want to put in there everything in our home directory.
We're going to put in our documents folder, our downloads folder, our desktop folder, our pictures
folder, and our videos folder.
So what we're saying here is, hey, use the time command, compress it, be verbose, tell us what's
going on, zip it through GZIP and make the file called backup to GZ and inside there.
Put the documents, downloads, desktop pictures and videos folders from our home directory using a
bit of brace expansion to save us some typing.
So that's it.
Now when we run our batch script, if we do batch backup DSH, you'll see that it's given us some output
and now we've got a backup to GZ file on our desktop.
So isn't that very cool.
Right.
And you can see here all the files that have been added into the, into the, into the, into the backup
folder.
Now in there we gave it the V option here for verbose and that's why we got to see all that output on
the screen.
Maybe we don't want that.
So if we just get rid of the V and let's say we remove backup dot GZ and now we run it again, you can
see that now we don't really have as much output from it, but we've still got the backup file.
So you can see here one benefit of writing these things in scripts, you can save and edit the behavior
of those scripts over time as your needs evolve, and they're always going to be there for you to run.
For example, we might not like the fact that this message comes out on standard error, so we can just
go back into our backup DSH and redirect this this command standard error to a place called slash dev
slash null, which basically gets rid of it so that we are we've redirected standard error to slash
dev slash null, which is a place on the file system called the Bitbucket that basically just deletes
whatever is sent to it.
And now if we try to run bash backup dsh, cli the screen, we run it.
Now we know that we now get no output, but if I delete this backup archive and run it, you can see
that the backup is still created.
But now we don't get any errors.
So you can see that you can customize these these scripts as time goes on.
So what I want to draw our attention to a bit right now is how we can make running the bash scripts
a bit more efficient and user friendly.
So at the minute, what we're doing is we are on our desktop where the bash script is located and we're
typing the Bash Command and then giving it the path to the bash script, which at the minute, because
we're already on the desktop is just backup DSH.
When I run that, we get the the required output, but that's kind of a bit of a hassle because if we
weren't on the desktop then we would have to give it a different path and we have to run the bash command.
There's a, there's a better way to do it in just one step and this is how you set that up.
So what you do is you go back to your home directory and you can see in here that we've just got our
normal folders.
What you do is you make a directory called bin, all in lowercase bin, and you can see that that's
been created.
And you what you do is you move all of your shell scripts into that folder.
Let's move from our desktop, the, the backup DSH file.
So backup DSH, we're going to move it into our bin folder.
So we're moving from our desktop, the backup folder file into our bin folder.
Okay.
So you see it's been moved from there.
And when we now go into our bin, we see that backup is in there.
And actually I'm just going to rename it from backup DSH into just backup.
Okay.
So now we've just got the same file called backup and if I run it, it'll still work.
So there's nothing wrong.
It's I've just renamed it a little bit now.
I need to make this so that it can be run from the command line.
So what we're going to do, there's two ways to do this.
First of all, we can open up in our file browser, we can go into bin and then we can right click click
properties, go on permissions and then click allowing executing file as program.
Okay.
That's one way to do it, but I'm not going to do it like that for now because I want you to learn the
terminal way.
So what you do is you type the C mod command.
Now the C mod command is stands for change mode.
And what it does is it allows you to change the permissions that a certain file has.
And then as a second argument, you type plus X, which means give it executable permission.
So add executable permissions and then you tell it the file.
So just back up when I run that.
So what we've done is we've added executable permissions to the backup.
Now when I look at it with LS, you can see that it's gone green.
Okay, so that means that it can be run as.
A script.
Now, the cool thing about this is, is if we close out, actually, what I'm going to do is as well
just to make it a bit clearer.
If I go back in to back up, what we're going to do is I'm going to save make sure that the backup is
always saved on the desktop.
That's just going to make it a bit clearer to demonstrate what's happening.
So what we're going to do is I'm going to close the terminal.
I'm going to delete our backup here.
And now I notice I'm in our home directory, but if I type back up, we can see here that we're getting
an error saying that the backup program is not currently installed.
Now, if you remember what I told you a long time ago, right at the start of this course, when the
shell says it can't find something, that means it's not on the shell's search path.
So we need to edit the shell search path to include our recently created bin folder.
Now, the way that we do this is we're in our home directory.
If we go ahead and use the nano command, I want you to edit the dot bash RC file.
So the dot bash RC no spaces, all lowercase will open up this very important file.
Now, if we scroll all the way down to the bottom and add a line down there, this is what I want you
to write, okay?
I want you to write path in uppercase, then an equals sign.
Then some double quotes are two double quotes.
And inside those double quotes, dollar sign path a colon, then dollar sign home in uppercase slash
bin.
So let me explain this a little bit.
So what we're saying here is a variable called path.
We're going to have we're going to set our path equal to what the path currently is.
Then a colon, then our home folder slash bin.
So the bin folder in our home folder is going to be added to the path.
And when I close that, we right to the bash.
RC close it and if we close our terminal and open another one, what that has done, if we echo the
path, what that has done is it has added slashes, home slashes, the add slash bin to our path.
Now that means it'll be able to search for our our backup script in the path just like it would any
other command.
So now when I run back up, you can see that our script has just been run and it has now created the
backup.
So if I, if I now go into our bin folder and I create another file called Hello for example, and in
there I tell it that it's a batch script with the shebang and I say Echo hello world and save it.
And then I give the file executable permission.
So C-H mod plus x.
Hello.
See that it's gone green now.
I can be anywhere in my file system.
I could be in my documents folder but if I run.
Hello, you can see that hello world now works and I can pipe these to wherever I want to redirect them.
So if I redirects it to a file called Hello to my desktop, there we are.
We've now got a file called TXT on the desktop and we're now basically creating our own commands.
So isn't that awesome, right?
Like we've got our own our own commands and anything that we put in this bin folder, if we give it
executable permissions, we can run it just like any other command.
So that is incredible.
But now you might be thinking, okay, Ziad, this is cool and everything, but what's really the difference
between an alias and a script?
So remember, aliases are things that you put in your in your in your bash.
Aliases file this one this dot bash aliases file.
And if we if we take a look in there, what we're doing is we are setting command names and we're giving
them what those commands mean.
Why didn't we just make a make an alias and we say backup equals task dash CV, Z, F of to our to our
desktop and then put back up to GZ and say we want in there, we want our desktop and our documents
and our and our downloads and our pictures and our videos and then close it.
Why don't we just do that as hard?
Like we, you know, we know how to make aliases.
Wouldn't that work the same?
Well, yes, it would work the same in this instance, because we have a one line script.
I'm going to close that.
We're not going to do that.
That's because we have a one line script.
So aliases can become the same as one line script, really.
But the good thing about script is that scripts are designed to contain more than one command line,
whereas aliases are usually just to give more convenient names to just one command line.
Now, secondly, scripts can have more complicated programming constructs such as loops if statements
and functions.
Now, if you've never done any programming before, suffice it to say that scripts are more heavy duty
than aliases can be.
And thirdly, scripts can be scheduled to run at any time using Cron and.
Scheduling part is what we're going to focus on in the next video.
So over the last couple of videos, you've seen how to create your very own scripts.
And the key about batch scripts is that they allow you to store useful command sequences in files that
you can use later on.
Now, because anything on your computer is changeable and editable through the command line, and because
you can save these series of commands that can do specific functions, you can create incredibly powerful
batch scripts that can do whatever you want and save those batch scripts for later by using the fundamental
skills that you've learned in the last two videos.
Now, a key piece of information is that bash scripts must have the shebang line, telling them that
the file needs to be interpreted as bash commands, using the bash shell interpreter, which is located
at slash bin slash bash.
Now this shebang line is the first line of every script, and there mustn't be anything above it, and
there must not be any spaces in the shebang line.
The text that's highlighted here in Orange.
So it must come just as you see it there.
And it's the shebang line that turns your regular text file into a bash script and tells the shell to
interpret everything in that file as bash commands.
Now, if you wanted to make a Python script, you could put as the shebang line something like hash
bang, slash slash user slash bin slash Python three, and that would allow the script to be interpreted
as a Python program.
But this is how you do it in orange here for bash scripts.
Now, you've also seen how you would make scripts rumble just like regular commands, and you saw how
you would make a bin folder in your home directory and add each script to that bin folder.
You would then give each of the scripts executable permissions using the C mod command and add that
bin folder to your shell's path by editing your bash RC file.
Now this allows you to run scripts just like regular commands.
So we type backup and our backup script ran.
We typed hello in our Hello script run.
And this will also work for Python programs as well.
Now as an aside, if you're interested in learning more about the Python programming language, then
go to the last video of this course where you have some discount coupons for all my courses, including
my best selling Python course, the Python Bible, which will take you in a project based way from absolutely
no previous Python experience all the way to writing 11 Python programs.
So if you're interested, go ahead and check that out.
But the bottom line is that scripts are more powerful than aliases, and scripts can be run at any date
or time that you like, which allows you to automate your workflows.
And it's precisely that scheduling that will be coming onto in the next video.
So in the next video, you're going to learn the true power of shell scripts by learning how to automate
them using an incredibly useful utility known as Cron.
So to learn how to schedule automated tasks, I'll see you in the next video.