 Hello and welcome to a video from filmsbychrist.com. I'm Chris. That's Chris with the K there's a link in the description and I'm gonna have to explain this video This this is let me just show you what it does the script does Then I'll explain certain scenarios where you might want to do something like this and then we'll look at the code Okay, so I'm in a directory with a script. I just called bar.sh. It's already executable I can dot slash to run it and now it says welcome and at this point I can use either the s key to add an Equal sign to the line or an a key to subtract The equal sign it'll let me go down to one and if I hold down s You can see it's drawing all the way out and I have it set to stop 10 characters from the end of the screen Okay, why would you want to do this? So just a scenario where maybe you want to have a bar that represents a power level or a volume level I have for example a controller knob It's just a knob that has a USB plug that I can program to do anything Maybe I want to write a shell script that it controls the brightness of the screen or the the volume and I want to display a Bar a power bar a percentage bar using you know just equal signs or other characters in the shell Well, this is how you would do that. So that's that's a scenario would do this But also it's just this script's gonna help you build your skills There's things in here that you would use in different cases You're gonna learn a few things. So let's go ahead and look at the bar sh And there'll be a link in the description to the script online and the script Probably could be cleaned up a little bit again. This is a script. I wrote years ago anytime I look at scripts I wrote years ago I'm like I bet I could shorten this up, but you know what shortening up doesn't always make it more readable, you know for people who are trying to learn how to do stuff so first of all our script starts off with our Shabang line that tells your system use bash. This is a bash script. Okay, and you can see I wrote this back in 2018 I'm finally getting around doing a video on it. I on my copyright notice there, okay GPL license so just use it however you want as long as it falls under, you know the GPL guidelines Okay, we're gonna set a variable B. We're saying B to zero. We're saying let B equals zero That's just one way in bash to say this is a number because we're gonna do some math on it Okay, then we're gonna have our main function which is called at the bottom of the script Technically, I don't need to put this into a function for the only thing in the script But if you want to use this functionality in other scripts, you could rename this bring it into a script and then just call that function It's just keeping things clean We're gonna print welcome to the screen and then we're gonna start our main loop here So what are we gonna do? So the I think the only external command outside of bash that we're gonna be using is tput To get the number of columns that there is a way to do that natively in bash I'm pretty sure I have a video on that but a lot of people when you look this up the use tput tput is commonly installed on systems, but not always almost always it's in your repositories But in this case, it's just giving us the number of Characters number of columns or the characters across a screen because that number can vary depending on how big your window size is What font size you're using? You know stuff like that. So it's going to say get the number of columns I'm in this case. I'm saying run this command tput columns and Subtract whatever number that gives from 10 and I just did that to do math just to keep it away from the edge of the screen just because Just again learning example, they're going to use the read command now the read command You've probably seen and used a lot. It's to get user input lots of times You just say read you might give it a prompt and then a variable and then whatever the type user types and hits enter It puts into that variable well here We're going to give it some arguments which is basically saying only accept one character and as soon as they hit a character on the Keyboard continue. Don't wait for them to press enter. Okay So anytime you hit a key on the keyboard, it's going to continue. So let me show you. Let me exit out of the script Let me run the script and you can see I'll hit K Right L J H K J I oh nothing's happening, right? I can hit S and it adds a and it subtracts So let's continue our script and see exactly what's doing there. So it's looking for one character And then it's going to echo that one equal sign which happened when I hit the initial K But then we're going to go, okay We only have two options that we want to look for if it's if X the character that's hit is a Well, then we're going to take our B from the top here Which starts off as zero and we are going to subtract one from it if it is S Then we're going to add one from it to it Okay, but then we need to check Okay, B if it's less than one Set it to one that's setting a minimum so we don't go to zero or negative numbers And then we're going to say okay is B greater than the number of columns Which is the number of columns or characters minus ten in this case if it is then just set it to the number of columns That makes us so we can't go past the max. That's called clamping. So we're clamping it from one to however many characters We're allowing on the screen. Okay now this command echo Whoops echo dash n e and then inside quotations back slash R. What are we doing here? So n means no new line character because normally with when you use the echo command it automatically n goes to a new line Carriage returns and then he says, you know, look at these as special characters. So we're doing back slash R It's not a back slash R. It's saying do something special basically what this is saying is go back to the beginning of the line Don't start a new line go back to the beginning of the current line Okay, so we've checked what keys were pressed we check whether they're in the minimum and maximum Then we're going to go back to the beginning of the current line and then what are we going to do? Well, now we're going to again look at the actual number of columns on the screen and we're going to loop through that and just print a Space character with no new line character. That's erasing anything on the current line basically. It's not really racing. It's putting space Spaces on that and so what that does is when we are adding the equal sign when we start moving the other way It makes sure that it clears out Anything passes basically just clearing the line. That's what we're doing here We're going back to the beginning of the line and then we're clearing the line just by putting spaces across the entire thing Then once we've cleared the line. We're all the way at the right side of the screen We're gonna go back to the beginning of the line So all the way to the left side of the screen and then we're gonna loop Just I as an integer. It's just a variable. We're creating Doesn't really matter because we're not even gonna use it we're gonna say for this and then we're gonna use the sequence command and we're gonna say start at one and Loop however, whatever be equals so it can be anywhere from one to however many characters can fit on the screen And what we're gonna do is we're going to print the equal sign without a new line character. I Hope that all made sense it will loop forever once the loop is broken somehow We will exit out of the script so again We run that and I can hit s and if I hold down s it goes all the way in and you'll see every once While the cursor you'll see it flip around. That's just computer running a little slower and you will see I'm not sure if it's picking up and recording it happens occasionally You'll see it flicker and that's it jumping in the beginning of the line printing all over the end of the line Jumping in the beginning of the line and then jump printing out to where you are But yeah, that's how you can print a Line of characters on a screen with a key press or some sort of input again like a controller knob I'm really trying to think of what to call this video like a bar output level output But yeah, so if I have that knob I could be controlling the volume But also showing the output on the screen of what the volume level is based on that or something along those lines again I don't see this you using this exact scripts You know a whole lot, but the techniques we're using it can be useful in many scripts I just thought I'd show give a demonstration of that the the script There'll be a link to it in the description of this video so you can grab it and play around with it I do thank you for watching and as always visit films by Chris calm. That's Chris the gay There's a link in the description. I hope you enjoyed this tutorial Hope you found it useful visit my website search through my videos check out my patreon page I also have Libre pay and PayPal if you want to support me that way like share subscribe and comment And as always I hope that you have a great day