 Hello and welcome. We've been looking at the date command in the Linux shell and today we're going to create a very basic countdown timer till Christmas. Let's go ahead and do that. We'll just call it Xmas. Oops, Xmas and I'll call it one because I'm going to make a second script in another video. So I come in here and I'm going to start off with my little bash template here. Let's make my font a little bit smaller. So I just have a main function in here that's called welcome sleep and then exit. So we'll do welcome and then we'll do while one with spaces. Bash is very sensitive about spaces when you're doing if then statements and while loops. But so just make sure while space bracket space one space and then a bracket and then do and done and then everything in here. So that just means true. One is true. So it's going to loop forever until you kill it. So we need to do now is get the today's date and subtract that from Christmas's date and we're going to do that using epoch timestamps. So I'm here. I'm just going to create a variable called seconds. I'm gonna say that's equal and again no spaces there and inside these parentheses I am going to run this command. We're going to do dollar sign double parentheses. So this is going to be doing math in here and so we're just going to put we put these in different variables. We could get today's timestamp Christmas's timestamp then put them in here and do the math and put them into the sleep the seconds variable. But we're going to do it all in one line here. I'm going to say dollar sign parentheses inside those parentheses date dash D and then give it a date and I could just say 1225 and it would automatically do this year's date. But if you want to do it for next Christmas you can do 2019 if you want. Let's just leave it at that for right now. So that and then we're going to say plus percent s. So right there what this is doing is this right here is running a command and hopefully you know that already or you have a little bit of a bash knowledge dollar sign and something in parentheses like this single parentheses is saying run this command which is the date command and we're getting the epoch time that unix timestamp of this date midnight first second of that day or just the instant it started that second dash D just means I'm giving you a date. So the outer dollar sign and double parentheses means we're doing math. So here we're going to say minus. So we're taking that number and we're going to subtract today's date from it. So date but we want it again in seconds in epoch time. So if I was now echo dollar sign seconds I can now exit out of that change my plus X the name of that script only you do that once on your system to make it executable and now we do dot slash meaning this directory run this script welcome and it's a second and now it's looping and it's giving you how long from now until Christmas in seconds that's how many seconds until Christmas and it's looping very very fast here that's why you're getting a lot of the same number and so it's going to keep on looping multiple times a second. I'll control C to kill that and we'll go back into them. Let's make this a little bit nicer and we're going to format it but let's before we do that we're going to clear the screen. So now instead of using echo in this case I'm going to use print F which is similar to echo it works a little different sometimes it's a little bit easier when you're trying to format things. So we're going to do and if you watched a previous video where we did date stuff what was it we just recently I recorded a video where we were doing oh just just basic timers we're going to count down the date we were just you know counting time. We're going to do percent two zero D so that means in the place of this there's going to be a numeric number a digit but we only want it to be two characters long this is nice so it's going to be if it's less than two it's going to put a placeholder zero and then we're going to say days which is just text and we'll do percent two or zero to D again and then we'll say hours percent zero to D and then we're gonna say minutes percent zero to D and then we'll do seconds until Christmas backslash end for new line and then right after that on the same line you want to put in the variables that you want to replace these values with but because our lines getting kind of long I'm just gonna do a backslash and hit enter that means the next line is basically it's like it's being on the same line as the print command the important thing here is you don't have any extra characters after this it's backslash and enter if you put a space there there's going to be a problem so here is we're going to put our variables but we want a variable for each of those times and if you remember last video where we worked with time for the countdown time or the stopwatch timers and the alarm timers again we're going to do math and we're going to do some division and some division with dropping the the decimal or the whole number depending on what it is anyway this part is math in parentheses here again dollar sign double parentheses means we're doing math and we're going to take that seconds variable and we're going to divide it by eight six four zero zero to get our days so however many seconds there are so you saw that big long number of number of seconds until Christmas if you divide that out by this number that's how many days there's going to be because that's the number of seconds in a day then we're going to take our same variable seconds which is still the full number of days we're going to divide that by three six zero zero which is the number of seconds in an hour I believe am I saying that right and then we're going to say percent twenty four to drop off the and just get the the whole number for that I'm not explaining this while I went this sort of thing in more detail in the previous video but we're going to do seconds now divide by sixty percent sixty sixty seconds in a minute sixty minutes in an hour that will give us our hours percent or it's our dollar sign parentheses in quotations here and we're gonna say seconds and we're going to percent sixty so this will give us our number of days this will give us our number of hours this will give us our number of minutes and this will give us our number of seconds evenly rounded and then these they'll be placed in here in the appropriate order based on this is the first one this is the second one because that's the order that came in and again the percent two zero two D just means limit it to two characters which days you might actually want let's leave it like that but I'll add three because you might have three digit characters when you're going to date and then we're going to say sleep one and we're done after that you know we actually have to do but the only we're going to get out of that is by control seeing it anyway we should be able to run our script now welcome and now we got 69 days 14 hours 20 minutes and 47 seconds until Christmas and it's going to keep on looping and it's only updating once every second so theoretically you might skip a second here if you computer and slow in between seconds but and actually probably would every once while again because of like I've talked about previous videos where even if it's a hundredth of a second or a thousand second doesn't take that long to be off to where you're going to be a full second off would that apply to this doesn't matter this is our countdown timer for Christmas and it's not very long at all I mean really it's this line and this line it's two lines of code whoops let's close that window it's basically two lines of code to generate the rest is just looping through clearing the screen waiting a second so two lines of code basically to get that information printed nicely and the rest is just looping it but in the next video we're going to format this a little bit better have a little bit more fun with it so check that out I do thank you for watching please visit films by Chris dot com that's Chris decay there's a link in the description check that out as well as my patreon page patreon.com forward slash metal x1000 you can support me there and I would appreciate it thanks for watching and I hope you have a great day