 This video is part of a playlist. Be sure to check out the full playlist in the description of the video, especially the video right before this because we're adding onto a script we made in the last video. So currently I'm in a directory that has a couple of files. I wrote a script that allows me to select those which file I want. So I can say the name of our script dot slash, meaning in this folder, this file, this script I wanna run, file man, when I hit that, it says please select a file and then I can choose a number, such as number six and it says your file is, and it echoes out the file. And if I was to press eight, it would quit. And if I select an option that does not exist, it would tell me to try again. But let's say all the files in here are files. Let's say I want to make a directory called dir one and I wanna put files inside there. So I'll just copy all the files in the current directory into that directory. And then I will copy dir one to dir two, make sure I do it inclusive. So I copy the files as well. So now I can list out the files in there one and the files in there two. So let's say I want to move into directory. So I wanna be able to select a file that's inside one of those folders. Well, my current script when I run it, if I was to choose, in this case, eight for dir two, it tells me I chose that file but doesn't actually move into that directory. Let's make it so we can move into that directory. So I'm going to now edit my script and I'm going to put what I have so far into a function that I can call. So I'm gonna select all these lines and indent them. And again, I'm using Vim as my text editor but use whatever text editor you find most useful that you enjoy. So I'm gonna create a function, I'll call it get file. And instead of having this echo command here, so let's exit out of here. You can see that it says please select file and then we have this prompt here. Let's go ahead and not have that up there. We could have a message up there but we're gonna actually put that down at the prompt here. So let's go ahead in here and we're actually gonna change this to say PS three equals and we're gonna give it this prompt. So that will show up in the prompts line rather than up at the top of the script. Next thing I'm gonna do is I'm gonna clear the screen because I think it's gonna make things cleaner when we call this function. And we are now going to check the existence of a file. So outside of our function, we're going to create an if then statement. So we're gonna say if and then inside braces, we're gonna say exclamation mark dash F and we're gonna say dollar sign file then and close our if then statement. So what this is saying is check if this is a file and then we're gonna say if it's not, so that's the exclamation mark it says. If it's not a file, the file that we're looking at, then run this. And of course the file does not exist at this point so it should return that it doesn't exist. At this point, we will then say a while loop. So we're gonna say while one. So it's gonna loop continuously. We're gonna say do or do and we'll close our while loop. And now we're gonna call our function get file. Now we're gonna check the return of that. So we have to have it, this function change our variable here. So we're gonna say file will now equal the present working directory, forward slash and our file name that we just grabbed up here. Now we're gonna say double braces space dash D file, dollar sign file and CD dollar sign file. So what this is saying is, okay, we're gonna run this function. This function is gonna create our variable called file which will be our present working directory and whatever file or folder is selected. Now that that function has run, we're now gonna check is that a directory dash D says is this a directory, is our variable here a directory. If it is, let's move into that directory. Then we're gonna say, well, if it's a file, so if the file we select is not directory, it's actually a file. What are we gonna do? Well, we're gonna check that and if that's true, we're just gonna break out of our loop here, break out of this while loop. And so we are kind of done here. Let's go ahead and save that. If I typed everything properly, I should be able to run that script now. And you can see that we have moved our prompt down here rather than at the top and now we can select a file or a folder. So if I press four, it says your file is and I chose file three dot txt. But if I run the script again, this time I choose six, which is a directory. It moves into that directory. Now I'm looking at the files within that directory and I can choose whichever file inside that directory. And I can continue on and on. So if I was to move into dir two in this case and make a new directory, I'll call it new dir, and I move it new dir. That's funny. Move into new dir and I will create a file in here called file dot txt. I can say this is a file with some text and I create a second file, file two. This is the second file. It has three lines, anything in there. So now I can go back out and I can run my script again. And if I was to choose eight, which is dir two, and here I was to choose two, which is our new dir. And I was to choose one of our files here. I'm gonna choose two. It says that I chose that file. I can adjust my script file menu that when it's done, once a file is selected, once we break out of that, I can now cat dollar sign file. And that should work. Let's see, we're gonna choose eight, two, and two. And it says your file is and then it listed out the contents. And this is a great way rather than having the user type in the file name where the user can type something incorrectly, we're kind of forcing them to select something from a list. And if they select something wrong, it prompts them to try again, they can always quit. The only other tweak that we could probably do this is that you'll probably notice the first option is always dot, because when we do the find command it's showing dot, which is the current directory. And we could also probably add a little tweak to remove these dot slashes when it's listing the files. But our script does work, it's the basics of it. Again, I'll try to remember to put a link in the description of this video to the script so you can go and download it and give it a try. I thank you for watching this, if you enjoyed this, if you found it useful. Think about becoming a supporter over at patreon.com forward slash mail x 1000, there should be a link in the description of that. Even something as small as a dollar a month is greatly appreciated and really helps out. If you can't afford to spare a dollar a month and you still like my videos and wanna be a supporter, think about liking, sharing, commenting, and subscribing, that really helps out a lot. Comment below, let me know what you think. If you did like this video, be sure to give it a thumbs up. Be sure to visit my website, filmsbychris.com. That's Chris with the K, there should be a link in the description. And as always, I hope that you have a great day.