This course has been updated! We now recommend you take the Full Stack for Front-End Engineers, v3 course.

Check out a free preview of the full Full Stack for Front-Ends Part 2 course:
The "Shell scripting" Lesson is part of the full, Full Stack for Front-Ends Part 2 course featured in this preview video. Here's what you'd learn in this lesson:

Jem walks through an example of a shell script and shows the pros and cons of shell scripting.

Get Unlimited Access Now

Transcript from the "Shell scripting" Lesson

[00:00:00]
>> Jem Young: Let's talk about shell scripting. So we can write scripts that only run on our shell, they don't open up to any external programmes, they don't do anything like that. Shell scripting very useful, why? Only two reason, they're simple, and affordable. Simple being, I don't know, we can run that fine command, we can run through all these things, I'll put that to a log file, we can do all sort of things in a Shell script.

[00:00:23] And it's just simple cause you start to know these commands like the back of your hand Like, you know fine, you know grep, you know Nmap. You can do lots of things with just shell scripting, so it's simple, it's portable in that we're all running bash. Everybody here is running bash right now, hopefully.

[00:00:37] So if I read a bash script, it's gonna run on your machine just fine cause bash is a pretty standardized shell. So you're saying why shell scripting and not, I don't know, writing a Python script or a PHP script, things like that? The benefit is when you write a shell script it doesn't have to open any external processes.

[00:00:54] It doesn't have to open up a compiler, an interpreter, things like that, it just runs by default cuz they're portable, they're simple. A word of warning, shell scripting is not the fast way to do things a lot of times. If you wanna run something repeatedly like, I don't know, appending to a log, you're doing that thousand of time a second.

[00:01:12] It's partly barely you see or even a better scripting language because that's what they're built for. Shell scripting is for writing simple repeatable task that you are just like, I know how to do that because I know my Unix commands. That's the benefit of shell scripting. Any questions on shell scripting?

[00:01:27] Nobody?
>> Speaker 2: How do you make a shell script excludable almost every time?
>> Jem Young: Great question, you got that money I sent you, in the red envelope.
>> Speaker 2: [LAUGH]
>> Jem Young: We will cover that in one second because that is a great question. So, let's cover just kinda the anatomy of the Bash shell.

[00:01:44] I'm not gonna ask you to write anything that's complicated, but if you see a shell script, you'll say, I know what that is.
>> Jem Young: First one is, we have to start our command we have to start our file, a shell script with this it's just saying like here's how I want to execute this, I want to execute using this type of shell.

[00:02:06] That the hashbang is actually called a shebang you can Google that right now. Shebang it's a real thing it has an actual phrase and when we write note scripts we'll see that instead of bin slash ssh. We're going to write bin slash node usr bin slash node. This is just saying I wanna run this file as a shell script.

[00:02:25]
>> Jem Young: You can ignore the trap for now, trap is just saying, well, how deep do I wanna go. Does everybody know about interrupts on a system? Okay [LAUGH] I'll talk about it briefly. In any given you have a program running. That program has to handle interruptions. So the user hitting Control-C, is an example of an interrupt.

[00:02:47] You have to handle that at some point or otherwise, your system can just crash, you'll be in a weird state where you're writing some memories, reading through memory. This traffic is saying I'm gonna handle interrupts and I'm not gonna let people exit. In general, you trap shell scripting because you can get trapped in your shell.

[00:03:03] Cuz again, it's a trap, so you hit Control C, Control D, Control Q, it won't do anything. Cuz it's saying I know you're trying to quit, but I'm not gonna let you. So in general don't do that. But it's good to be aware of what trapped is. If you wanna execute standard commands we just put it in the grave marker, that's the slash, I think right?

[00:03:23] G-A-R-V-E with a little fanciness on it. But, [LAUGH] it's on here to the key, that's a [INAUDIBLE] marker. So that's just saying I wanna execute this command. So I'm gonna find all log files an search for a log that has been modified in the past day, that's in time.

[00:03:42] I'm just going to put that, and hold that centered out in my files variable. Echo, of course, is just putting stuff on screen, and our wild loop that says we want to ask the questions. We want to read from the input, or we want to read from the console, save that as input.

[00:04:00] And we're going to hold the program there, and we're going to say, for every input we want to say why, whoops. If we want to say yes, delete all the files, delete all the old files that have been modified in the last day. I don't know why you would do that, but it's an example, and to get the variables that we declared up above.

[00:04:19] So here we just put a dollar sign on it. And that's it. And yeah, maybe not the most elegant example, but this is actually pretty easy to write. You don't have to think about node, how do I open FS? How do I read from a file? How do I do that asynchronously?

[00:04:36] With a shell script you just do it, you can just run any Unix commands you want. Handle the standard out, handle the standard in.
>> Jem Young: Okay, no one looks that confused. It's one of these things that once you do it over and over again you're like, yeah. It's your default go to just to write a quick shell script to do something.