 Okay, I was recently watching another youtuber that does Linux topic videos And he was showing some examples of like looping through and making a timer with a Shell scripts and there was great. You know, it was great little demonstration stuff But I have some issues if you're going for accuracy and I'm not going for like top-of-the-line accuracy But when you're looping stuff, especially if you're running commands in between the timers like displaying stuff to the screen You can get off on time and so let's have a quick look at that we're going to use the time time or timer command the time command and It'll tell you how long a process actually runs for So let's use the sleep command as an example. So in here I am going to say Time or I'm gonna say sleep for one second. So maybe it's not time. It's just a time command Time sleep for one second as you can see it didn't really sleep for one second It slept for one second and one thousandth of a second if I run it again You'll see a little bit ran a little bit faster that time and I'll run it again And you see we're getting pretty close. Actually, it's being pretty good right now. Usually it's a little bit more It's maybe point zero zero four or point zero zero three off And you might say to yourself that's not a big deal in a lot of cases It's not but if you're looping over and over and over again You're trying to keep accurate time for a long period of time This can add up think about a hundredth of a second which we're only going thousandth of a second here But if you're a hundredth of a second off that means every minute and just over a minute and a half You're gonna be a second off and even though we're only a thousandth of a second here And that's pretty good over time that will add up and you'll be seconds and eventually minutes off Depending on how long you're calculating out this time and again We're looking at just the time for sleep if this was a loop the loop takes time If you are printing anything to the screen that's gonna add a little bit to it and over time your your timer It will drift off and not be accurate anymore. So how can you fix this? How can you get a more accurate time? And I'm not talking like scientific We're calculating out atoms and molecules and moving around specific to the millionth trillionth of a second But we can get a much more accurate time over time if we take a timestamp of when we start and then we can Calculate the time that has elapsed so while we're calculating and displaying the screen that might throw us off a little bit That is individual to each time we loop. It isn't accumulative where it adds to it So let's go ahead and just start writing our script here. I will make a script called timer and I will make it a bash script bin bash, right? So we got our shebang. Let's go ahead and make that executable so that it will run as a Program, okay, so the first thing we want to do is we want to create a timestamp. So let's create a variable Right here. It's going to say basically we're creating a variable called start and in it We're doing the output of date plus percent s We're just gonna give us the number of seconds since 1971 January 1st 1971. I think it is and Then what we would do is? We can create a while loop now So we'll say while and we'll make this run forever We're gonna say do and done and then here we can say to echo and we're gonna do a little math So inside these double parentheses with dollar sign We're going to echo out and I'm just going to say this so we're gonna run the date command With that same command or so that will get the current time in seconds and then we're gonna subtract the previous time in seconds from the beginning of the script, let's go ahead save that let's go ahead and run it and Yeah, it's looping through really really quick. I forgot to put our little sleep function in there So I'm gonna sleep for one second you can lower that number if you want it updated more rapidly than that But here's zero one two and you might say Chris We're still using the sleep plan. We're still looping but again each time it's calculating out difference So although we have a little bit of a delay there while it's calculating all that our continuous time is going to be more accurate And you know what if it drifts off it might skip a number in the display But that's just because we've shifted over and we're going to keep that accurate time throughout Let's go ahead and update this a little bit. Let's go ahead and add a clear command Okay, so we can do that And now it's going to keep it up at the top of the screen there Of course, we can make it look a little bit nicer if you're using figlet or toilet and again Yes, toilet is an actual command you can install. It's similar to figlet. In fact, I think that I don't know if they're even the same project now but I'm gonna say Figlet and I'm gonna give it a font and in this case I'm gonna say big mono 12 and now we should get a little bit nicer nicer larger We're getting larger numbers up on the screen and you see I think even there it might have skipped the number two I wasn't really paying attention And that's just if things drift off for some reason if if my system is running Let's say you are running a regular loop and you're not doing it this way You're doing it the original way where you're just sleeping and you're keeping accurate time But then you do something that bogs down your machine It can make your script get way way off where this if it was to bog down Let's say it was completely lock up for a while for five seconds when that five seconds went over It would jump to the right time not Continue where it left off, but we're good. We can do better. So when we kill our script and start it again It's starting over again. Let's say we want to keep that time I am going to come in here instead of just creating this variable like this We're gonna dump that time to a file. So and then we're gonna check it that file exists So let's go ahead and make it so we're gonna say if dash f So if this file and we'll I'm gonna put it in the current directory, but you would put it somewhere Where it would be stored. Maybe the user's home directory But I'll just call it timer dot SMTP or not SMTP SETMP for stamp you can call it whatever you want. So we're just gonna create text Well, we're gonna say if that exists, what are we gonna do? Okay, if that exists Then we're gonna set our variable start equal to whatever is in that file Of course, you may want to do checks to make sure that it's a valid Information in there and I should be using variables for my file name But I'm just gonna put it in here like this So it's gonna look at what's in that file if it exists and put it into our variable Okay, if it was to fail well, then we're just gonna do our command here where it gets the time But of course we need to update our file then we're gonna say echo dial dollar sign start and we're gonna put that into our little stamp file So what does this do if we were to run our script now and if I typed everything properly? It works just like before right so it's it's going through it's it's looping it's calculating out the time, right? And if I was to control C to kill that and wait a couple of seconds, and I was to run it again Hey, it didn't start at 9 or 10. It actually knew how long it's run even though my code wasn't running So this is a great way to keep time even if your code gets shut off And I'm working out of a temporary directory now But if I was to save that time stamp file to the user's directory You could shut down the whole computer you can shut down the whole computer for a year when you start it back up It will know exactly how long it's been Since everything left because it's not counting the time that's passed It's taking when it started and what time it is now and subtracting one from the other So if you're looking for long term keeping time, there's probably more accurate to it But this is a very simple way that you can do in any programming language to keep accurate time You can see I ran it again, and we're already at 60 seconds 61 seconds. So yeah, I'll put a link into the description I've done videos on this before but since someone else just recently did a video on that and his video is great I'm not criticizing his video at all. It worked fine He was just showing you examples more on how the tools work, but again, if you're trying to keep accurate Counting over time You don't want to actually be counting that whole time like if I asked you to keep track of how long it's been since we did Something you're not gonna sit there and go one Mississippi to Mississippi. No, you're gonna look at what time it is You're gonna do whatever project when you do. Hey, what time is it now? How long has it been? We're doing the same thing with code. So anyway, thanks for watching films by Chris calm That's Chris with a K. There's a link in the description. Yes I actually do spell my name with a K if you were wondering And I also have a patreon page links to that in the description as well as on my website Check out my website lots of useful stuff there subscribe comment share and I hope That I'll see you soon. I won't see you. You'll see me. I hope you'll see me soon. Have a great day