 Okay, welcome back. I'm turning right where I left off as you can see our timer is still going Let's clean up our code a little bit and then clean up our output a little bit So I'm going to control C to get rid of that Control L to clear the screen and then again, I'm using Venom as I text editor, but use whatever editor you like And so they have been trying to do more with my bash scripts and lots of times the bash scripts You don't do this, but you know, I've been working with a lot of other languages And I really like, you know cleaning things up into functions, especially a short little script like this It isn't really necessary But let's go ahead and create some functions. It helps prevent any You know repeating of code and what I'm going to do is up here I'm gonna go like this. I'm gonna say function main This will be my main function and you don't have to write function like this You can just write main in the parentheses here, but I like to write function I just think it looks a little cleaner I meant indent there Okay, and now we're gonna call that there, but we that creates the function We want to call it somewhere so flower script. We're just gonna say main so it will load everything and then Start in our main function here. So we'll have our little message here and then our while loop I'll put in here I'll say function start Timer and put all this in a function indent everything properly Now up here we will say Start timer the function and Then we will Exit zero meaning that we exited properly if we ever get there, which we never really will because we're in a loop But it's good to have it there Okay, so now if everything is correct We should still be able to run this and really there's no difference to how it outputs It's just we're cleaning things up a little bit because we're gonna add another function right now so Let's go ahead and think about this. So right now we're counting seconds So after a minute it's gonna keep counting seconds and after hours still gonna be seconds It'd be nice if we could format things so that they display nicely with 90 seconds a second column a minute column an hour column and even a day column So let's go ahead and look at that. I am going to Save typing it all out. I'm gonna paste a function in here that I have and 99% of this is just from sample code that I've used and found online a while ago I would give credit, but I don't think there really was any credit where I found it It's very basic, but I'm gonna walk you through it here So we have I'm calling this format time and then you would pass it a variable basically your seconds And that would be anytime you pass a variable to a script The first variable or argument is going to be dollar sign one Well, the same is when you create functions in your script So we're going to when we call this we're gonna call time format and then give it a number of seconds Which would be these seconds right here and What this is going to do is going to take that and then putting in a variable called no We could call it seconds, but we actually have that somewhere else. So it's just to keep it even From being confusing. We're gonna call it no And then we're gonna set our minutes hours and days to zero so that they start off at that And then we have a series of if then statements with some math here So we're gonna take that number and we're gonna check is it greater than 59 if it is Well, then we're going to take that number and we're going to give it this percent 60 and what that's saying is it's gonna take that number So our seconds divide it by 60 and give us the remainder and That's gonna be our seconds column Then we're going to take the what's left over when we're gonna take that number and divide it by 60 and That's gonna give us we're gonna replace our num with that and then we're gonna take that and again see if it's 60 or more or sorry. Yeah, well, yeah greater than 59 or 60 or more you could say You know equal or greater than 60 or to say greater than 59 same accomplishes the same thing And again, now we're going to divide it by 60 and get the remainder and put that in our minutes column And then we're going to take our number Which we reassigned here and divide that by 60 and put it in the num So basically we're chopping it up By 60 and then the let's left or by 60 and then next time we're going to do it by 24 because There's 24 hours in a day So Then We have some else statements, you know in case those things don't get their values We're going to give them the numbers here all the way down for hours minutes and seconds And then I'm going to output just like we did before I'm going to say echo Dash e and the end means don't print a new line at the end of this and the e is saying hey look at this special Backslash r here, which means to go back to the beginning of the line So it's going to over at the line and then instead just outputting the seconds It's going to output our days value hours value minutes value and our seconds which by default All these except for seconds because we're going to start with some set with zero seconds after doing our math here We're going to have zeros but then once we break that 59 second all sudden minutes will start being updated and once the minutes past that 59 minute barrier then the hours will so we do that and Again, that's in a function called function time or sorry format time So really we don't need this Because our function is going to output that I could return it would be a more proper way probably to do it What I'm going to do here is just paste in that and we're going to give it the seconds and That should be it that's going to clean up our output So there we go It could would be nice if we put some placeholder zeros in there I think that would be nice So that would be zero zero zero zero zero zero and then like zero one zero two zero three here and then once the first but We could do that. I'm not going to do that in this video though because this is good enough So yeah, so we've cleaned up our output a bit and it's not just displaying seconds You're going to get real time. I'll let this video run for another 30 seconds at least so you can see when it flips over But again, I hope that made sense the math in there basically we're checking whether the seconds are greater than 59 and dividing that number of seconds by 60 and Getting the remainder and that will then be our seconds from there on out and then taking our number and dividing it by 60 and Replacing the number with that and then checking are the minutes over 59 Which is the num now and so forth and so on so here in a second. We should get one zero. Oh Why did it go to nine? Oh? That was just Okay, let's do that. Let's let's see if it does that again And that's that that so okay. Yeah, it's going to do that again I didn't realize that so because of the way that we're clearing the line It's not really erasing the line what it's doing is it's going back to the beginning of the line and rewriting it and so the When we update now that caught that rose not going to be as long So that last number of nine is going to be there until I hit a key or the number grows So we really should put placeholders zeros. So let's go ahead and do that. I Was just about to Actually look up how to add the placeholder zeros, which I'm still going to do here But I just realized a quick way to actually clear the line in a way by putting spaces there You can actually do backslash B a number of times is Backspacing but another option would be to if I put another R here and put a bunch of spaces That are longer than my output what it's going to do is it's going to go to the beginning of the line Print a bunch of blank spaces and then go to the beginning of the line and print my output here so if I do if I run the script it looks the same at this point and Actually, just to make things a little bit clearer. I probably would put these on two separate lines It's going to accomplish the same thing I just think it will look nicer plus gives you the added benefit of Making sure you put enough spaces to that it's longer than your output there Which it definitely will be because these take up these variables are taking up more characters than the seconds will or the Total time will the only thing it's going to get more than two digits would be days and it would take days and days It would take it would take months to for it to accomplish that But right now I should be able to run this script and again, this is one of those things as a program you do and Off the bat it doesn't really seem to make a difference. The end user isn't really going to notice it We're gonna let it again run here, and you'll see that as it gets to the 59 mark and then it flips over to the next minute what it's going to do is it's going to Actually every time what it's doing It's going to the beginning of the line printing a clear line going to the beginning of the line and printing your new output I don't know if it's the most efficient way to do it But you'll see that nine that was at the end of the line before Should not be there because we're actually overriding it with blank spaces So there actually will be a blank space there, which there is actually every time we're running this loop here You just don't see it because it happens so quick But we're almost there So as it flips over from 59 Back to zero There you go. We didn't that nine ding gets duck there Let's go ahead and add in those placeholder zeros Which you're gonna have to give me a moment to to look up I know it might have to use print F is how I'm familiar doing it And then you do something like percent the D and the number of characters you want give me a moment Okay, so I figured things out And actually we're gonna rewrite a lot of our script here because well, let's get to point number one Let's add some placeholder zeros here. So we'll go back into our script here. I also want to mention You notice earlier I said there's multiple ways to do math in Bash and I use the let which seems to be less used than using these double parentheses And it's just that's the way I learned to create integers 12 years ago when I first started writing bash script So that's how I use it and you'll notice down here I am checking things and then doing math inside these parentheses and that's because I did not write the script this function I mean But let's let's just get to our placeholder zeros here, which again I don't know how to do it with that. Well, I've done it with print F before so I checked and What we would do is Let's go ahead and move our line Eraser up here. So the backslash Backslash are so I'm still going to use echo I could probably I know I can do all this in one print F command But off the top of my head. I don't know how to do that, but we're gonna say go to be in a line Write a line of blank spaces and then go to the beginning of line again They're going to come down here and if we do print F and So sorry to look at this and one option would be Way I started off was percent to or sorry zero to D and then close those quotations and then if I put Spaces between each one of these and remove that last quotation mark what we'll get You'll see is not quite what we want, but I'm going to run it and It's close. We have our placeholders here spread saying timer timer colon something. Okay, so it's going to print everything multiple times Let's go ahead and just for now remove the timer and put in the semi colon there and that works But we have this last little semi colon there and I don't like the way that looks So then the next thing I tried is I put back in our timer colon and then here I put Because we have days hours minutes seconds. So for we have one two three four Erase that and we should get the output that we wanted and there you go, but There's a better way. Let's turn our format function into a One-liner how about that? So let's go back into our timer here and what we're going to do is we're going to erase this entire function except for those two lines that we just had for our output and I'm killing people who use Vim like me that I just held down D rather than typing in the line numbers that I wanted Anyway, I just know someone's going to complain about that. Let's go ahead and pull those lines And paste them in here and dent everything We'll get rid of our Format time call there and what I'm going to do is in here What I'm going to do is I'm going to instead of doing Hours days blah blah blah. What I'm going to do is I'm going to say in quotations dollar sign and double parentheses here and In there, I'm going to take our seconds and divide it by Eight six four zero zero Which is the number of seconds in a day and then again in other quotations double parentheses with a dollar sign and I'm going to see it say dollar sign or just seconds divided by 36 zero zero percent 24 and Some of you are probably seeing what I'm doing here and I should be able to do Again double parentheses here and the lines getting kind of long so it wouldn't hurt to break this down a little bit in multiple lines But let's go again seconds divide by 60 percent 60 and basically we're doing all our math on one line here so we're going to do is seconds and percent 60 so Here this is number of seconds in a day So we're taking the seconds and dividing it by that and that's going to be our day output Then we're going to take our seconds divide it by 36 and then 24 to get our hours Because that's the number of seconds in an hour. That's how many hours we have And then we're going to take our seconds divide it by 60 And then divide it by 60 with the remainders and then we're going to get our seconds and by that And if I typed everything properly that one line now not only gives us our placeholder zeros But should properly output our timer here I'm going to let it run here for a little bit. So, uh, yeah So I saw that when I went to look for placeholder zeros I saw an example of time output that was very similar to that. So that's what I ended up putting there and Yeah, that's a lot better than that big long function if you can shorten up your code and That's it. So hopefully I'll remember this will be up on my Paceman account if you go to films by chris.com up at the top you'll see a button that says scripts And then you'll click that and it'll bring you down to a script section Click on the one that says my notes and my notes will be bringing to a searchable page And you can actually search for the script on there You know just type in some of the keywords from the script like type in seconds or whatever and it should bring you Give you a list that'll bring you to the paste bin of this code Again, you can search through a lot of my example codes. There's probably almost 700 different scripts In that list So that's it go ahead check that out again I'll try to remember put a link to the the code in the description of the video, but If I forget for some reason go to films by chris.com. That's Chris the K link in the description And go to my notes and search there for this code. It's gonna be called Basic bash timer or something like that and check it out there. I do. Thank you for watching as always I hope that you have a great day