 One of my favorite things to do on camera is to create a problem and solve it using Bash scripting using a lot of command line utilities using a lot of the GNU core Utils that are available to us on Linux and what I wanted to do today is one of the most common Applications one of the most common kinds of programs people want on a personal computer as an application that keeps time So something that you think of it as a timer or a countdown or a stopwatch Something that just ticks off seconds or minutes or whatever it happens to be that you're trying to track as far as time And of course, there are probably Hundreds of these applications available to us on Linux You don't have to write your own stopwatch or countdown script or anything like that But what's the fun in that? I wanted to show you today I'm gonna open up a terminal and with many of the basic GNU core Utils We can actually get a pretty accurate stopwatch or a countdown and you could literally do this in a hundred ways I'm just gonna show you using some of the most frequently used commands in the Linux terminal how you can achieve this So let me switch over to my desktop and I'm gonna go ahead and switch to this workspace where I have a terminal open I'm inside the bash shell and I'm zoomed in here So hopefully you guys can see everything I'm about to do now Let's start with creating a basic timer on Linux So one of the standard command line utilities on Linux is the command time What does time do? Well, you do time and then some other command after it And it gives you the time that it took that command to execute for example if I did time Firefox and I hit enter. I'm not sure Firefox is installed on my computer It is, you know, it launches Firefox here And let me get back to the terminal and it will tell me the time that it took to run that command So a really simple way to do a timer is to time and then time some command that really Has no output or no input for example the cat command if I do cat and then this dash behind it a single hyphen What this means is cat is going to take standard input That's what the hyphen signifying, but what's it taking as standard input? We really didn't direct anything into the cat command right cat dash That's the only thing the time command has nothing to do with the cat command This is just running a time on that the cat command though is smart enough It's always going to try to read from standard input so we don't have to specify the dash But if I run time cat watch what happens Cat is kind of it's running is trying to cat standard input But we really didn't give it anything to have standard input. So it's just it's timing It's running the time right until we kill this process It'll be a never-ending process until I hit control C to kill the timer basically and you can see that ran for 19.929 seconds So that is a real easy way anytime you just want to get a quick timer on something Basically think of time cat as a stop watch just time cat hit enter And when you want to stop the stop watch control C and that gives you a really accurate time And like so many things on linux, there's a hundred different ways I could do this I could actually instead of timing cat. I could time the the read command and hit enter and what's it reading? Well, we didn't specify right it's just going to be one of these Never-ending processes right it's just going to keep running and running until finally control C and then once again We get the time so that's a pretty accurate stop watch either the time cat command or the time read command But what if you want to feed this kind of stop watch a certain amount of seconds? Maybe I only want it to run for exactly five seconds or 10 seconds or a minute or whatever it happens to be Well in that case instead of just using the time command you want to use the time out command time out command You feed it a specific amount of times Maybe I'm going to do five seconds for example And then you feed it some kind of command to actually run the time out five seconds on in this case because we've already Done cat and read without any standard input. We could do time out space five seconds space cat space dash for Standard input. We're not giving it any standard input. So this is going to just be a running process That should in exactly five seconds time out and it does now I'm not crazy about not having any kind of output at the end of it So what I would do is I would probably do something like time out five seconds cat Dash and then do a semi colon and then we're going to have it run the command echo Done exclamation and now Let's go ahead and wait for the time out of five seconds And then at the end it should echo the word done exclamation and once again just like everything on linux There's a dozen different ways. I could have done the exact same thing here instead of time out five seconds on cat What I could just do is in the best shell, of course You have the sleep command and what you could do is sleep five that tells the shell sleep for five seconds And then what I could do semi colon and then Echo once again the word done So what's the best shell doing it sleeping for exactly five seconds? And then once it's done it echoes done Now let me switch over to a different workspace with a new terminal because i'm going to have to kill this terminal After I entered this command now. This is strictly for fun what i'm about to do here So what we're going to do is we're going to read space dash t five So we're going to read for five seconds and then pipe pipe So two pipes write the or and we're going to run a command line utility many people are not familiar with I just something you'll never really use but it is their speaker dash test And we're going to use the flag dash t and then the argument for dash t will be sign meaning that the speaker test What's it going to use for a sound for this speaker test? It's going to use a sine wave and then dash f is the frequency of the sine wave I've chosen 500 for the frequency watch what happens here So it's going to read for five seconds and then it's going to run that speaker test Oh my goodness. Let me kill the terminal That will hurt your ears So let me go ahead and clear this terminal now another standard command line utility is the date command the date command It's nice because we really don't have to play with any formatting here What I could do is I could use a well loop to print out the date and because the date includes hours minutes and seconds That actually is a nice little timer. So what I could do is I could well true and then semicolon Do print f and what are we going to print here going to do percent s escape r And then the ending single quote there. So what we want to do is while this is true I want you to print whatever value with a carriage return. That's the escape r there the backslash r And what exactly is percent s going to be in that printf statement? Well, we need to actually specify a command. So I'm going to do date And then any while loop of course needs to have done at the end So semicolon and then done and if I did this correctly what this should do is print the date command forever right every single second right And look what happens here. You see that is a really nice timer Now like everything else I've shown you I've shown you multiple ways basically to do the same thing instead of using a while loop Using the print command to print out the output from the date command We could do a for loop instead and instead of date. We could actually sequence Some numbers here. So what I could do is I could sequence 60 and then I'm going to do space minus one and then one. So we're going to sequence 60 numbers Let's do it in reverse order. So this will be more of a countdown. Let me scroll back up You can see we started at 60 and we went down all the way to one decrementing, you know by one So let me clear the screen here. What I'm going to do is I'm going to do for in And let's do for i in a command. So I'm going to do the dollar sign and parentheses. What's the command? Well, obviously we're going to do sequence 60 space minus one space one And then let's go ahead and add a semicolon. So for i in this command the sequence command I want you to do and what do I want you to do? Let's do echo this time I'm going to give echo these flags dash in e and then what do I want you to echo? I want you to echo the carriage return It's followed by dollar sign i so dollar sign i here represents whatever numeral it happens to be on during the sequence countdown Then after that, let's go ahead and do a semicolon because I want to enter another command after this I want you to sleep One second right because I I don't want this sequence output of these 60 numbers to happen all Instantaneously, right? I want 60 wait a second 59 wait a second 58 wait a second You see how that's going to to work that's going to be a nice little countdown there And then finally we need to do a semicolon and done because like the while loop has to end and done The for loop also has to end in done and if I did this correctly, which I'm not positive I did 60 59 58 57 And you see how it's all on the same line because we specified dash in and the echo right meaning no new lines here And once again to kill the process if you don't want to wait for the countdown Just like everything in the shell control c will exit out of that process Now let me go ahead and clear the screen now so far these countdowns and timers. They've been rather plain I want something a little fancier maybe different letters different coloring different font sizes Maybe some ascii art things like that and we could do this so here in the the bash show what I could do I'm going to specify a variable. Let's do n equals 100 So we'll start counting down from 100 and what I'm going to do is Semi colon now that we've stated that let's start a while loop I'm going to do while and then let's do a two opening braces here And then I'm going to do dollar sign and then two opening parentheses here And I'm going to do dash dash capital n and then the closing two parentheses and then Greater than space zero space and then the ending brackets And then the semi colon and I'm going to do echo dollar sign in And then I'm going to also pipe that echo so pipe The echoed in the numeral into this command line program figlet Now if you've been around my channel for a long time in the early days of the channel I talked about some really cool ascii art ascii text command line programs such as figlet Basically what figlet does is you pipe it some words or some numbers instead of just printing it out It prints it out in these big ascii art bubble letters. So that's kind of cool We're going to do figlet dash see and and sleep One because we want to sleep for a second Let's do a semi colon and because we have the while loop still running We have to have it close with done. So just very quickly what this does We're going to set this numeral this variable here in to 100 And then this while loop is going to decrement in 199 98 and as long as it's over zero It will keep that while loop running once it reaches zero the while loop stops And while that's running it's going to echo the number that it's currently on And it's going to pipe that number into figlet to give us big bubbly ascii art letters Let's see if this actually works 99 98 97 96 Yeah, that's really nice, but I don't like it printing The new number on new lines. I would like it to all be in one place Let me hit ctrl c to kill that process clear the screen So what I could do is I could essentially run that same command except this time in the echo part instead of just Echoing dollar sign in I'm going to echo two things. The first thing I'm going to echo Is this escape sequence here? What is this going to do? It's going to clear our terminal and then echo The the in the in value So what this hopefully does is now instead of giving us that list of numbers as it counts down Hopefully the numbers are kind of in one place. Let's see if this actually works 99 98 97 No, it still moves the text down once it reaches the bottom of the terminal It's all in one spot. But yeah, that's not exactly what I wanted But still kind of a need effect. Let me control c to kill that once again clear the screen So I think what I'm going to do let me up arrow back a couple of commands. I'm okay with this command here where the numbers Appear each on basically their new lines. I'm okay with that But I would like a little bit more bling to the command So what I probably could do is instead of just doing figlet Let's go ahead and have figlet pipe into lol cat lol cat is a program that gives fancy like rainbow coloring to any input that gets piped into it So we're going to echo the number into figlet to get the bubbly letters And then figlet gets piped into lol cat to give us that bubbly letter Rainbow coloring and let's see How that looks that's very nice Yeah, that's very cool. So I'm gonna You know what I want to keep some of these countdowns and timers How can I do that? Well what I could do is I could I could create a script where I could just execute the script anytime But I think because they're such simple little programs just a couple of lines long in most cases It would be a good idea just to add these as functions custom functions to our bash rc So let me open my bash rc in a text editor So I'm going to do a vim here dot bash rc and in my bash config here I have some sections. I think where I already have some custom functions such as Yeah, this ex function, which is an extract function What I could do is I'm just going to create a place here above it And I'm going to call this Countdown so I want this countdown function to be that last Command that we did with the figlet into lol cat. So what I need to do is I need to create a name I'm going to call this c down and then opening and closing parentheses right that signifies This is a function and then we need opening and closing braces there And this signifies everything in the braces signifies what that function the c down function actually is going to be So I went ahead and essentially put the exact command that I ran in the shell, right? So n equals 100 I don't need semicolons because the line breaks are essentially the semicolons inside a script, right? So n equals 100 and then the while loop where we're decrementing in and while the while loop is running We're going to echo in pipe it into figlet pipe it into lol cat and sleep one second in between every single numeral So let me escape and then colon wq and vim to write and quit and now let's source our bash rc so Clear the screen and now that we've sourced our new bash rc. I should be able to run c down And get the countdown from 100 that's nice But I would much prefer c down to actually take a number as an argument So instead of counting down from 100 and only 100 I give it a number for the number of seconds for the countdown So what I'm going to do let's get back into our bash rc and I'm going to edit that function Just ever so slightly. So what I'm going to do is instead of n equals 100 Let's make n equal dollar sign one that signifies user input user inputted value So let's go ahead and colon wq and now and let me go ahead and clear the screen And I'm going to go ahead and source our new bash rc Clear the screen again to get rid of that ascii heart, right? And now let's run our c down function and it takes now a user argument the user argument needs to be a number I'm going to give it five for five seconds. So four three two one zero and of course on zero it terminates the process So those are just a few different ways using standard command line utilities on linux You can create timers stop watches countdowns if you want something that's already been scripted for you There are a ton of these little programs out there that you can find one of the ones I know that's out there I know it's in the arch repositories or maybe it's in the a you are the arch user repository Is a program called term down if I do term down without any arguments it says command not found So I don't think I have term down installed. Let's see if I can install it Not sure if it is in the standard repository or not. It is so let's go ahead And now when I up arrow and do term down again It's just a timer. You can think of this as it's just going to go up, you know, binaural forever And now if you want to specify a time what you could do is term down and then do 60 for 60 seconds, right? And then it starts at 60 and decrements all the way down to zero Control c to kill and of course sometimes you want to do something more than just seconds You want to do minutes or hours you can actually specify The time with term down and hours minutes and seconds so I can term down and then inside quotes I could do one hour Zero minutes zero seconds So it's going to start counting down from exactly one hour from now You know and if I do that there's one hour 59 minutes 58 seconds You know it's just going to keep going And then in one hour from now, of course it will finally terminate the process. I'm going to do control c to kill that process So that's just some of the ways that I would you know create my own little custom stop watches and timers at the command line Using some of the standard basic command line utilities. I hope you guys found this educational I know I enjoyed myself because I got to play with a whole bunch of command line utilities on this very short video And these things are always fun because I think a lot of people imagine That scripting and programming and solving some of your real life issues In a terminal at the command line that this stuff is hard And it's a lot easier than most people think now before I go I need to thank a few special people Of course, I need to thank the producers of this episode gave James matt maximum at Mitchell paul west Why you bald on the alex armor dragon chuck commander area. Okay, George lee march term nadar yon alexander paul Peace archon fedora polytech realities for less grip profit roland steven tools devler and willy these guys They're my highest tier patrons over on patreon without these guys This episode would not have been possible. The show's also brought to you by each and every one of these fine ladies and gentlemen All these names you're seeing on the screen right now These are all my supporters over on patreon because I don't have any corporate sponsors I'm sponsored by you guys the community if you like my work want to see more videos about linux and free and open source Software such as the good new core utils subscribe to distro tube over on patreon. All right guys. Peace My ears are still ringing from that speaker test