 Well, last month I made three videos on a simple timer script, so I thought I was done with that, but then someone asked in the comments how to do this. They wanted to know how to make an alarm timer that plays a sound. So we're going to start from scratch here, create a whole new script. Let's go ahead and jump into that. So I'm just going to call it timer.sh, and I'm really quickly going to put in my own little template here, which is basically just ahead of copyright stuff, and a main function. So let's go in here. Let's go to the top and make some variables that we're going to need. So first off, we're going to need an alarm sound play. So we're going to tell it where to look for that sound. We're going to create available, I'll just call it alarm. And I will say that it's in my home directory, and it will be a hidden file alarm dot wave. And I'll tell you why I chose wave. And I'll tell you right now why I'm choosing a wave file over like an mp3. And it doesn't really matter. But normally when I do media stuff, I like using mpv back in a like m player mpv is kind of I think a fork of that mpv is awesome. If you haven't used it, that's normally what I would use. Obviously, I'm just going to use the play function from socks, just because I think a little quicker. But another reason is, as you'll see as we move on, that if we don't have a audio file like this, socks can generate tones. So that's why I'm using socks, but socks can't decode mp3s. So that's why I'm using a wave file. So here, next, we're going to say timer, or we'll call it time. And that will be the input that the user gives as an argument. So in seconds, we're not going to get real fancy with this, at least not at this point, as far as like putting in like hours minutes, it's just going to be how many seconds you want until the alarm goes off. Maybe in the future, we can look at modifying this to where you put a time. So right now I'm thinking of it as more of a stopwatch with an alarm that counts down, like an hourglass. But we could also look into creating one that waits for a certain time. But I'm probably not doing that. Anyway, because you're better off using a program that already exists for that. Next, we're going to say start. This is our start time. Now last time, I did something like this, which works, date plus percent s inside our parentheses dollar sign there that runs this command and gives us the output for epoch time, which is time number of seconds since 1970, January 1st, blah, blah, blah. Well, some of the comments point out something that I did not know. I'm going to go ahead and save this real quick. I'm going to say echo dollar sign seconds and look at what we get. So what is this? This is the number of seconds that the script has been running. Now you might be asking why is my number so big? That's because we're running in the shell here and my shell has been open for a while. But in a script it would give you a lower number, but it doesn't matter what the number is. It's just a matter of accurate timing that it's been running. So again, thank you to the viewer who pointed this out. I wish I had his name in front of me. But check out my previous, my first video on timer applications and he's one of the first comments. Anyway, so instead of this, we're just going to say seconds. And so that would give us a starting time. I mean, it doesn't matter if, for some reason, we put this further down the script and the script is running for a couple of seconds. It's just a starting point. It's going to start when our script starts. So, and now we are going to create a variable called s and set equal to one, that we are going to change fairly quickly in our script, but we're going to, you know, check it first. So we're going to create it. Okay. So those are our main variables. Let's go to our welcome or our main function here. We say welcome. And this is again just a little template I have here, I'll leave that sleep because we're going to update our script every second. And we're going to say while, and we'll say while and make sure you put these spaces here. Bash is very particular about spaces there and here. I'm going to say dollar sign s, which currently equals one. And we're going to say as long as it's greater than zero, continue to loop. So we're going to say do that. And then down here, we'll say done. And up here, we'll do this. And now we will check the current, the time given by the user and then subtract seconds minus our start. So let me write that out real quick. And as I said in the previous videos, I have a habit just because how I learned I have been using let to create integer variables. It seems like most people don't do that and seems a little bit cleaner to do it this way with the parentheses. So I'm trying to start doing that. We're going to say our time, which is the number of seconds. So if I say 10, we'll say 10 seconds and we're going to subtract and then parentheses here because we want to subtract these two first seconds minus start. Okay, now, first off, all three of these are variables. You could write it like this with the dollar signs. I think that's what I did in the previous videos. But it turns out you don't need those dollar signs there when you're doing math like that, like this with variables. So what we want to do is get basically take our time. So let's say we say 10 seconds and subtract how many seconds have passed from that to get our current countdown time. So if it's been five seconds, we want to subtract five from that 10. So this is a 10 that we initially get from the user. And then if we take our current time that our script has been running and subtract the time that the script started running up here, then we're going to get whatever that difference is. So we'll say five, if it's been five seconds. And then we'll subtract that from 10. So we say, you know, five, S will be five. And that's why we're checking up here. As long as S is greater than zero, we're going to continue. But once it hits zero, we're going to exit out of this loop and do something else. I know in the end, last time I did the print function to clear the line. I'm just going to do it the way I originally did it, because this is how I know how to do it off the top of my head. I'm trying to think of the print. I don't use print F enough, and I should. But it doesn't matter. Here, we're going to say backslash R and we'll say dollar sign S and we'll say seconds left. So what are we doing here? So we're saying echo. We're saying do not print a new line at the end of this. And the E is saying look for these special backslash characters. And what these backslash characters are saying is go to the beginning of the line and start typing again. So if we say go to the beginning of the line and then put a bunch of spaces, basically clearing out the line. And then we're saying again, go back to the beginning of the line. And then go back to the beginning of the line here so I do that twice. It doesn't really matter. And then we're going to print out what we want to print out. Actually, yeah, so let's just leave that there. Anyway, and then we will sleep for one second. And let's go ahead and give that a quick try, okay? Gotta make it executable, change my plus X and name our script. Just do that once on your system. Make it executable. We'll run that. And it says zero seconds left. Why does it say that? Because we didn't give it a number. Let's go five. Now it's counting down five seconds. And when it gets to zero, it's going to exit out. Perfect. So there's an issue. If the user doesn't give an input, it's going to just start at zero, or actually start at one and subtract from that. But let's go ahead and that's our one. But yeah, you know what I'm saying. I hope you know what I'm saying. I don't know what I'm saying. Let's go ahead and do a quick check up here. So we're going to say if space bracket space dollar sign pound, and then we're going to dash LT dollar sign one or just one. I'm going to say then if and then we're going to say we're going to give it some the user some information echo time needed in seconds usage dollar sign zero. Now if you remember dollar sign zero just gives you the name of the script. So it doesn't matter what you name the script, someone renames the script, it's going to print out the name of the script here. And they'll say seconds, seconds. So that's usage and they'll give them an example. So say example, dollar sign zero, and we'll say 10. And then we will exit one because the script failed. Okay, now let's save that and run it again. If we don't give it a time, it's going to print out that message give them a little bit of help. And if we give it a time we'll say 10 seconds here, it's going to count down. Great. I'm doing everything in the main function really some of these things you break up into different functions you can have the main timer, you can have the user input check in a function usually I don't put that in a function just because I like having at the top of the script before you get into the main function. But we're just going to keep this simple. And after this, we're going to now echo out. We're gonna say dash E backslash and times up. Now what's with the E in the backslash and well because we don't we have this no new line here because we're writing over the same line over and over again. If we didn't put this E and this backslash and it would actually type this at the end of this line. Actually, let me go ahead and remove that and show you exactly what that would do. Make the shortles make a three seconds and you'll see it'll print it right there after left and we don't want that we want it on a new line. So we're going to say dash N new line not dash N dash E dash N would say no new line at the end of this and we do want a new line at the end of this. So let's go ahead and run our script for three seconds again. There we go clear the screen and now we want to play our sound. So we will say play and socks has to be installed for this and that's something we can look into you know checking if socks is installed and so if I just do this now I do have a wave file in my home directory called dot alarm wave but we'll remove that later so you can see but we'll go ahead and run this welcome three seconds and if my volume's up there we go sorry it's coming out my headphones I had to switch my audio output so it works I can control C to stop that or we'll stop at the end of the audio file it's just some random audio file get off but you get all this playing of the output blah blah blah we don't want that showing up in our script so what we're going to do is we're going to take the output of that so two greater than and we're just going to dump that into den of null perfect now we're running again the controls see that looking good I don't know why I invented it like that okay but what if our wave file doesn't exist we want to check for that so we're going to say if and here we're going to say dash f and we're going to say dollar sign alarm which again remember is up here near the top of our script is saying it's a hidden file in your home directory called alarm dot wave so you can always just change it in the one spot up there if you want to change where your alarm file is saved then and then we will say else because otherwise your script will not play the sound the socks will give you an error but you won't see it because you're dumping all the dev null so that won't get an alarm sound so at this point I'm going to copy and paste something for my notes here this is actually something I've been meaning to do tutorials on generating synthesized sounds in a shell script but basically this is going to play a chord and the chord is going to play is a C chord and I'm not going to explain all this we're dumping against dev null so basically if our alarm sound does not exist it is going to play that chord let's see so we'll do it once with our alarm wave sound kill that now I'll remove my alarm wave sound and if we run our script again this time so it played a chord and you can actually write out full songs like this which we're not going to do that's just a basic chord there but what we can also do is it only plays that chord once it fades in fades out based on our parameters here and it's as long as I tell it to be let's go ahead and just put this in uh we'll type four I in I think would be a good way to do this and I'll say one two three uh do and then I'll say done now it should play that chord three times in a row there might be a more elegant way to do that let's go ahead and start and that is it our basic timer alarm script is complete obviously there's more things we can do and uh but this is a very basic script here gives the user output uh if they don't enter a time we didn't really do anything if they give you something other than a numeric value and I'm sure what would happen there let's go ahead and just say it probably will just go to zero yeah so that was interesting um we could look into adding a check for that in the future um but we have we have a pretty good script here it should keep some accurate timing uh it updates every second you know so you're not going to want to use this for like a stopwatch thing because because we are using the sleep command to check so if it's running slow during the countdown it's not a problem but if it ran slow right at time when it was supposed to go off or even it's checking every second um it's not going to be exactly falling on the correct second when it plays the alarm uh but it's going to be within a second as long as your computer's not jacked up um but we also have it to where it's playing a wave file but if that wave file doesn't exist it defaults to playing a synth that's going to generate on the fly uh which again you can change all this so let's go ahead and like make that a two there and now that's not pretty much the same um put that back to one again uh i've been meaning to do a tutorial on synth stuff like this we're playing chords here these are the notes we're playing the type of wave sign we're playing uh blah blah the fade in how long to hold fade out all that stuff um but i think uh for 15 minutes or so of typing we've got a pretty good little script here i will try to remember to put a link in the description of this video to the script up on pastebin i do thank you for watching and as always i hope that you have a great day please is films by chris.com chris with the k there's a link in the description there you can search through all my videos on both my channels and if you like my videos be sure to like share subscribe comment and also check out my patreon page again there's a link for that in the description and on my website you can support me through uh paypal if you would like but i really do appreciate any support i can get from viewers and i hope you have a great day