 Okay, what are we going to be doing today? We're going to be taking a bunch of functions and making a script with those functions that has a menu so you can select which function you want. In this example, all my functions are like one line. Usually if you have a function, it's more than one line. Otherwise it's kind of pointless of having a function, but it's just as an example. So yeah, so we're gonna have a searchable selectable menu that lists all the functions we have in our code. So I have a starter code here. So let me go ahead and just cat this out. So it's called a runfunk because we're gonna run a function and this is the entire script here. Let's go ahead and actually open that up in Vim. Okay, excuse me. So I have a function called IP that gets my current wifi IP address, SSID that will give me my current, the SSID of the current wifi I'm connected to, date format that will give me the date formatted out, runtime that will give me on my uptime but just the time, not the rest of the information. And then actually, let me go ahead and delete both of these because I'm going to add those in later. And as you can see at the bottom of the script here, we have IP which is actually just going to run the IP function. Because if I get rid of that line and I just have the functions and I already have this executable, I'll make it executable just to be sure. I am going to run that function and nothing happens because it loads all the functions but then doesn't run any of them. So go back into there and at the bottom here, I'll run IP. And now if I run that function, it will give me my IP address. If I go back into there and change it to, we'll say runtime. Now it will run the runtime function. And of course I can give it more than one. I can say runtime and IP and it should run the runtime and the IP address function. Okay, so we have that. How can we make it so that we can actually select one of those functions with a menu? And of course, if you watch my videos, you know I love FZF, the fuzzy finder. And although it's not installed by default on many systems, it's available for pretty much every system, every operating system and architecture that you probably are going to be using. And it just makes it so easy to make a list of things that's searchable. So let's go ahead and go back into our code here. And also you notice usually when I make functions, I put the word function before it. You don't have to as I'm showing the example here. But not only do I do that because I think it makes it a little bit clearer. This is the beginning of a function. We're actually going to utilize that in a little bit. But let's go ahead and delete these two things. And what we can do is we can actually pass the argument from the command line. So I can just say dollar sign one here and that's going to be the first argument. So if I was to rerun the script now with nothing, it doesn't do anything still. If I was to run it again, but say IP, it's actually going to run that IP function. If I say running time, is that what I call the other function? It will run that function. It will actually run any command. So if I was to actually paste in LS there, it will actually run LS because it's just running in the script here. This is like a command. It's running whatever the first argument is, okay? So we can do that. But again, we want a menu. So what I could do is I can use the echo command. If I wanted to, I could say echo. And I'm going to say dash E, which means recognize special characters such as the new line character. And I'm going to say IP backslash n SSID backslash n date format underscore format backslash n. And I can say running time. And when I run that, actually right now it's just going to echo out that, but I just want to show you when I run that, it's going to list them like that because I use that new line character. I could also have wrote it, echoed out multiple lines. We're going to do that like this. And we're going to then after that, paste that into FCF, right? So we're going to now run this. And when we run it, we get this nice little menu that we can search through and select something like that one. And it just prints the word. But if we were to go back in here and say eval and then dollar sign parentheses around this, it's going to again echo out this list into fuzzy finder, FCF. And then when I select one, it's just going to print that information but it's going to print it as an argument for the eval command, which is going to run it as a command. So now when I run our script, it gives me this menu and I can choose one of our options and it'll run it. So there's date format, runtime, SSID. You get the idea. That's great, but let's say I want to add another function. So I'm going to add another function here. I'm going to say hello, and then I'm going to say echo hello world. Now I run this, hello is not an option. So I have to go back into the script here and add it here. I'm going to say, let's go in, hello. Now it's there and I can go up to it or I can quickly just type HE like that. So I can go like that, I can type an IP, I can go like this, I can type an H and I can enter, I can go like this, I can hit D and hit date. So that's good, but every time I add a new function, I have to add something to that menu, which isn't the end of the world in certain cases you're going to want that. But another option you could do is one, as I said earlier, I like to have the word function at the beginning of each of my functions like so. Not only do I think it looks cleaner, but what it's going to allow us to do is search through our script for functions, right? So instead of all this, what we're going to do is we're going to say grep from dollar sign zero. Dollar sign zero will paste in right there the name of our script. So I actually don't want grep dollar sign zero, I want grep and then I want to say carrot symbol function. And so we're going to look at the current file and find all lines that start with the word function. It's more that it starts, if one of them is indented, it won't find that way, you can always do function, but then if the word function shows up somewhere else in your script, it could be an issue. But we're going to then pipe that into Oc. Well, let's just see what that does. So now I run our script and it lists all our functions with the word function and basically everything on that line. So what I'm going to do now is I'm going to take that and I'm going to put that into Oc. And I'm going to say print. I'm trying to blank here. I want to put this inside here. And I'm going to say dollar sign two. So that's going to print the second column, which will give us the name of our function, but will also give you the parentheses and the curly braces or brackets or whatever you want to call them. And so I'm going to say cut with a delimiter of the opening parentheses dash F1. Now if I run this, it will list out the name of my functions without all the extra stuff. So again, now I can take that, pipe it into FZF and I can say evolve and put all that inside dollar sign and parentheses. And if I typed everything right, I can now run our function and it lists all my functions. I can choose one like hello. Now though, if I go in here, if I create a new function, function by, and I say echo, goodbye. Let me put a little dash in there. There's different ways you can write goodbye. Right away, bye shows up and I can use it. Now, you still might want to pass arguments. Maybe you don't want to go into the menu. Maybe you only want the menu if there is an issue with not getting an argument. So what we could do is we come in here and what we're going to do, let's make this a little smaller here, I am going to say inside these double brackets with spaces, I'm going to say dollar sign one. So this is going to say, is there a first argument? Okay, if there is a first argument, what do we want us to do? Dollar sign one. So if there is a first argument, so this is the ampersand ampersand is saying, if that's true, then run that argument. If that was to fail, then create our menu. So now if we were to run this, I can say our script without any arguments, without saying it what to do, and it'll just give me the menu and I can type in like bye and hit enter there. Now I can also come in here and I can type in the word bye and it just runs by. And I can say hello or IP. Now, if I was to type something that is not one of our functions, it is going to try to run it, tell us it's not a command and then quickly dump us into our menu. So you don't even see that message until we get out and then you can see that it did say that their command not found. So you can always dump that into DevNull if you don't want that to display. So we'll run our functions if they exist. If not, they will go back to the menu. So I'll put this code up on Payspin so you can check it out. But again, I can create as many functions I want in here and our code will create a menu for all of them. We can still pass arguments from the command line but if the argument fails or if they don't pass anything, we will get a menu and with all our functions. So I hope you found this useful. I use this in a number of my personal scripts at home. It just makes it quick and easy to run particular functions if you have one command. For example, I have a command on my computer called music. When I type it, it will give me an option to play my local directory or I have a list of playlists or they're local and I also have some YouTube playlists that it plays through and then I also have an option in that menu to switch to that directory. So I can type in music play and we'll start playing my local folder randomly. I can say music search, which runs a function that I can use FCF to check the music and search the music, take that and it dumps it into a playlist and starts playing that playlist. And the next time I can type in music, replay and we'll replay the last playlist but if I don't type anything it'll bring up menu with those options. So this is the sort of thing that I do all the time in my personal scripts. You might find it useful as well. So check out the link in the description to the code and if you have any questions, let me know in the comments below. If you have any improvements, I'd love to see that. But I think it's pretty straightforward. You know, the only other thing I might do here is like the FCF's command. You can say dash dash prompt and you can say select a function. You know, it just gives you a prompt message, select a function or command or whatever you wanna call it. So now when I run the script it's going to say down here at the bottom, select a function. So they know what they're supposed to be doing but the list might also make it obvious. The only other thing that you might wanna do is like if we select something from there that doesn't because you're still gonna get your little command does not exist. So that works as well. You might wanna handle those errors a little different. So I thank you for watching filmsbychrist.com. That's Chris K. There's a link in the description. I hope you have a great day. This is my thumbnail for the video. Have a great day.