 Alright, let's do some shell scripting and let's also learn how batteries work on Linux or at least in the Linux file system because I see people doing funky things if they want to script something with their batteries. They'll use extra modules you have to download, stuff like that. It's way easier once you know the actual Linux system directory structure and how you can take advantage of that. Alright, so here's what we're going to talk about in this video. You'll see up here, I have a little battery icon that I've actually made a little bit bigger for the purposes of this video, but I can run it on the command line as well. Here's what it does. It actually prints out the two batteries that I have connected, ignore the little emoji problem there, that's with my terminal. And it prints out their percentage capacity and it prints out a little icon basically saying if they're plugged in or not. So for example actually I have my cord right here so I'm going to plug in my cord and show you what happens. If I plug it in and I rerun it, you will see that one of them now has a little plug next to it. And the same thing up here so my status bar has actually changed as well. It reruns this command every couple of seconds. So let me show you how I did this. I have a little script for it, it's already on my github, but just basics, let's talk about how to actually access your battery information on the command line. It's simple. If you go to sys class power supply and look at the contents of that folder you will actually see all your battery status or at least a list of all your batteries. Now I actually have two batteries hooked up right now. I'll show you. Let me actually take this off. Give me a second. Okay. So I have two batteries right now. Now I have one because I took off my thinkpad slice battery. Now you'll see my little icon up here has now reduced to one battery. If I rerun that script you will see I only have one or let's look at the contents of this directory, you'll see that it has now disappeared. If I plug this battery back up, let's do that. Let's plug it back up, okay. Now it is going to reappear and again if I run the battery command, bam look it's still there. Alright so let's talk about how you actually write a script like this. Now to be clear, it not only lists your batteries but these batteries if you look at the contents of these directories it actually has a lot of information. All of this is generated automatically about your battery. Everything, you know it has a serial number, manufacturer, stuff like that, a bunch of stuff you never think you'd, so let's for example look at the manufacturer. I don't actually know what it knows about it, okay. So it seems to know stuff about the battery status and stuff like that. But for, let's say if you want to see you know what kind of capacity you have left, well look there's actually one of these that is actually capacity and if you look at the contents of that file it has 48, that means my battery is at 48 percent. You'll see in the status bar module it says 48 percent, right. Or let's look at the capacity of the other battery, oh look at that it's at 97 percent. Additionally it has a little thing called status and if you cat out status, well and this one it means unknown that usually means it's stagnant, it's not increasing or decreasing. Let's look at my other one. It's discharging, it's actively discharging because we're not connected to the power right now. But if I plug this in, let's plug ourselves in, okay. If we rerun that it's gonna say charging, okay. So here's what I do in my battery script. I look at only two things, well it goes through all the batteries and I'll show you how I do that in a second and it gets the capacity which is gonna be the percentage left and the status of the battery. If it's charging, if it's discharging and what I do with that information is if it's plugged up and it's charging I want to have this little plug icon. If I take this off I want it to just show a battery because it's discharging or stuff like that, okay. So let me show you the actual script and I'll talk you through it. It is, I just have it named battery, I had opened this before and didn't save, but here's what it looks like. Okay, so first off, this little part up here we can actually ignore it as to do with i3 blocks, it's basically so if you click on the script it'll show information about it. But here is the rest of the script, okay. We have, first we go through a loop, all right. We have a little for loop here and it says for all of the call it batteries in this location that is sys class power supply bat as we just said that is the location of where all your batteries are gonna be, okay. Now the question mark here, this is a glob matching kind of regular expression kind of thing. This means all the locations that are capital B, A, T plus another character, the question mark is a stand in for any other character. So if I, if on the shell, if you do something like if you try and cat out bat question mark, it will try and cat out all of the replace question mark with any kind of character and that'll be what it's looking for, okay. So basically anyway that's just to say we're looping through all of our batteries, that's all that is. Now for each one of them it's gonna do the following commands. First it's gonna get its capacity and it's gonna set that it's gonna get the slash capacity of it. So let's actually see what that would be in the case of bat zero. Well that's gonna be 49 right now and it's gonna set that equal to the variable capacity. It's gonna do the same thing with the status. So it is going to take status of that and it's gonna see okay it's charging. So it takes that information and I take one, there's one more thing that I do and that is this is a request from subscribers who used my dot files. Let's say if you have really low battery, let's say we're not plugged up, I'm gonna take it out, you're not plugged up and you have a battery that is really close, it's like 25% or less. You wanna have an extra little notification, a little warning that you're going to need to plug it up. So this line right here, this is another example of instead of using if statements using very terse code here, we check okay if our status is equal to discharging, again we just took status from the status thing right here. If that is equal to discharging and if the capacity of our battery is less than 25, we're gonna make a variable called warn and that is gonna be equal to a exclamation point. And basically I'm gonna print the contents of this variable out in the final command. Now it's not showing up here because my batteries have good capacity right now. But this is just an extra little warning that I'm gonna put in here. Now how I actually format each of these batteries is with this printf command, which is actually a whole bunch of, it's hard to read this command. But let's look at what printf is actually doing. Now printf, this is how printf can be a little difficult to understand if you're a newbie, if you haven't done programming before. But basically you just give it something, you could print some arbitrary text, just as an example. Okay, so if we run that script right now, it's going to, well, I'm actually in read-only mode, I'm not even gonna bother. Okay, so let's do that. So if we can give it arbitrary text for it to print, so it's gonna print all this extra stuff out, or we can give it strings. Meaning that's gonna be, basically I'm gonna give you a variable, put that variable in, or some kind of command or something like that. Put that in that string, okay? So the three things we're gonna put in there is we're gonna put the battery capacity, we're gonna put the warning if it exists. And we're also gonna print the battery status. But here's the thing, I don't want it to say, just I have this complex looking said command, because I don't want it to just show the status. I actually want it to replace the status, instead of having a long word, like charging or discharging. I actually have some said substitute commands that replace, for example, we substitute discharging with this little battery. Or we substitute not charging with this little stop sign. Or we substitute the sequence full with this little, I guess, electric bolt or something. And that is just so I don't have all that junk printing. I have just little icons, little emojis instead. I just find that is a lot more readable. And I like the garishness of emojis in my status bar. I know some people complain about them, but get over it. That's what I do. And it's easy enough to do that. I just replace, I just have a said command and I replace all of those sequences with that. If you want to know more about said replace commands, I might link, I might do another video on these or substitute commands, I should say. But I did a video in the past on this, maybe I'll link it at the end of this one. Okay, so anyway, all this is to say, we print out three different strings. The first one is this capacity or the status command where I have replaced all the charging, discharging, I've replaced all of that with some emojis representing them. The second thing it prints out is the warning icon. So if we set the warning icon because our battery is low, it's going to be an exclamation point. Otherwise, it's just going to be nothing because our warning icon is not set. And then the last thing we're going to print out is capacity. Notice here I echo capacity into this said command. And what this said command is doing is actually pretty, it's pretty straightforward. It substitutes an end of a line, substitutes the end of a line with a percentage sign. And that's just because I want a percentage sign at the end. By default, when we cat it out, if I go back up here, if we cat out the capacity usually, it's just going to have a number. But if we use a said command like this, if we say something like this, so said, substitute the end of a line with a percentage sign, it is just going to print that, oh, I actually had two. Oops, I just accidentally put in the wrong character. Okay, so there, there, now we're going to have it printing out what we want. Okay, so all of that looks, it's actually very simple, very quick running because it just, this script, it just loops through your battery, it gets their information, and it prints them out. There's no extra modules you need. And of course, if you want something way simpler than what I have, I mean, let's say you want to go hyper minimalist, you want to want a status bar that just prints out your one battery's capacity. Well, that's just one command, right? That's just sys, class, power supply, bat, zero, if you just have one battery and only one battery ever, and then print out the capacity, that's all you have to do. I just like the little flourishes. And again, these flourishes you're getting for free, but I mean, nearly for free. It's not very intensive to run a couple said commands on this. All right, so that's about it, that's what I do. And the mindset, I guess, you know, I used to use more complicated things for my batteries until people of course pointed out, oh, why don't you just use the system internal things. But the reality is the importance is, you know, you just want to be mindful of what your system has in it by default. And this is just the example of batteries, maybe we'll talk about some other ones soon. But anyway, hope you learned something and I will see you guys next time.