1.5 JS in a Shell Script - Video Tutorials & Practice Problems
Video duration:
4m
Play a video:
<v Instructor>Our final example of this little survey</v> of hello world programs is a shell script. This is very similar to what we just did with the JavaScript file, but instead of being something that we run using Node at the command line like this, we're going to be able to run the file directly. So we'll see how that works. We're gonna call it "hello." Now this is something that's important with shell scripts. It's generally a good practice not to expose the name of the language being used to write the script. A lot of people do this, but I don't think it's a good idea, and the reason is because the name of the file is the user interface. It's the thing you type. So suppose that you wrote some utility in JavaScript and then later on wanted to rewrite it in a different language. In Ruby, or Python, or Perl. People using the program shouldn't have to know the language that you're implementing it in. So we're gonna start with that practice here in this fourth section, and use just the bare hello as the name of our script. And conveniently, that means it's now distinct from hello.js. So, let's take a look at this. It's gonna be exactly the same. In fact, let's just copy this here, but there's going to be one extra line at the top. So at the top here, we're going to put in what's called the shebang line. I think shell is maybe where the sh comes from, and then the bang is the exclamation point. It's a common way to pronounce that. And then we're gonna put the path to the Node executable. Now this is the kind of thing that might vary based on the system. Remember we can do which node. So, which node, there we go, usr/local/bin/node. Just highlight that, and paste it in. You can type it in if you want. This is the kind of thing that I suggest doing quiche on your system to get this path right. And then I'm gonna save it. And what I wanna do is run this as a script, and the way to do that is to just type the name. If your executable is on what's called a path, which is covered in "Learn Enough Text Editor to Be Dangerous," you can just type it. But in this case we have to tell the system where the file is, because it's not on the path. So, we go relative to the current directory and then slash, and then the name of the file. This isn't going to work, though. If you've seen this before, this is again covered in "Learn Enough Text Editor to Be Dangerous." You might know why. So this says, "Permission denied." What's going on? Well, the answer is that this shell script has to be what's called executable. There's a little bit of metadata associated with a file. You can even look at it here. You can see this rw, that's read/write. This is read, this is read. So I'm gonna do this, chmod, +x. I'm gonna say, alright, you're gonna be executable, and now you can see there's an execution bit here. This x, x, x. So now anybody can execute this thing including me. So I can do this, ./hello. Hello, world! Look at that. So this has been a nice, little, what might be called a breadth first introduction to JavaScript. We've taken this very simple program, this "hello, world" program, and implemented it four different ways. And these are really the four main ways that JavaScript is used these days. In terms of the webpage, the REPL, a file that sometimes might be used in other files, and then a script like this, an executable shell script. One of the reasons I like doing this is because when you see the same simple program in these four different contexts, it gives you confidence that no matter what you do with JavaScript, you're going to be able to make it useful. You're gonna be able to put it on a webpage, or in a script, or in a library that can be reused, and so on. This gives us a great foundation for learning more about the JavaScript language itself, which we'll start in the next chapter on strings.