This course has been updated! We now recommend you take the Full Stack for Front-End Engineers, v3 course.
Transcript from the "Bash scripting: Exercise" Lesson
[00:00:00]
>> Jem Young: So let's write a quick shell script, this one is pretty fun. So on your home machine, so create a new file, vi load.sh.
>> Jem Young: You can call it actually whatever you want. I'm just gonna call it load, cuz that's the name I'm gonna use for the rest of the course.
[00:00:21]
>> Jem Young: And put your shebang for /bin/sh.
>> Speaker 2: And could we run bash instead of sh as our way to execute it?
>> Jem Young: Yeah, sh is pretty standard, cuz bash is kind of a higher form of sh, but yeah, you could run bash just fine.
>> Speaker 3: If we don't specify, it will run the default?
[00:00:43]
>> Jem Young: It won't run at all, cuz it won't know what to do. If you don't put that in the top, it's just gonna, it's a text file, how do I execute a text file? By putting this, it's saying, all the code underneath, I'm gonna run it with this shell.
[00:00:54] Great question though.
>> Jem Young: And what are we doing here? I tried to break down a more complicated command. So we're gonna cat, we're gonna split the output of /proc/loadavg, which is just kind of a running tally of what's happening in your system. If you ls/proc, you'll see a lot of interesting files, loadavg is something that it keeps track of.
[00:01:14] It's saying, what's the load on my computer? And then were gonna pipe all of that, so we're gonna pipe the standard out into awk, A-W-K, and we're gonna extract the first, second, and third columns of data. And you're saying awk, what is awk? I spent, in college, I probably spent two, three weeks on just awk, and then the other parts were on said, but awk is a way of extracting data from a file.
[00:01:43] If you master awk, you can do amazing things. You can parse through any sorts of data you want very, very quickly, and it's really easy to do. I wont go on a long rant about awk, but it's probably one of the more powerful tools in your kit. If you would ask someone who's like a DevOps person, been doing it for years and years and years, if you asked them about awk, they'd be like, yeah, it's amazing.
[00:02:08] It does exactly what you want very simply. It just, it's a way of reading files very quickly and extracting data. So for now we're just gonna extract the first, second, and third columns of data, that's it. So I'm gonna go ahead and do this on my machine. So vi, make sure we're in the right directory, print working directory, clear, so vi load.sh.
[00:02:36]
>> Jem Young: /bin/sh, and just doing cat /proc.
>> Jem Young: Type that into awk.
>> Jem Young: One, I don't remember, yeah, I'll print.
>> Jem Young: Does that look right, yeah.
>> Jem Young: And so write quit on that.
>> Jem Young: And as always, if you're ever confused about these long commands we're running, run them individually.
[00:03:37] See if they output, and then just do that one by one, and you kinda see how long commands get built. It's pretty useful to do.
>> Jem Young: So if you try to run your command, you're probably gonna get an error. So I'm gonna say load.sh. In fact, it won't even auto complete.
[00:03:57] I'm hitting Tab, it won't auto complete, cuz it knows it's not an executable command, but even if I try to force it, it'll say load.sh not found. Let's try it with this syntax. Permission denied, why is that? Because it's not an executable file. Like you asked earlier how we make shell scripts executable, we're gonna use our old friend chmod.
[00:04:16] Let's jump there, so if you sudo chmod 755 ./load.sh, it'll become executable. So,
>> Jem Young: sudo chmod, change mod, and 755 ./load.sh, and now,
>> Jem Young: Oops, I did something wrong, that happens.
>> Jem Young: Maybe I missed something.
>> Speaker 4: Trailing quote.
>> Jem Young: Trailing quote?
>> Speaker 4: The last after three.
>> Jem Young: Did I miss one?
[00:05:11]
>> Speaker 4: No, it shouldn't be there.
>> Jem Young: You're right. Good catch, thank you.
>> Jem Young: Awesome, I have no load cuz I'm not really doing anything right now.
>> Jem Young: So if we jump back to what the load will look like in the future, remember our old friend top? top, tells you what's happening on our system.
[00:05:34] Right now, we're not doing anything, it's just an idle system sitting, spinning, so we don't have any loads, but if you look, do we have htop installed? I'm gonna install htop cause it's prettier. So apt, you don't have, to but it's nice, htop.
>> Jem Young: htop is a kind of a prettier version of top, it just tells you what's happening in your system.
[00:06:01] And we see our load average is 0, because frankly, we're not running Nginx, we're not running Node, we're not actually doing anything other than running a shell. So our load average is nothing. That's htop, if you wanna install it, but you don't have to.
>> Jem Young: But when we do have a load, eventually, we'll come back and look at this script, cuz we're actually gonna use it later in our node process, it's gonna be awesome.
[00:06:23] But loads, they come in, they're always in three parts, they're 1 minute, 5 minute, and 15 minute increments. Why is that? Why would we care about 1 minute, 5 minute, 15 minute, anybody? Think about servers, think about what your server is doing, but why would we want insight into what happened 1 minute ago, what happened 5 minutes ago, what's happened in 15 minutes?
[00:06:53]
>> Jem Young: So nobody, hazard a guess?
>> Jem Young: Okay.
>> Speaker 5: You wanna know if it's a blip in time or if it's ongoing problem.
>> Jem Young: Exactly, that's precisely right. You wanna know what's happening now. Has it been happening for a long time? Is it just, are we just handling an unexpected load of traffic temporarily?
[00:07:14] Or has this been going on for a long time and it's something we need to actually address? That's why we have 1 minute, 5 minutes, and 15 minutes increments. I can go a bit more into loads if you want, but we'll cover that later, cuz we're gonna actually use this data.
[00:07:31] And we're gonna spit it out to our webpage to make a nice, cool graph thing that shows you our load average.
>> Jem Young: So word on chmod, if we haven't done chmod yet, because chon is actually a little bit easier to use, cuz it's just chon our user, what we wanna do.
[00:07:48] chmod is much more fine grain. You're saying what is 755, what do all these things mean? There's a very weird formula about reading, writing, executing for the owner, the users, the admins, things like that. I don't actually know the formula off the top of head. I don't know of any people that do, cuz there's a calculation you do with math, I don't know, math.
[00:08:09] But things you should know at the top of your head, 777, if you see something with chmod 777, it means, let anybody do whatever they want to this file. Let them read to it, let them write to it, let them execute it. You generally don't wanna do 777, it's kind of the lazy man, it's just like, whatever.
[00:08:24] 755 is the owner can read and write and execute, everybody else can read and execute. So they can't write to it. If you're saying, this is so nuance Jem, why do I care about that? Because permissions, like we covered earlier, are pretty important. Someday, if you do all right, you'll be running your own server.
[00:08:42] You're gonna want people to log in to your server, but you don't wanna give them permission to do things that they shouldn't do. Cuz it's easy, as we know, to shoot yourself in the foot, to make easy mistakes. So we locked down permissions so that only you, the owner, can do things.
[00:08:56] And we want everybody else to just like, I want everybody to be able to read and execute this shell script, but I don't want them to be able to change it. That's what chmod is for. We can go a long, long way on chmod and the flags, things like that, but I put a link at the bottom of someone much smarter than me talking about it.
[00:09:12] But know that chmod, the basics are 777, lets you do anything, 755, kind of let you do anything, but let everybody else just execute and read. These are the basics, but we won't chmod too many things because we're the owner of most of the things we're gonna do, but in the future, it's something good to know.
[00:09:30] I know, it's not sexy, but I guarantee you, if you haven't seen chmod, in the future you will, on some part of your machine.