 Okay, welcome. This is video three in our 2017 Bash Script videos. These are, again, intermediate videos. They're not advanced videos, but intermediate. Hopefully you know the basics of shell scripts. If not, I've got plenty of stories out there. Just check out my website, Filmsitechris.com, or my YouTube channel. I have playlists for shell scripts. Go ahead and check those out. Today, we're gonna be creating a simple animated spinner using arrays and functions, which we went over in our last two videos. So I hope that you watched those. I'm gonna use Vim as my text editor, but you use whatever text editor you want. And I'm gonna create a script called spin.sh. In here, I am going to give it the shebang line so that our computer knows what interpreter to use. And then I'm gonna create an array, I'll call it spinner. And in here, I'm gonna give it a little animation. And I'm just gonna use this because I've seen this in examples before. And then we'll go, okay. So basically, our array is gonna be each one of these. And each one of these is gonna be a little visible frame of our animation. So let's go ahead and we're gonna create a function that we're gonna call spin. Whoops. And in here, we're gonna do a little for loop. So I'm going to gonna say for i in. And in here, we're gonna say in quotations. So I don't make a mistake. I made in the first tutorial, spinner. And inside the square braces, we're gonna say at. So we're gonna say loop through each item in this array and use it some way. It's gonna say do and done. So this is gonna be our full loop here. And here, we're gonna say echo. And then we're gonna say dash any. And then we're going to, well, I need that semicolon there. Sometimes I put them there. Sometimes I don't. Backslash r, which is special character. So we have to be using double quotations, not single quotations for this. Dollar sign i. Okay, so what is this saying? We're gonna say echo. And then n means don't create a new line at the end of this echo. E, I forget exactly. I think it means basically we're using some special characters in here, like this backslash r. I think if we remove that E, this backslash r is not gonna hurt. The backslash r basically means reset. So it's going to print something out and then go back to the beginning of the line and print over it. So that's how we're gonna create our animation. It's going to print something and then it's gonna go back and print over it again. So let's go ahead. And so that we can see your animation, we're gonna put a delay in there. We're gonna go sleep. I'm gonna go zero dot two. So that's just over a quarter of a second or just under a quarter of a second. Let's go ahead and save that. Make it executable with changemod. And we're gonna give it the name of the script. So changemod plus X means make it executable. Dot slash saying we're running a script in the current directory, the name of the script. And here we go, nothing happened. Why did nothing happen? Because we created a function called spin, but we never called that function. So let's go ahead and do spin. And we're gonna run that. And as you can see, it ran through the animation once. Let's watch this again. Do, do, do, do. But we want it to loop forever. So I'm gonna come back in here and I'm going to say while space bracket, space ones bracket. You have to have these spaces in here, okay? So we're gonna do that. And then I'm gonna say do. And then down here, I'm gonna add in done. So that's just saying loop forever until I kill this function. So I'm gonna run that. And as you can see, it creates a little animation there. I'm gonna hit control C to kill that. And this is something I learned a long time ago and I have notes on it. And I've changed a little bit, but I wish I remember where I got this tutorial because I would give them credit. So if you made a tutorial that's similar to this out there online, I apologize. I would give you credit, but I've had this for years in my notes. So anyway, that's our spinner. It's looping forever, but it's not really doing anything. So you really want the spinner to happen when something is happening and then you would kill the spinner. So what we're gonna do here is we're gonna create another function. And I'm gonna call this function copy as if we're copying files, but we're not really gonna be copying files. I'm just going to have it count through numbers, but in real life, you can have this running wallets copying files if you're using a for loop. So in here, we're gonna call the spin function, but we're also gonna say ampersand after that, which means run this function, but throw it into the background so that we can continue forward. Next, we're gonna use something special. We're gonna say dollar sign exclamation mark. That gives us the process ID of the last function run. Let me go ahead and save this. If I say dollar sign exclamation mark just here at the shell, I'm not in dash. Let me run that again. Dollar sign exclamation mark. Echo test. Okay, nevermind on that. Let me go back into my script here. That in theory will give us the process ID of the last run function, which is our spin function. We want that so that we can kill the function later. My little example there didn't work, but we're just gonna move on from that. So we're saying PID, so we're creating a variable that holds the process ID, the PID for the spin function. Next, we're gonna create a for loop and in my pretend example, we'll be copying files here. I'm just gonna say that we're going to go through numbers one through 10. We're not actually gonna display anything, but what we're gonna do is we're gonna say do, done, and we're gonna say sleep for one second. What that's gonna do is basically count to 10 seconds, but imagine that it's copying 10 files instead in the background. Once that for loop was done, it's gonna continue. We're gonna say kill dollar sign PID, which is gonna kill the spin process. Now, we have to come down here where we call the spin function before, but we're actually going to call our copy function instead. So what's gonna happen when we run this script? Let's go through it all. We say, okay, this is a bash script. Create an array and each one of these is basically a frame in our animation. We're gonna say, then down here at the bottom, we're gonna say call our copy function. What does our copy function do? Where it starts the spin function. So this is just gonna start looping through the animation, just over and over again, independently of this function. But we're also gonna grab that process ID for that spinner. Then we're gonna loop through numbers one through 10, sleeping one second each. So this whole thing should run for 10 seconds. When the 10 seconds is up, kill the spinner function. Otherwise, if we don't do that, the sleep, this loop will finish, but this will keep running in the background. So let's go ahead and see if we typed everything right. Well, we already made that executable. So let's go ahead and spin.sh, and there we go. And it's looping through the animation, but instead of going forever like before, after 10 seconds, it should finish. There we go. Now you notice that this little prompt here that is there at the end, and that's actually my prompt, and that happens in ZShell, something similar will happen to Bash. So what we can also do in here, just to keep things a little clean, is I can add in an empty echo command. And basically that will put a new line at the end so we don't get that little percent symbol or wherever prompt is at the end. I think a Bash, your whole prompt would just be up here after that, which just looks funny. So now let's run it again. So it's gonna go again counting from one to 10, one second between each one independently of the spinner, and after the 10 seconds, after it loops through 10, it will kill that and we get our prompt again. So let's go ahead and change that a little bit. What we could do is we can also add in a command here. We can say echo again, dash end to prevent a new line from happening. And we can say copying files in this case or whatever you're actually doing with this little spinner loop. And oh, see that doesn't work because it put a whole new line there at the end. So let's go ahead and not do a new line. So we'll just say copying files and we'll do our little animation there. Let's change up our animation because you can make the animation look whatever you want. If you are familiar back in the day, 90s and so, you get a spinner that looked a lot like something like this. So we'll start with a line and then we'll go line this way and we'll do a line that way and then we'll do a line this way. And that would be our loop. But these are special characters. So just like if we had spaces, special characters also need quotation marks around them. Sorry, my phone is ringing. Should have put that on silent. Do not disturb until there. Okay, it's off. Sorry about that. And I'm gonna put quotations around it like this, which even when we were doing the O animation, putting quotation marks around them was a good idea. But actually, let me undo all that. Since these are special characters, we should really do single quotations because as you saw at the end there, this last one was a different color pink and that's because it was commenting out the next character. That's a whole different tutorial I've gone over in the past, but single quotations for this. Now if we run this, we can say copying files and it does a little rotating thingy there. Another thing, not as advanced, if you wanted to go simpler than our spinner, we could do something like this instead of using an array at all. We can just do a while loop. And instead of our array, we'll just say dot and here we can add back in. And if I'm thinking about this right, just kind of winging this, we can say copying files dot, dot, dot, dot, dot and we'll just keep on putting dots every quarter, second or so, sixth of a second, right? Yeah, fifth of a second until it finishes. And I kind of like that, even though it's simpler than the other ones, you don't need the array at all or that for loop, just the while loop. I like that because if you remember back in the day, installing doom on DOS, that's what you would see while copying files. And back in the day, it would be dot, dot, dot, dot, dot, dot, like that. And then as your computers got faster, it started doing lines of dots at a time as time went on and nowadays you just apt, aptitude, aptget, install, you know. Anyway, I hope you enjoyed this tutorial with a little animated spinner. Again, I have had this in my notes for years, but I didn't write down in the notes where I learned it from. It might have been a hodge podge of a few different tutorials. Usually my notes are a few different things I've learned, but this one I think just kind of stood out my mind and I wish I could give that person credit. Anyway, I do thank you for watching. Please visit filmsbychrist.com. That's Chris Decay. There's a link in the description. Also visit my Patreon page, patreon.com. Ford slash Melix 1000 there. You can help support me or you can support me on my website. There's a link to the, to my PayPal account under support. As always, I thank you for watching and I hope that you have a great day.