 Hello, and welcome. Today we're going to be looking at basically selecting files, getting the user input to select files in a few different ways, and finally we're going to end up using FZF, which is a fuzzy finder, which is a great little program that I've been using in a lot of my projects lately, and I'm going to be doing a lot of videos on it in the future. So I thought I'd show a basic example of it here, and it has a lot of benefits to it, and it's cross-platform. You can install it on Windows and whatnot as well, but what we're going to do today is we're going to write a script that converts given input files into grayscale using image magic. So here's a list of JPEGs in this directory. I can xdg-openxdg is a program that if you're using xorg that will open up whatever program is the default program for the file type that you're giving it, so I can give it this image and open up in my viewer, and I can scroll through these photos. So yeah, we're going to be converting some of these to grayscale, and let's go ahead and I have three scripts here, so three different options of doing that. So the first one is 1.sh. Okay, we're going to get the shebang line here, then we're saying a variable for our output directory, then we're making that directory. I'm using the dashp option here, and I'll show you why. So if I was to make a directory and call it output, not a problem, but if I try writing that command again, I'm going to get an error. The dashp option, one of the functions of it is it will not create the file, the folder if it already exists, so we don't have that issue. We're then after going to create that file, we're going to use this double brackets and a dollar sign in here. What that does is it checks to see if the user gave an argument, in this case, a filename. If they did give it, then we're going to set the variable img to whatever file they've given. If it did not, that's what this pipe pipe means, it's then going to use the read command to ask them for a filename. Then we'll echo out what file we're converting, and we'll convert it into that directory. So let's go ahead and give that a try. So 1.sh, and if I give it a filename, so if I was to give it this one filename, we'll run that, it converts it, I can then xdg open from our output directory, that file, you can see it's been converted. I can also give xdg our directory, and it will open up directory, and I want to point out, I don't know why this happens, but when I convert images to grayscale using image magic, the thumbnails still show up in color, but I can double click on this and show you that it is converted to grayscale. I'm not really sure why that happens, I'm not too concerned about it. Okay, so that was option one, but what if we want, oh, left out part the functionality of that, so we can give it a filename like we just did, or if we don't give it a filename, it will ask you to enter a filename at which case we can enter a filename, and it will convert it. And remember, image magic automatically overrides any files if they already exist, so be careful with that. We're going to look at our second option here. What if we want to give more than one file input? So we're going to do again, we're going to set our output directory, make that directory, we're going to check to see if there's at least one argument. If there is, they're going to check all the arguments, all the files given, and put them into a variable called images, or imgs, yeah, imgs, imgs. If there isn't at least one given, then we will ask the user, we'll prompt them to enter it again with the read command. At that point I'm going to take those, and right now they're separated by spaces. I'm just converting those spaces to new line characters and using a while loop. There might be a better way to do that, but that's the way I'm going about it now. So we have a list of images, then we're going to go through each one as an img, and run the same commands as we did before. At the end I add it after they're all done, we'll open up that directory in whatever our default file browser is. So that's sh2, or 2.sh. Again, I can give it one file name, like so, and it runs fine, opens that up, or I can give it multiple names, and I can tab complete here, and choose a file, I can tab complete again, and oops, choose another file name, and I'll do that and it converts all three again. They're showing up in color in the thumbnails, but they are all converted to grayscale. Great, we can also say 2.sh and I give it a file, and I can paste in a file name and it works, or I can give it no name there, and I can with spaces add in file names. That's great, but what would be nice is, one, I could type and search, so all these names pretty much similar things, but let's say I had files that have unique names, I can use fuzzy find or search them. It also prevents errors on the user end of typing in file names that don't exist that can cause errors. So again, if I was to do this and I was to give it one of these file names, but then the next options, I just give it random stuff, we're going to get a bunch of errors. It converts the one fine, but we'll get errors because they don't exist. We can avoid that. Again, anytime I can, I avoid the user manually entering data. I like them to be able to select stuff from lists, so let's go ahead and look at our last piece of code here which uses fuzzy find or fcf. So again, same line at the beginning, that the shebang line saying this is a bash script. We're creating a variable for our output directory, creating that directory, checking if there's at least one argument. If there is, then we're going to put all of those into a variable called imgs, just like we did in the last thing, but now instead of using the read command, which allows the user to manually type in a file name, we're going to list all the jpeg files and pipe that into fcf, and we could be done there and select one file, but we want to give them a prompt so they know what to do. Files, we can say whatever we want here, we can say select images, like files, whatever, and then we're using the dash m option. The dash m option allows them to pick more than one file or one more, more than one item from the list using the tab key, and then the rest of the command is the same. So the only thing we really changed here is instead of using the read command, we're using fcf. So let's go ahead and quit out of that. That's called 100. Again, I can give it a couple of files here like so, and it works, opens up the directory, or I can just say 100 and it's going to list all those jpegs. You can see that there's nine of them. It lists it right here. We have our prompt saying files. I can start typing. I can type like 2019. Now this file here is the best match because it has 2019 in it. These other ones are showing up because they still have 2019. That's the fuzzy finer part of it. It's not looking for an exact match. It's looking for the closest matches and putting them in the proper order there. So now I can hit enter and we'll choose the one I'm on, or let's go ahead and do that. So it converted to one. Now I'll run it again, and again I can filter it. I can say 2019, but if I want to, I can go tab, tab, tab, and select the ones I want. So I selected three of them there. You can see there's nine total. It's displaying five of those nine, and I've selected three of them. I hit enter and it converts all three of those. And I can go ahead and run that, and I can just hit tab a bunch. Now I have all of them selected, and I can convert all of them. Again, thumbnails are showing up in color for some reason, but the images are converted to gray scale. So that's just an option. Again, there's other options out there for this particular application where we can actually display thumbnails, and allow you to select those thumbnails. One of the benefits of this is we are not relying on any graphical interface. At the end, we're opening up the image, the directory in XORG, but ignoring that, we can select and convert them all on a server where SSH into, and we may not be forwarding X. So that's one of the benefits of this. But again, I try to avoid allowing the user to type in anything that is going to be entered into the program when I can have them select from a list. And we're going to be using this a lot in future videos where we'll have lists of names. So if we're creating forms where you're selecting employees or customers, you don't want people typing names. For example, my name is Christopher. I usually go by Chris. And if we're going to be entering stuff into a database, we want to be consistent. We want to either always be Christopher or always be Chris. And I spell my name with a K. People, someone else typing my name might type with a CH. And then we're trying to search through database or log files, and things are inconsistent. We can be consistent by giving them a list of names that they can choose from rather than manually typing them in. So stuff like that. We're also going to look at creating menus so that you can select different items and menus that run different functions in our shell script. So we're going to be doing a lot of stuff like that in the future. But I thought this was a basic example. And the future, like I said, we're going to create one that has a menu. And the menu will allow you to kind of do this where it converts grayscale. But the menu will allow you to select images and what you want to do to them. So maybe you want to scale them. Maybe you want to grayscale them. Maybe you want to send them into a sepia tone. So we're going to write a shell script that does all that with different functions. But the user will be able to select everything from a menu-based thing using fcf. And fcf is a small application. It's portable. There's a Windows compile for it. So you can use it on a lot of different systems. I use it on my phone in Turmux. Makes it easier for me to select files instead of having to type everything out on the little keyboard. I can just click on an icon on my desktop. It can bring up a list of files. And I can select from those. So it's a very useful program that runs on pretty much everything. And makes things super duper nice. It has lots of functionality that we just looked at some basic options of here. So I hope you enjoyed this. I'm going to put a link to the script, the final script in the description of this video. I'll have it up on Payspin. I thank you for watching. And as always, I hope that you have a great day. Filmsbychrist.com. That's Chris of the K. Check out the links in the description for that. As well as links to my Patreon page. Patreon.com. Ford slash Milox 1000. I thank you for watching. And for your support, have a great day.