 Today we are going to do some practical shell scripting. What do I mean by that? Most of my tutorials, I try to show you how tools work and things you can do with it, but people go, well, how do I use that in the real world with scripts I'm writing? And the answer is I don't know because I don't know what you're trying to do. Programming is problem solving. You have an issue, usually something you want to automate and you want to script it out and you need to figure out how to do that. And so I show you these little bits and pieces that you should put together, but I figure why not just make up a scenario and we'll try to write out a shell script that does this. And what we're going to do is we're going to grab the titles in this video of a YouTube video based on URL, okay? Now I'm aware that there's a program out there that does lots of stuff about getting information and other things from YouTube. I'm not even going to say the name of it because any time I mention it in a video, I get a strike on my channel, but we're going to do it manually, okay? So here I am at filmsbychrist.com. That's Chris Decay. There is a link in the description. My website, you can search through all my videos here and I'm just going to choose one here. I'm going to click on this one here. I'm going to pause the video once it starts, blah, blah, blah. Wait for it to load, okay. Pause that. Okay, I'm going to grab the URL from this channel. And I am going to use W get, but you can use girl. So I'm going to say, you know, W get. Now if I just do that, it's going to download the HTML from that page to a file, but I don't want to do that. I'm going to say dash Q for quiet so I don't see the progress or information about the download. And I'm going to say dash or not dash O, but I'm going to say dash Q and then a capital O. That's the letter O, not a zero dash. I was going to just dump the output to the current screen. Ha ha, that's a lot of stuff. Now, what I'm going to do is, you know, go through all that. Now we know the title of this video is a tutorial compilation, okay, is part of the name. So what I can do in here is I can take that same command and then pipe it into grep. By the way, this is an intermediate video. I kind of assume that you know how these basic tools work, but hopefully if not, you'll learn something. But grep is just going to say, hey, in all that output, look for these words. So I'm going to do that. The thing is, a lot of stuff is all on one line. So I get a lot of blah, blah, blah here. So what I'm going to do is, before I do the grepping, I am going to pipe it into a TR. You can also use said. I'm going to say, find all the commas and make them new line characters and then find the line that says tutorial compilation. Okay, that helps a little bit there. So we're looking through here and there's one right there. And we have one right here. So we have this one that says title, title. And there we go. There's our title right there. That's one option. So let's see if we can find that. So we can see this line starts with title inside quotations, colon, and then quotation mark, right? This other line starts the same thing but doesn't have that quotation mark there. So let's try grepping for that. So we'll erase this and try to put this on a line so you can see it like so. So I use single quotes because there's regular quotes in there. When I do that and lo and behold, we do get a bunch of stuff returned. And it looks like the first one here is the title of this video and then there's other words that have title. Oh, you know what I didn't do? So this finding all instances of that. What I want to do is right here add the little carrot symbol. And that's going to say lines that start with quotation marks, title, quotation marks, colon, quotation mark. And there we go. We have our title. Let's go ahead and use the cut command and say, okay, look at the quotation marks and give us the fourth column. We have the title of the video. Now we need to make sure that this works against other videos. So let's go back to my films by Chris. I'm going to copy the URL from this video and I'm going to replace the URL here with the URL video there. And we'll run that. And it says, build do nuke gum 3D on Linux. And that was the name of that video. Let's grab another one. We're going to copy the link address. And in here we will replace it. Once we've tested on a few, we will then throw this into a script. There we go. So this seems to work. This right now will get us to the, you were scraping it. So as long as they don't change the format of the page, which they could do at any point, this should work. So let's go ahead and just highlight that. And let's start making a script. I'm going to use, well, actually I'm using Neo Vim, but I have an alias to Vim, but I'm going to be using a Vim variant to write my script. And I'll just call it YouTubetitles.sh, okay? And I'm going to say bin bash here at the top. This is saying this is a bash script. And I will paste in that right there and I will save it and exit. And I will say make that executable. And I'll say this and voila, it gives us the title of that video. But of course, that just gives us the title of the one video. Let's go in here. And what we're going to do is I will delete in these tags and I will say dollar sign URL. And I'm going to say URL equals our first argument. So now I can say YouTubetitles and I can copy one of these videos. So let's go ahead and copy this URL and I will paste that in there and it gives us the title of that video, perfect. But what if you don't give it anything, right? We're probably going to either get an error or no output, no output. So we should do some checks, okay? So what I'm going to say is I'm going to say, okay, dollar sign one. Okay, if that does exist, I'm going to say the variable URL equals that first argument. If it does not, I can say read-p please enter video URL and I will say URL. So what's saying here is, okay, check. If there's an argument, then make the variable URL equals that argument. If not, ask for the URL. So now we'll save that. We'll run this. I didn't give it a URL. So I will copy one from here, copy link, paste it in there. Great. And of course, if I do give it a URL, let's give it a different URL, copy it shouldn't ask for it because it sees you gave it one. But what if you give it a URL that isn't a YouTube URL? So, and also what if you just want to use what's in the clipboard? I like doing that a lot. I like having, you know, checking for user input. If there's no input, check the clipboard. If there's nothing in the clipboard or not the right thing in the clipboard, then ask for it. So let's go back in here and see if we can write this a little differently. So if URL, if the first argument exists, we're going to say, set the URL variable to that argument. Then I'm going to say, if that fails, then I'm going to say URL, whoops. Put that on the new line. I'm going to say URL equals. And for me, I like using XClip. Now this may not be, you know, when you're sharing a script with somebody, you have to either make a package manager or have your script check. I'm using XClip, which is not installed by default on all systems, but should be in the repositories. So right here, I'm not going to go into too depth with this script, but you would want to do a check to see if XClip exists. And if not, then there's a, was it X S X select or X S E L? I don't know, I don't use it. There are different ways to get stuff from the clipboard. And if you're on termux, you can use termux dash clipboard or something like that. Okay, so here we're going to say that. And then, so we're looking for an argument, saying the URL to that. If that, if we don't pass an argument, we're just going to check the clipboard. Next, we're going to say, okay, dollar sign one, if it does not equal HTTPS colon forward slash forward slash Y and anything past that. I think this is the proper way to write this. Okay, so if it does not, then we are going to say to ask for a URL. Okay, so we're here, we're either getting it from the user input, we are checking the clipboard if that doesn't exist. And then we're saying, okay, now that we have a URL, is it actually a URL that at least starts with why? Because I say why because here we're using youtube.com, but if you actually select the share link, it's actually shorter. So share here, it's going to be, I guess I could go U because it's utu.be. So let's do that, let's just go. So as long as it starts with U, okay? So let's go ahead and we'll copy something that is not. And I will run my script here and I will not pass it an argument. And it should ask me for a URL. Yes, so now I can copy this URL, paste it in there. And I got the title. Now, if I was to run it and I was to copy this one, let's see, websites as Android apps, copy that. We will paste it in here. Oh, nope, I did something wrong because it should. Oh, www, see, we could have a check multiple variants. I'm just gonna leave it as, make sure it's a URL, okay? Okay, so that time it looked at this. It saw that I passed it something and it has HTTPS. Now, if I was to do this, it's gonna ask me for it because it doesn't start with HTTPS. And then finally I could go copy this URL and not pass it anything. But it should see, see, it didn't work. Let's see, xclip-o, okay, it should have worked. I typed something wrong. This is part of the learning process. Okay, we're saying, okay, is there an argument? If so, set that to that. And then here we're saying URL equals this. And then we're saying, okay, oh, that's the problem. This should be URL because we've set the URL. Okay, now let's run it. And it should check my clipboard. Let's make sure I have a URL selected. There we go, I'm gonna check my clipboard. So we have those three different options. You can pass it an argument. If you don't pass an argument, it will check your clipboard. If your clipboard matches at least HTTPS, it will use that. If neither of those things work, it will ask for a URL. And then it will give you the title. So that I think is pretty good. We have three lines of code and a pretty good, I think, script here. So now we can use it however. So I hope you found this useful. I hope you learned something. So again, this is kind of an intermediate media video which I mentioned a few minutes in the video. I should have said right off the bat. So hopefully you understand some of this. But yeah, here we're just checking. Are we passing something? If so, use that. If not, check the clipboard. So again, XClip, XClip can put stuff in the clipboard or show you what's in the clipboard. So as I showed you, if we do XClip-lowercase, oh, it shows you what's in your clipboard currently. And then here we're checking. Okay, if it's not a URL, then ask for a URL. And then we will continue. Oh, what we should actually do is, again, check, does URL have HTTPS in it? If not, then we should exit. And you can exit one with an error if you wanna count that as an error. So now, if I run our script, right, it's gonna ask for something. And if I give it something that's not a URL or just gibberish like this, it will exit. You could set it up to give a message or something, but it's just kinda cancelling out, right? So yeah, that would be the last part for that. So now we have four lines of code. And I think that this could be pretty useful, if not just for getting the titles of YouTube videos, but if you're scraping websites, these four lines could be in your script all the time for making sure that a URL is passed in some way, right? So I thank you for watching. Filmsbychrist.com, that's Chris the K. There's a link in the description. As always, I thank you for watching. Please visit my website, my Patreon page, think about supporting the support section on my website. There's links to everything in the description of the video. And as always, have a great day.