Check out a free preview of the full Introduction to Bash, VIM & Regex course:
The "Introducing Scripts" Lesson is part of the full, Introduction to Bash, VIM & Regex course featured in this preview video. Here's what you'd learn in this lesson:

To keep from writing the same sequence of commands repeatedly, James shows how to write and store a script that can be executed.

Get Unlimited Access Now

Transcript from the "Introducing Scripts" Lesson

[00:00:00]
>> James Halliday: The really fun part and useful part about writing scripts in Bash is you can take all of the commands that we've already learned, that we've already been typing, put them into a file, and then run them again and again with whatever arguments you want. So all of you pretty much already know how to write Bash scripts.

[00:00:23] We just need to do a few more things, and then we can take these command line work flows and encapsulate them into something that is more reusable, more portable. So how you do this is, so maybe we'll make a script that will just log a message into a file that's based on the current date, right?

[00:00:53]
>> James Halliday: So I'm gonna make a new directory to do this. I'll call this script Logger, no extension. Sometimes people put a .sh extension, but you don't have to. The first thing that you put is called a number of things. I've heard it called the shebang line, or the hashbang line, or the interpreter it's also sometimes known.

[00:01:21] I don't know exactly, but anyways, you put a pound sign, an exclamation mark, and then the name of the interpreter which in our case will be /bin/bash. And there are other interpreters on the system, like sed and Perl and whatnot, but here is how we write a Bash script.

[00:01:40] So the next thing that we wanna do is we wanna put the commands that we would otherwise type, but we're gonna put them in this file instead. So to do that we're gonna say, we'll maybe print out the timestamp, because logs with time stamps are more useful I think than without them.

[00:02:03] So to do that we can use back ticks which we just learned about to this state stream %F %T or %T not +T.
>> James Halliday: Maybe with a dot. I like that format or how do they usually put it? It's like z, I don't know, that's fine. So that's gonna be the start of our, this is our log format.

[00:02:31] Why don't we just stat doing this in the command line so we can kind of test out how this is gonna work. All right, your %F %T just like this, and then we're gonna have our message. So message goes here. All right, and we gotta do echo in front of all of that and that should be back text, okay.

[00:02:52] Great, so this is how we want our log format to work and then we're gonna wanna write that out to our Log file base on the current date. So we can use backticks again for that. We'll just date %F which is gonna be the year, month, day with dashes in between.

[00:03:11] And we'll call it .log, all right, so this is what we would do on the command line to put a message, right? But the thing is we don't wanna have to type this out every single time, right? So the part that's gonna be different every time is this message in the middle, right?

[00:03:28] Let me just make sure that that works and here we see 2017 whatever log. Looks like it works, all right. So now we can copy paste this into this other file, that starts with a #!/bin/bash and we can use an argument. So there are different ways of doing arguments.

[00:03:55] Whenever your script is called, you'll get all of the arguments that were passed to it as environment variables that start at one and increase in number. So like, dollar sign one, dollar sign two, dollar sign three, etc. So here, we have our script, right. And the next thing that we need to do is do this command, chmod +x and then the name of our script file.

[00:04:24] And what that does, is it sets the executable bit on the file, and I'll talk about this a little more in a bit. But basically, to run a script, you have to do ch1+x.
>> James Halliday: So to run this file, you have to specify either the absolute, or relative path.

[00:04:46] You can't just type logger. So we can use one of the things that we already learned which is that the current directory is called dot. So you can do ./logger, and then give it a message.
>> James Halliday: And now we see a new file, cool. So now our new message shows up in the log.

[00:05:11] But there's one more thing that I think we wanna do. So if I do a message that has two words in it, right, hello world, and cut it out, we only see the first bit of that, which is hello. So I could either run the logger by quoting the string like this, which prints out what we would expect, like the complete message.

[00:05:32] Or there's another variable we can use in our script, which is called, there's a few of them. But so there's $* and $@, whoops, $@ sign, there's kinda some subtle differences that I don't wanna get into about which is which and which works in which circumstances, but whatever.

[00:05:53] For this we can use either one, so we'll use $* and now, I can say hello Minnesota and when I cut it out I see the complete message. So I don't have to quote it necessarily, but if I quote that then also works,
>> James Halliday: Right, and when the next day rolls around it'll start writing output to a new log file because that's how our script works