Check out a free preview of the full Complete Intro to Linux and the Command-Line course:
The "Writing Scripts" Lesson is part of the full, Complete Intro to Linux and the Command-Line course featured in this preview video. Here's what you'd learn in this lesson:

Brian explains that developers can create shell scripts with bash to automate a task, and demonstrates how to create, build, and run a shell script.

Get Unlimited Access Now

Transcript from the "Writing Scripts" Lesson

[00:00:00]
>> We are gonna get ourselves into writing your own scripts. This is probably one of the, again, more useful things for you to do. Like if you find yourself writing, like doing the same tasks over and over again, the command line. Whether that's like downloading a file, executing it, and moving that storage to a different area, like you can put those into like scripts.

[00:00:21] And then, you can run just like a script instead having to run like a bunch of commands in a row, you can just go and run a whole script, right? I think that makes sense. And luckily like, you've kinda already know how to do this because, you and I have been programming this entire time just interactively, right?

[00:00:37] We've been doing, running one command and waiting for the result. With this, you can just use Bash script to run many commands in a row, and it's all using the same syntax. Okay, so we're in the shell scripts and then writing your own scripts part of this. So let's go ahead and go and make our very own my first Bash script.

[00:00:57] So we're going to do this all in vi, cuz it's a lot of fun, right? So we're gonna come back over to our command line here, and we're here in my home directory, and I'm just gonna say vi. And you could also do this with Nano if you want to.

[00:01:17] So make a file, what are we gonna call it? Gen files, underscore genfiles.sh. Okay, I'm going to put myself in insert mode with I, and I'm gonna say the first one is going to be mk dir-p. Till the slash temp, okay? So this -p means if the file or the folder already exists, don't error out, right?

[00:01:50] Cuz otherwise if you just do mk dir without the dash capital or the -p, if temp already exists as a K that already exists, I'm not going to, I'm gonna error out and now your script would fail. So the -p means, if it's already exist great, just don't do anything.

[00:02:05] Okay, then we're going to cd into that temp directory that we just created. So the nice thing about doing CD this way is it's only going to last for the duration of this script invocation, right? So it's not like it's going to if I run this folder is not going to transport me into temp, it's just for the duration of this script.

[00:02:30] Okay, then I'm going to do touch file, and then we're going to do that range thing that we saw before .txt, and then we're going to say echo done. So as you might imagine, this is going to create a directory, it's going to change directory into it. It's going to create a bunch of files and then it's going to echo done.

[00:02:49] Pretty simple, pretty straightforward from there. Okay, so I'm going to hit escape, and write, and quit. Okay, and then I can just say, source. What did we call it, gen_ files, right? And then you can see there, it just says done. So sources when we can do that, I can also do that with .gen files.

[00:03:16] So I have to get back out of this .gen_files like that. Totally equivalent, I would say that the dot notation there, that works in more distributions of Linux source only works in some of them. And you can also say Bash gen _files like that. And that's the part so here's why, notice that this did not change my directory, whereas source did.

[00:03:46] That's because Bash actually creating a sub process, and so the sub process will change directories. But once that finishes, it goes back to my parent process. Whereas sources running this in the same process, which is why it's changing our directory, kinda a weird hair to split there. But, that's a, I guess first of all, is anyone have any questions about that seems like it's a relatively straightforward application of things that we've already done before.

[00:04:14] It's just putting everything into a file, and it's just nice because you don't have to remember how to do it. You just say like, I'm going to create this script file is gonna do one thing for me, and then it's done. Kinda like what we did with the bash 2048 there as well, right?

[00:04:27] That's the same idea, that's just someone's very long game, they put it into a script file.