Transcript from the "Standard Streams & Redirection" Lesson
>> Jem Young: Parts 1s, this is where I cover bit of this, a little bit of that. Just general knowledge that I think you should have. But it's not necessarily core to running your application today. The first thing I wanna talk about it standard streams. Anybody know what standard in and standard out, does that sound familiar to anybody?
[00:00:21] Yeah, you've probably used it. If you've written any sort of Unix or even a note application you use these things too. There's things called standard streams, they're standard output which is stdout that you'll see it written as that. Standard input stdin, and there's standard error, stderr. It's great because this is the standard for almost all Unix applications.
[00:00:45] There's something called the POSIX standards, which, I don't remember what POSIX means, but it's in the notes. Portable Operating System Interface. So it's a group of people that decided, hey, if you're writing any sort of Unix application, there should be some sort of standard way that inputs come out or inputs go in, outputs go out and errors are handled.
[00:01:04] And you've already seen in call backs everywhere if you've written any sort of node application, you'll see it's always send it in, send it out, send to error or something along those lines. And it is really awesome cuz it highlights what we're talking about yesterday how the Internet is build on trust.
[00:01:19] And trust and cooperation which is amazing. Especially in today's world where we all disagree about what is the best burritos. And where's the best place to live in the world, things like that. But you know what we all agree that everything should have standard out, standard in and standard error.
[00:01:34] And it makes programs really, really readable. That means any sort of Unix program that I'm running, I can pipe the input, I can pipe the output, I can pipe the errors to a standard place. And they're all handled and I know that for a fact it's gonna work cuz they all share this common API.
[00:01:53] And what we do with these inputs, outputs, and errors is we use something called redirection. You saw yesterday when I use the pipe operator. So the pipe operator is a good example of reading from standard out, because I run something and then all the output I pipe it to something else.
[00:02:10] Let's do an example of that. Just go and log in to your server if you're not already on, let me clear this.
>> Jem Young: And what do I wanna run? I'll say, ps, nothing happening. Ps is just showing me my running processes. But if I wanna write this to a file, I can say ps.
[00:02:32] And actually I'll do a search for it. And then I'll do grep to do a search, and I'll say bash. That was it. So it took all the output from ps, and then it just wrote it over to the grep program. And then grep is gonna do a general regular expression on it.
[00:02:47] And then it just searches over everything I want and returns the line. That's a redirection operator. So we could do lots of things with redirection. We can write the files, we can read from files. I can do some really complex things that you're like that's not real code.
[00:03:01] This doesn't make any sense. But it does, I can. But I decided to leave out those examples because they're just cool looking and they don't actually do. They won't help you in your day to day, so I don't wanna go down that route today. Another one you've seen is the error operator or the greater than operator, or that arrow if you will.
[00:03:25] It will write to a file. So rather than typing it from one side to the other, it's just gonna write to the file, if you want. This is super useful for writing to a log file. So I'll create a fake log file. So I'll say touch. Actually, let me make sure that I'm in my home directory.
[00:03:43] Close that out. I'll say CD and clear. And I'll say PS- Actually, let me clear locks out, touch test.log. I'll say PS and I'll write to test.log. And nothing happened because the output was being gobbled up by writing to this log file. But if we kept the log file, we see that it just wrote all that output.
[00:04:15] Now a lot of times what you're gonna do is you're gonna take output and you're gonna write it to log files. And what's great about that is you type all the log files that are commonplace. Do you remember where the log files are kept generally? Nobody ringing a bell?
[00:04:31] It's okay. Slash bar slash log is the economical place where you always keep all of your logs. So we can say, actually let's take at our log since we haven't looked at that in a while.
>> Jem Young: You see all the logs for all your services are always gonna be written in the same place.
[00:04:48] So generally if you're doing any sort of logging you always want put it here. So you see there's an engine x log, there's off the log. Let me see if I can sudo ls and /var/log. Yeah, it didn't show me anything new. But the writing to output is probably one of the more common ones, and also the piping one.
[00:05:12] If you've ever seen people who are good at their Unix fu, you'll see they just chain things over and over as they go from AWK and their crap. They might run a Perl script and they'll do just amazing things on the command line using the redirection operator, all because we know that there's a standard in, standard out, and standard error.
[00:05:30] And I really love this because it shows the power of just having a common API. And when you have this well established pattern, you can do such amazing things. Now if you wanna just, the problems writing to a standard out file, it's gonna blow away whatever you had before.
[00:05:44] So if you want to attend it, you just do the double, greater than, see. Yeah, I got I got a chuckle. Thank you, Zach. I appreciate that. If you want to read from Saturday and we just type it the other way. So we can take a file and type it into the the program that we're trying to run.
[00:06:00] And standard error is a little different, it's the two with greater than. It's a little weird, but I think they ran out of symbols honestly. I think that's why it's a two. Weird useless fact, but you might see it. If I wanna write a standard out, it's actually a one greater than.
[00:06:14] And if I wanna read from standard in, it's actually a zero less than. But I didn't put that in there, cuz you won't see that format that often. But in case you're like, man, I really love full sack I want to look things up. You might see that syntax somewhere, and then the two greater than the standard error.
[00:06:28] So it's actually 0, 1, 2. So its standard in, standard out, standard error. Makes a little more sense now now you see the two greater than? Yeah? All right. Some heads, I know it's early and I just jumped right in. Don't worry, I'm gonna start off here and we're gonna slowly ramp down and then I'm gonna lie to you, and then we're gonna ramp back up and then it's gonna be a great day.
[00:06:53] All right, so what does this do? Just to show the anybody, anybody at all? Yes, Sam?
>> Sam: Takes foo and it writes it to bar.
>> Jem Young: Exactly. You'll see this quite often though, so later in the course will probably do something like this. And I want you to be familiar with the syntax.
[00:07:10] But if you ever need to reference, this is pretty straightforward, hopefully