 All right, we're gonna be learning a lot of useful details about shell scripting today Originally, I already recorded this video, but the thing is I didn't realize how dense it would be So we're actually gonna be talking about a couple things now Originally, I was just gonna do a little video on a little status bar module that I put up here a couple weeks ago Basically, I wanted something that reads network traffic and of course to do that I had to learn all right How does a Linux system store network information and stuff like that and how to manipulate it efficiently? But there are so many little details. Let's let's look at the script Okay, but there's so many little details that are worth talking about that are the go to make this script a little dense So let's let's just learn you're gonna learn stuff that you don't know I guarantee you I'm pretty sure I don't know Okay, so here's the deal. I wanted to make a little script. Here's what I wanted my script to do Let's say I run it every five seconds or every second it could be in or any interval. I wanted to say How much network traffic have I had on my computer either downloading or uploading? In since the last time I ran this thing So if I have it running every second, it's gonna show me the bandwidth per second or if I have it running every five seconds It's gonna show me the bandwidth over five seconds or a minute over a minute stuff like that So this is what it ends up looking like I actually made some changes to this today This first part doesn't matter. It's just the I3 blocks thing that makes it clickable But so here's the deal here's what you need to know first off about the Linux file system before we get into the shell scripting goodness Okay, so one thing you probably already know I I assume a lot of guys who use the command line should know this If you go to CIS class net and look at the contents of that directory that actually lists all of your Internet devices, so if it starts with W, it's probably a Wi-Fi device if it starts with E It's probably a you know ethernet device, right? So within these this is like a couple videos ago where we talked about battery information It's the same thing within these directories. Let's look at the Wi-Fi directory It actually has a whole lot of information about it. So, you know carrier. Let's look at carrier Let's see what that is. Okay, let's see what let's see Let's see if there's anything interesting does it have like a well You can look at for example upper state, which means is it up or down is it working or not now specifically for our purposes There's a folder in here called statistics, okay? Don't cat it ls it and it has a cup it has a lot of information about network traffic Specifically this there are two that we're gonna be looking at in particular one is Rx bytes Okay, so I'm gonna cat that out and you're gonna get a number Okay, and there's also tx bytes. Okay now what these two things are Is that they tell you your cumulative network traffic? Rx bytes means how many bytes you've received, you know, you basically your download Okay, tx bytes is how many you have transmitted. Okay, that's basically your upload. All right ignoring some details I suppose So that's what those are So that's actually all you need to write the script at least all the the system information you need You don't need any silly dependencies or stuff like that. So that's that's what I was working with I want to use this and again if I check it Let's say I check the rx bytes the received bytes the download bytes again You'll see that it actually has gone up a little bit because in the background. I've been you know Maybe downloading something I might have a script running or there's some other network traffic going on Okay, so that's that let's get this show on the road. So here's my idea for how this works This is probably not the most efficient way to do this That's one of the reasons I do videos on this because people will be like no That's not the most efficient way to do this do this But anyway, here's how I have it working now What I do is basically every time this script runs, okay I'm going to be reading this information And at the end of the script, I'm going to be writing it to a file Okay to store the previous information and every time the script runs it's going to check okay Here's the data now here and let me look at the data We had a couple seconds ago whenever we ran this last And let me see how much change there is and that is going to be the network traffic and it prints out that So let's actually go through what this is doing. I mean that's the idea about it But let's go through what this is doing now as I said there is a log file That I keep previous you know like the last time we ran it how much data we have transferred I keep that in net log and notice what I'm going to do is I'm specifically going to take our Received bytes our rx our current rx values and our current tx values set to these variables And I'm going to put them in that log file So it's going to look like the rx values space tx values two columns so to speak Okay, so when this script starts again, this stuff up here is only to do with the you know clickableness of it So ignore that When this script starts we are going to read data from that file and we're going to put it into prev data Previous data, you know, however you want to think of that one. Okay So that's the lab data from last time we ran this file now What we actually well then we have to do what I just talked about right read the current Information from the actual locations in sys class net whatever statistics So to be clear first off, I have a little regular expression a little glob here specifically You know here. I'm looking just at my wireless Okay, but really I want all network traffic from whatever wireless or whatever internet devices I have and of course on different machines If you have a different wi-fi Device it's actually going to appear as something different. You can't just hard code it It's not like that So these three numbers if we run that with that these three numbers Correspond to our three different network devices here. Okay So that's all that is now the only tricky thing about this is we want we don't want three numbers We want one number. Okay, because we want to monitor all network traffic and we want to add that up You know, we can't deal with just three numbers So what I do here is I actually put them first off I want to put them So that I can do shell arithmetic on them. I want them to be like number plus number plus number So what I do here is I actually pipe them into the command paste Okay, so I pipe them into paste and paste usually it's for putting files together But you can actually with the s option just have it do a line by line thing Using the delimiter plus and this is how that's going to look work Now first off, I didn't actually know about paste until a subscriber opened a pr originally I had like said or tr doing this kind of stuff or said and tr which is wasteful If you can have one command doing it. It's always better for that At least if it's a good command, right? But so paste takes these numbers and it puts it sort of like, you know, so it looks like algebra Okay, so why this is good Is as you know or as you may know you can Well specifically, okay, so we can echo this out as a variable But we can also put all of this stuff within To we can do the arithmetic thing in the shell That is we can have a dollar sign and two open parentheses And then put two closed parentheses at the end of that and that just means treat the stuff is treat the output of whatever This command is treat it as math and show me what that is and now I'm just echoing it So if I run that I actually get instead of these three numbers, they have been summed up So we have the thing we actually want we have our current network traffic Okay, so again, we have our current network traffic Sint and received and we've already gotten our previous network traffic Now we just want to put them all together in one command and that's what I have here This line, you know this command here though. I will say it's very dense. There's a lot of stuff going on Now let's look at okay, so this is a print-off command. I'll talk about that in a second Let's look at the things that it's printing. Okay. Here's what I'm doing here This notice that this is arithmetic as well. It is the dollar sign plus two parentheses Um specifically, let me space this out so you can see it a little better Let's look at it for what it actually is. Okay Let's I don't know. Maybe this is a bad idea because I'm going to mess it up But okay, so this is what's actually going on It's going to evaluate this as a math equation It's going to take the rx current variable and it's going to subtract Prev data with all of this stuff. What is this? What exactly is going on here? Well, this is a string operation that says, okay, take the prev data Uh variable, which if you remember, that's the data from the log file That has the rx that are like our previous log file from the last time we ran this That has the um Received bytes and the the transmitted bytes separated by a space. Okay So what this thing is is a string command saying, okay, give me the prev data A variable, but I want you to get rid of The second the space in the second column. That's basically what this is The the um Percentage signs mean find whatever matches this space plus, you know a glob match and delete that That's basically what that means. Okay. Now. Why do I do that instead of said or awk or something like that? I think I said earlier. I mean if you have if um If you have something built in in your shell to automatically do stuff like this It's usually better to do that rather than calling an extra program to do it And when I was doing this, I was thinking, okay, I got to optimize this. So That's what I did. Anyway, all of that is to say this is the previous amount of uh bytes And this is the current amount of bytes. So if we subtract we find the difference between them children That gives us the change in bytes the actual network traffic And since I want that to be, you know, I divide that by a hundred and or a thousand and 24 So we actually get you know, it get it in kb kib whatever Um, so that's all this is this is actually a very complicated looking thing But this just does math it says, okay, uh, and I should be clear these um parentheses here, right These are parentheses like math parentheses. Okay, they're saying find the the difference between these then divide it by 124, okay Very dense stuff going on here, but that's what's happening So once we find that once we get that uh math equation We have it run arithmetic on that. So this thing all this thing in here It's going to be a number. So let's see. I could delete this oops Uh, it is going to end up as being some kind of number. Okay, all of that's going to evaluate to a number And the same thing with this so this is going to be our received bytes. This is going to be our transmitted bytes Whoo, okay So now what what's the printf command for well the printf command just formats what we have and it inputs them in here as uh Strengths, okay, that's it. Actually, I probably wouldn't necessarily need printf I think originally I was using like only use so many digits. Uh, it's better to use printf for that But uh, anyway, that's that's that I could probably just echo that. Um, so anyway that actually prints out the uh, you know, what what actually is going the the data Okay, and the last thing is we actually echo that our new information to a file the log file We talked about a while ago. Okay, so summing up. Let's rehearse this. Let's let's rehearse all the things we've learned Okay, um, we've learned how to find network information specifically network traffic On any kind of linux device or I guess it works on other unix based operating systems I'm assuming they're more or less in the same spots. I'm sometimes different distros put things at different places But we now know where that stuff is. We know what rx bytes and tx bytes are We know that that is all network information And you should also know that there are many other kinds of information you can look at you can look at dropped Bites you can see, you know, you could have something monitoring to see if you're Losing traffic or something like that So you've learned where that stuff is Um, and so we made a little script. We talked about a couple the different things Cadding all these files and then pasting them together. We've talked about using shell arithmetic to add them all together And then I guess we talked, you know what I should probably do a whole video on these string You know manipulating strings and stuff like that because I think that's sort of a complicated topic That's worth talking about but anyway So again what the script does it reads the last time you wrote you you ran this file how much data has been You know transmitted It compares that to what's Presently been transmitted finds a difference between those prints it out as Your network traffic and then writes the new information to that file So next time you run it. It's going to tell you what the difference is. Yeah, that was a dense video I don't know. I feel like that that's the thing when you're when you're writing a good shell script It should be something where like every line is like extremely complicated I don't want to say extremely complicated. I think once you know what you're doing you look at this and you're like, okay Okay, I get it But there's so many little details that I feel like I got to talk about because uh, you know I haven't talked about them on videos before you know what I I sort of feel bad for the people who just randomly find my channel and think it's like super complicated stuff It honestly isn't I just sort of assumed that everyone who's been watching has been watching every video in the past But um, all right now. I'm just rambling. I'm gonna turn this thing off. See you guys next time