 three videos on a simple timer application. Sure thing. We're gonna take it a step further. So far we've created a pretty nice little timer script that displays properly formatted with placeholder zeros, a timer that starts when you start the script, gives you a fairly accurate time using the current time on your computer's time chip, rather than just using a loop, which can definitely get out of sync pretty fast. We're formatting it with leader zeros. We're clearing the line rather than the whole screen, which is nice in many cases. But there's one drawback to our code is if we accidentally close this window or kill our script, we started again, it starts all over again. Can we make it so that whenever we start the timer, even if we kill it, can we, if we started again, get an accurate time since the original time started, what if we were to shut off our computer and wait three months, a year, come back, turn it back on? Will it give us an accurate time from when we started the timer? Of course we can do that. So let's go ahead and go into our code here. The only thing that's different here is I added this little header here with the GPL and copyright information. But what we need to do is create a temporary file that we're gonna put this start time in, and then we can check if that file exists. If that file exists, get the start time from that. If not, get the current start time or if we tell it to start over again. So let's go ahead, we could put that information into our temp folder, which would be a nice place for it. But if we were to restart our computer or shut down, we're gonna lose that. So we could use the user's home directory would be the next best bet. So we're gonna take, let's create a variable called temp, and we're gonna say dollar sign home, for slash, I always call it dot, the dot makes it a hidden file. So dot my timer dot temp, how about that? Now what we can do, we can say if this file exists. So we're gonna say dash f, which means if this file exists, and we're gonna give it dollar sign temp, then else and the script. Okay, oops. So what I'm gonna do here is if it exists, well then we're gonna take this, let's put this in here, and we'll say let timer equal, and let's copy this again and paste it here because that's what we're gonna use there. So if the file exists, oops, I can't type today, we'll cat the contents of that file out into timer. So if it exists, we're gonna get it. If it doesn't exist, so are else, we're going to get the current time, and then we'll also want to put that start time into our temp file. So at this point it will exist. And that's a start, so let's go ahead and run this. I'm kind of making this up as I go along, so I might mess up a little bit. Okay, I printed that out, and let's kill it and start it again. You know, why is it, that should be going into that variable there. Oh, I have quotations. Quotations need to go in the right spot. Let's go ahead and do that. There we go, now, okay. And if we kill it and we run it again, oh, look at that, it started at five seconds. If we kill it, let's wait a couple of seconds, start it again, we're at 10 seconds. Kill it, we'll go three, four, five, run it again, we're at 15 seconds now. So that's good so far. So now what we need to create is for the user to restart the timer if they want. So let's go back in here. And let's add in a little bit of output here. I'm going to say, whoops, echo new timer started. Really, we should be putting all this into function since we're working with functions. And here we'll say echo timer reloaded, okay? Yes, let's do this. Let's go 90D, I'll come down here, paste, and I'm gonna create a function. I'm going to call function check timer. We indent everything, save that. Up here, we're going to go check timer, check. There we go. Let's go ahead and I put the semi call in there at the end, you can have it or you don't need it. I'm just, I write a lot of other program languages that require it so I get in the habit of it sometimes. There we go, timer reloaded. And if we were to delete that file, we'll say remove from our home directory dot my timer and we restart our script. New timer started, if we kill it and restart it, timer reloaded, okay. So now what to do is so that the user doesn't have to manually delete it like I just did, we're gonna go in here and we're gonna check here if dollar sign one, which is the first argument given to our command, and we're gonna say equals. Here we'll say new, oops, I can't type today. New, then, oops, what we're gonna do is we're going to remove dollar sign 10. So if I'm correct, we're gonna go like this. The timer is reloaded so welcome to my timer, timer reloaded and it's been a minute since I originally started this timer. If I kill it and start it again, it does the same. It continues again, but if I say new, it started a new timer, perfect. And again, as long as that file isn't deleted, you could shut down your computer, turn it back on minutes, hours, days, weeks, months, years later, and should give you an accurate time. Obviously if you come back years later, your days is gonna be a big number. We didn't put a column there for months or years, so this last little column here will be a pretty big number if you wait three years, you know? But let's go ahead and go back in our code and look it over real quick. The only thing that we could probably do is give user output on how to reset the timer. So here instead of just reloaded, we can say reloaded, we could create a whole help function where you do dash H and it loads stuff to the screen, but let's just right here say, I'm just thinking whether I wanna make a whole help menu. Now let's say to restart, run $0new. $0 is gonna be the name of our script, so it's wherever our script is or whatever it's named. So right now our script's called timer.sh. If we were to rename that, the $0 will automatically see that and put it there. And I guess I should put like this. So let's go ahead and run it without the new command and it's going to say, welcome to my timer. Timer reloaded, to restart, oh yeah, to restart run.slash timer new. And of course if this was put into a folder like my user bin folder, user local bin, it would, you don't have to do the dot slash and this will reflect that. So again, if I run it, again it's been two minutes since last time I restarted it. If I kill it, I can do dot slash timer new and start a new timer. And that's it, that's about what I wanted to do with this timer, so I'm happy with this. Everything's clear, I'm going to put a new line there to break things up. So again, let's look over it. We have our main function, which isn't run here, it's run at the end because you want the whole script to load for JavaScript. Same with like C programming, you need the whole code. If you're going to call a function, you can't call that function until it's hit in the script. So I can't call the main function until it's been declared. And then inside the main function, I can't call check timer or start timer until they've been loaded. So that's why we call main down at the bottom. That's not uncommon for a many programming languages, not all, all depends on how the programming language is set up. So again, it loads, let's go just from the top. We have our temp file here and then we're going to check. Did the user say they wanted a new timer? Well, if so, remove our temp file. And actually at that point, if that file doesn't exist, we're probably going to get error output there. So for example, let's have a look at that. Let's go ahead and remove that file and run our script. And you can see we did get an error, which is not a problem, but it's kind of ugly. So we should be able to rectify that. We could check if the file exists before we try to remove it. But we could also just say, actually that's probably the best way to go about doing it. So real quick, if we just wanted to put to null, we should be able to do two greater than dev or slash null. And now we should not get, even if we remove that file. Yeah, so no error there. Well, let's just make sure we do it without the new. Yeah, no error there. The error is happening, we're not seeing it. So, but another option doesn't hurt to have that there, anyway, but what we really want to do, a proper way would probably be to check if that file exists before we try to remove it. So we should be able to check if the user says new. And we can put an if statement inside the if statement, but we should also be able to, in this case, do dash f, dollar sign, temp. I like to put things in quotations. And it's very important with bash to have these spaces that, you know, right there, there's a space, there's a space. If you don't put these spaces, you're gonna get an error. So if you're getting an error, that's Y. Oh, it might be Y. But I think that will work. Let's just, first of all, remove that. Run our timer script, we didn't see an error. Let me go ahead just to see, I'm going to comment this out. Let's go ahead and remove that, run this. Yeah, no error, because the file didn't exist. But it doesn't hurt to leave that in there, the dev null. It's irrelevant because, well, it might be good to remove it in case, for some reason, the user doesn't have permission to remove that file. They'll see the error there, which might be a good thing. So we might not want this here. It's up to you as a programmer. Just try to think of, you know, if their user, for some reason, the permission's got messed up in their home directory and they can't delete that file, our timer program's not gonna work and they're not gonna know why, because they're not getting an error if we put it to dev null. So let's not have that in there. Anyway, there's, we started, reloaded it, and new. Again, this code, I'm gonna post on pastepin. If you go to filmsbychrist.com, that's Chris the K, there's a link in the description right here, you can go to software. And then down here we have software, scripts. I think last time I said my scripts was actually the name, oh, I'm sorry, not scripts, notes. I said it wrong in the last video. Notes will bring you to my scripts, you can see right here it says. And then here, I can start searching for stuff. In this case, I can type in bash basic timer. And it would come up, except for my list updates once a day and I just uploaded the last one, I haven't uploaded this one yet, so they're not showing up in the list. But it searches through all my pastepin files, which I have 623 entries in there. This is a great place, if you're just bored and wanna try some things out, you can type in bash or Python, and you can see code, and it's fairly somewhat smart. It might run a little slow on mobile devices because it's not the best design because I actually have it load, not only a list, it's searching through every word in the scripts, and it actually loads, right now in the background of this page, every single script is loading. The entire, the entire script. So if I have a script that's five pages long, it's in this page already. So it takes a little time to filter on slower devices, and that's just how I designed it. Instead of filtering it on the server and which is how it should be done, I'm filtering it on the client end. And so there's hundreds of pages of scripts loading when this loads. Anyway, that's a great place to look through my scripts. Bigger projects I have up on, oh it says GitHub, I need to replace that because I actually started using GitLab, my GitHub's still there, but I'm not updating it anymore, Gitlib, GitLab. So I didn't realize I forgot to update the link on my homepage, there we go. That's me, see, with a crown, rather than a silly little mustache. So I do thank you for watching. I know, I hope that I took something as basic as creating a timer and complicated it beyond belief for you, but that's, I find a lot of programming. You can write a script that does pretty much anything you want, pretty simply, pretty easily, and pretty fast, but then when you start thinking about how people are gonna use it and thinking about mistakes the end user might make or extra functionality or more cleanly doing it, you can spend a lot of time on a basic thing such as a stopwatch application. So part of this three-part tutorial was to show that, it's very easy to go down that rabbit hole, but that's part of being a good programmer. If you're writing a script for yourself it's usually pretty simple, but once you start writing it and thinking of end users who may not be thinking like you think, I can get a little more complicated or if you just want to add more functionality to it and sometimes you also overthink it where you start making it too complex. So keep your code short and simple and thank you for watching. Again, visit my website, filmswightcris.com, lots of information there, lots of videos to search through and that's it, have a great day.