 So, a few weeks ago, I got this comment on one of my videos. Oh, no, not that one, although I hope that I fixed that issue. This comment about writing a script where you get the input from the command line, which is something I normally do on my scripts, but in the tutorial that wasn't the focus of it, so I just had a read me command, or now read me command, a read command run. Well, today we're going to look at how I do it, and I don't just grab what is passed from the command line, the arguments in the shell. I actually do a few checks on different things. So we're going to do an example today where we're going to give a script a URL, and it's going to download the video using YouTube DL, but it's going to check for the input in different ways. You'll see what I mean. Let's go ahead and get started. Okay. Again, I'm going to use YouTube DL as an example program in this. Basically, we're going to put it in our script and have it take the user input and download a video. Obviously, YouTube DL works as loss options, but if you just give it the URL to a YouTube video, it also works with many other sites and hit enter, it will download that video for you. There we go. So I just downloaded a video I did a couple of weeks ago, my Mason jar video, Blender and Mason jar. So you have to check that video out. Okay. So that's how it works. So let's go ahead and make a script. I'm going to call it my script, spelling it properly, script.sh, and of course it's a bash script. So we're going to start with the shebang line for bash and for this script, if we just call the command YouTube DL and we can give it the URL like this, then we're going to exit out of our script. Again, I'm using a Vim as my text editor, but you can use whatever text there you want. We're going to make the script executable and we're going to run it. And it's going to run the videos already downloaded. So it's going to check and tell me that it's already downloaded, but it ran properly. Let's go back into Vim. And the way we would get this to use the user input from the command line as an argument is we're just going to grab the first argument. So dollar sign one. Now if I run my script and I give it that URL, it will run just as before. But if I don't give it input, it's going to throw an error because there's no not a valid URL there. Great. So let's improve upon that greatly. So first off, instead of just passing the first argument to YouTube DL, let's create a new variable and just make it equal to dollar sign one. And then we'll put this in URL here. And so again, that will run the same. I can get out and run that. And if I give it a URL, it will download it. If I give it a bad URL or no URL, YouTube DL will fail. But what I want to do is I want to start off by checking and making sure that a URL is passed. So I could do something like this. I can say if dollar sign URL, and again, URL just equals dollar sign one. It's just naming the variable so we know what it is. I could say something like this and then I can say exit. And not just exit, we want to exit one. Why do we want to exit one? Because that means our script has failed. If you're going to run this script within another script and you want to make sure that your script ran properly, if you just do exit, it will exit out of the script. But it will tell the system that it exited successfully. But we're unsuccessful here. So we're going to exit one so we can check that with other scripts. So now if I do my script and I give it a URL, it will try to download it. Again, it's already downloaded. So it's going to tell me it's already downloaded. But if I just run my script with nothing in there, it just exits. No message or anything at this point, it just exits. Let's go back into Vim. And you could write it like this, but I prefer to use some shorthands. So we don't need the if, we don't need the then or the end of the if then statement. And we don't even need the equals blank because if we just do this, it's going to check is there anything in that variable. If not, it fails. And if it is, then it's going to continue. What I'm going to do here is I'm going to say the pipe pipe symbol, which means the last command failed. So it's checking. Is there anything in this variable if not exit? So that's a much cleaner, shorter way to write it. Again, I can run it with nothing. It's just going to exit or I can run it with a URL and it will continue great, but not perfect because I can still run this script and put some gibberish in there. And it's going to try to download that gibberish. So what I would do in this case is I would add back in that equals equals. And I would say check and make sure that what is passed starts with HTTP. So we're going to say HTTP asterisk. You can also check if you're looking for a certain string within that string put an asterisk there. So if you wanted to check, does it have YouTube in the URL, you can do that. But again, YouTube DL works with multiple sites. So we're just going to check, make sure that we're giving it a URL. If it's an invalid URL, YouTube DL will let us know, but at least we're checking that. So again, now I can run this script. Let's clear the screen without anything and it will exit. If I give it gibberish, it will exit. And if I give it a URL, it will continue properly. If you are getting there at this point, depending on how your shells set up, you might want to put the URL in quotation marks. I usually do that just to be sure some shells might get upset with the special characters in a URL like this. Let's go ahead and go back into our script and improve upon it even more. Okay. So we're checking, does URL have HTTP in it? But that's what's passed to our script. Not if nothing is passed, but I have copied a URL from my web browser. So I'm just going to copy that line. We're going to check, does the URL that's passed as arguments have HTTP at the beginning of it? That's going to be the first thing. If you run the script and you pass it something, go with that. Then we're going to say, okay, if it doesn't, instead of exiting, what we're going to do is we're going to reassign the URL. And in this case, I like to use a program called XClip. There's other programs that work similarly, but XClip will let you put stuff into your clipboard and retrieve stuff from our clipboard. And if I just do XClip-O, it's going to give me the output of my clipboard. And you do have more than one clipboard on Linux, so make sure you're checking the proper clipboard. But that's going to check the default clipboard. So what this is saying is, okay, look at what the argument passed to the script is. Does it start with HTTP? If not, check what's in the clipboard. So if you've copied a URL, it's going to go with that. It's going to check that. Does the clipboard start with HTTP? Okay, if not, then exit. So now what we can do is if we run our script with gibberish, it's going to exit. If we run with nothing, it's going to exit. If we run with the URL, it's going to run that. But if I was to copy this URL, I can run this script without any input, and it's now going to check my clipboard. So it's going to check if an argument has passed and that it's a URL. If not, then it's going to check my clipboard. This is super useful because I have a script like this on my machine that uses MPV and YouTube DL to play a video. So when I'm online, if I'm searching through YouTube, I don't usually click on the video to play it within my web browser. I copy the URL and I hit Windows key, my modifier key, and Y, and it plays it in MPV rather than playing it in the web browser that avoids any ads. It also gives me the player, the video player that I like and the keyboard shortcuts that I like that I can speed it up even more than the YouTube player. Anyway, that's a whole other thing. We're not done with our script though. There's a few other things I would like to do. So we have now checked the input from the arguments. We then checked the clipboard and then it exits. But what we want to also do is check if both of those fail, give the user a chance to enter a URL. So we're going to use the read command dash P and I'm going to say, please enter a URL and I'm going to put that into the variable URL. So now what's going to happen is, let me copy something here so that I don't have, whoops, don't have that URL in my script. So if I run the script with a URL, it's just going to take that URL and download it. If I give it something that isn't a URL or if I give it something that's not a URL or something that's blank, it's going to check my clipboard. Right now my clipboard doesn't have HTTP in it. So at this point it's going to ask me for input. I'm going to copy that address from up there, paste that in there and it's going now use what the user put. So I can now pass arguments to it. If nothing is passed to it or something is passed to it that's not a URL, it's going to check my clipboard to see if that's a URL and try to download that. If both those fail, then it asks the user to input something, but our script also still continues to check. So if I run this again and, oh, let me copy something here because I already copied that URL so it was in my clipboard. So now I'm passing it something that's not a URL that can be gibberish or it can be blank. I don't have a URL in my clipboard so it's going to ask me for a URL. If I put in gibberish here or leave it blank, it's going to exit out. Great! Our script is pretty much done. Although we kind of do want to tell our user how to use our program. So what we could do is in our script, what I would do in this case is I would create a function. I always start off my functions in bash with the function, the word function. You don't have to. I just think it's cleaner looking. You can also just select, I'm going to create an error function like so. You should be able to just do this, but I think it just looks better and it's clearer what's going on if you say function. And so at this point, I'm going to give a little message, usage, usage, yeah, yeah, no E in the middle there, usage. And then we can give the name of our script, but if the script's name is changed, like someone renames the script, you want it to show whatever the name of the command is. And they could also be using aliases. So what we're going to do here is we're going to say $0, which is your first argument, which is whatever the command you're running is. I hope that makes sense. So right now our script is called myscript.sh. If I was to put in here myscript.sh, but later on someone renames the file, this usage isn't going to make any sense, but if we use a variable of $0, no matter what they name the script to or alias to, it will show here. And I'm going to say URL. And then I'm going to say, I like to give them an example. So I'm going to say example, $0. And let me just again, copy that URL. And then at that point we'll exit one. And down here instead of exiting one, we are going to say error. So now if I run my script and I don't have a URL in my clipboard and I didn't pass the URL, it's going to ask me for a URL. And if I give it gibberish or blank, something that isn't a URL, it will then tell me the way you use this program is like this. Here's an example. And I should be able to copy that and paste it in. And it should work because my examples should function. Yeah, so now they know how to use my program. They can see that they can pass it a URL. I could also put in that message that it will check the clipboard. But anyway, that is a lot of people will just make a script that checks, oh, it was something passed, if not exit. I like to do stuff like this. I like to utilize the clipboard a lot. And again, like I have, right now if I was to highlight this and copy this URL on my keyboard, if I hit Windows Y, it pops up a little message saying it's loading a video. Right now it's grabbing. It got the title and it starts playing my video. So anytime I want to play a YouTube video or any of the many other websites that YouTube DL supports, I just have to copy the URL and hit Windows Y and it runs an MPV, which is my preferred video player. Something else I do that we didn't do in this because we're working with URLs here is if you're doing file selection, you can have a check the clipboard, you can ask for the input, you can get the URL. But another thing I would do is instead of saying please enter, what I would use is the FZF command, which I've gone over many times in the past and I use in so many of my scripts. But FZF, if you use it by default, it's just going to start listing the files in the current directory and subdirectories. So there's two files in here and it will return that. You can also use like the find command find and pass that to it and then you can choose a file. So I'll do that if my script is looking for a file name. I'll have it check the arguments, maybe check the clipboard, depending on how the script runs. But if those fail, then I have it will run an FZF command on the folder, the directory, either the current one or the one that that script is looking for and list all the files. And that way, if there's hundreds of files, the user can easily search through them and it will use that rather than a read command. That doesn't help us with the URLs because I don't have a bunch of URLs to search through. Anyway, just food for thought there. I'll put this script up on a paste bin. I'll try to remember put a link in the description of this video. And I hope you found it useful. So again, I'll just post it up there. Very short, very simple. We have three lines. So three chances to get the proper input from the user. Anyway, thanks for watching. My website is filmsbychris.com, that's Chris the K. There's a link in the description. I also have a Patreon page that you can check out if you want to support me. There's also other ways to support LibrePay, Paypal. Check out my website. There's links to all that as well as in the description of this video. I appreciate you watching. I appreciate your support, and I hope that you have a great day.