 today we're going to do something rather simple but we're going to add lots of functionality to it and we're going to create a shopping list as a bash script or a bash script as a shopping list or a list for anything obviously this could be very simple you could just use you know a text editor and put your stuff in there and really you don't need a script but we're going to add some functionality to not only put stuff into a text file but read back that list remove items search through the items this is something that is somewhere between I would say a new user and a medium user because it's a simple thing but again we're going to do a little more advanced to add functionality to the script so let's go ahead and get started let's look here first off I want to mention that this entire script is going to be available at Payspin there's the URL I will try to remember put this in the link in the description of this video so go ahead and go to that to get a copy of the script but let's just see how it functions I am in a directory here and I have my script called my list dot sh and when I run it it shows me items on my list right now just milk I can also create a new item or I can just quit so let's go ahead and add a new item we'll just say bread boom a new item has been added see now we have milk and we have bread I'm going to add another item we'll just say what's eggs there we go and now I can go to an item with my arrows or I can type eg and it narrows it down to eggs I can hit enter and it says do you want to remove and it says eggs in red uh y or n and y is capitalized so I have that set up if I hit just enter now it will automatically default to yes I can hit enter and you can see if it's removed if I choose bread and then I can say n it then continues without me having to hit enter and it uh did not move bread so let's have a quick look at this script I'll use vim I'm going to use vim as my text editor use whatever text editor you'd like for writing scripts and here's our script it's not very long it says it's I think like just under 60 lines but you know 20 of those or so 17 of those are just the headers so really it's about 40 lines of code and uh yeah I've divided things up into functions about five functions here so I'm going to start writing this so you can watch me as I create it you can see here uh well let me just go ahead and well let me stay in here and I will delete my whole script first off we'll start off with a shebang line bin bash uh now we want to where we're going to save and we're just going to use a text file to save our our list too I'm going to save it to our home directory you could probably think of a better place to put this uh but I'm going to say my list uh dot list and I start off the file name with a dot so it's hidden again you could probably pick a better place to put this but I'm just putting in a home directory now if that file does not exist we're going to say touch list so well either way if whether it exists or not touch is just a command that you know updates the file if it doesn't exist it will create an empty file so if our list does not exist we're going to touch it we're going to create it uh right now I'm also going to create two variables here that um we're not going to use the later in the script but red and normal these are going to allow us these are codes to color code the text red and then so we can make a word or multiple words red and then the normal will be a variable to set everything back to the defaults I am going to use functions here rather than writing everything in a long list so I'm going to create a main function now as I always say when you create a function in bash you can type in something like this right you don't need this word function right uh but I think it looks cleaner to have the word there I like having our main function near the top of the script the thing is uh bash does read things in order so if you call functions after the main function uh you I'm sorry let me rephrase that you call the main function at the top it's not going to know about the functions after it so what I normally do is I create the main function up high and at the bottom of my script I call the main function and then I'm also going to say dollar sign asterisk that's going to pass any variables passed to our script to that main function I forgot to show you I do have a an argument that could be passed to our script where we type print and it just prints out the list without bringing up the menu uh so let's go ahead and what is our main function going to do well it's going to allow us to either print the list or open up a menu and then have different options with the menu based on what is selected uh so let's go ahead and just to save time I don't want to type everything out let's go ahead and create our menu function I'm just going to copy and paste here but then I'll talk about it so I have a menu function here what's that going to do well it's going to create a variable called l and what's going to do is going to take our list our text file and it's going to sort unique it that means it's going to put everything in alphanumeric order and the dash u is unique meaning if there's any duplicates don't show it more than once okay then we're going to put into a variable called item and that's what we're going to select we're going to echo with the dash e option that allows it when you put these forward slash ends in here I'm sorry backslash ends in here those are going to count as new line characters so this is going to create our menu we're going to have a quit option new item option and then it will list everything that is on our list text file we're going to pipe that basically put all that information into a program called fzf which I talk about all the time on my channel it is an amazing program for filtering and searching through lists and that's what we're using as our menu here so if I was at this point I can say menu here and if we x add that first time we run it we're going to want to change mod plus x that makes it executable dot slash the dot means in the current directory run this script if we run this it's going to print up that list I can come up here I can select an item and I put that into a variable but didn't do anything okay so let's go back into our script and let's go ahead and create another function before we continue with our menu let's create that function that just prints the list okay so we're going to create this function here it's print list all it does is well use a sort unique to print out our list now the numerical order again removing you duplicates and then it's going to exit out of the script so what we can do here now is under our main function before we call the menu we're going to do this we're going to again to save time I'm just going to copy and paste we're going to check the arguments the first argument passed to our script which then will pass to our main function if it the word is print if the argument is print well then what are we going to do we're going to run the function print list let's go ahead and save that let's go ahead and run our script again if we run it just normal we can select item put that into a variable hasn't done anything with it yet but if we type in print as our first argument it's just going to print our list and exit out of the script so now we have that we can view what is in our list either just by printing it or as a menu and now we need to start doing stuff when we select items from that list so let's go back into our script and what we're going to do now is okay so we're going to run our script it's going to check did you add the print argument if so print the list which will print the list and exit if it doesn't exit well then it's going to run our menu and we're going to list the stuff in the menu or create our menu and then we're going to take whatever item we select for the menu whether it's an item from our list or the word quit or new item if it's if once we select something from that it's going to put it in the item variable and now we need to check that variable so let's go ahead and just do our parentheses dollar sign item if I could type today equals rush that's the next line we're going to say this and then we're going to say exit one what does this do okay it's checking is there something in empty in item one it's basically is it empty if it is empty so if it exists do nothing or continue if there's nothing in that well that means it failed that's what the the pipe symbol means it says you know is it true that something exists if not then just exit that means we selected nothing from the item so what does that mean if we come in here and we run our script if we choose either this empty line that's always here or we type in a bunch of stuff that that isn't in the list so we're selecting nothing it will exit out of the script with an exit code of one meaning our script failed means we didn't select anything let's go back into our script okay so if we did select an item if item is not empty what are we going to do well first we want to check okay does that item equal new item okay if that is true so we're saying okay if it equals true the two ampersands means the last statement was true so if we select new item which is an item that is always in our menu we're going to run the function new item well what does the new item function look like let's go ahead and add it down here again to save time I'm just going to quickly copy and paste and so what's it going to do well if we're going to add a new item we're going to use the read command we're going to give it a dash p prompt and it's going to say new item and then whatever we type it's going to put into the variable new item then we're going to take what was typed and add that to the bottom of our list the two uh greater than symbols here means append to the bottom list once we've added item well then we're going to run main again so we're going to go back to our main function here and it's going to continue so basically it's going to bring us back to our menu because we're not going to be passing the print option at that point okay so let's go ahead and give that a try we're going to run our script if I choose new item it's then you're going to say new item and I'll say uh I think we've removed eggs so I'll add eggs back to our list and now you can see eggs is in our list I can add another new item and I could just say I don't know uh grapes and now grapes is on our list great we can add items but still nothing happens when we select an item from the list uh choosing to remove them so let's go ahead and continue forward uh we had another option in there uh so very similarly we're going to now again check our item variable and if it equals quit which again is in our menu here so if we select the word quit also if you're new to scripting uh to bash scripting realize that I'm putting spaces here that is important if you don't have these spaces and bash is very particular about its spaces in that aspect so make sure that you're writing it just like that uh well if we choose quit then we're going to exit but this time not with one that means we exit but we successfully exited we wanted to exit there wasn't a failure uh okay and now we're going to say item select okay and that's going to run the function item select let's go ahead and create that function down here again before our main function call and what are we going to do here okay so if you selected an item we're going to use the read command okay and we're going to dash p prompt do you want to remove and then we're using that variable red remember at the beginning we created this variable up here that tells it basically anything past uh this variable make it red and then we also have our normal which will set it back to the normal colors for your prompt for your uh shell so we're saying do you want to remove color at red the item that was selected set it back to normal from the list then we're saying yes or no and we see capital y I put that in there most programs the letter that's capital is the default and whatever you choose whatever you type this point is going to be put in the variable called a now notice I have this n option dash n1 that means one character that allows you to hit y or n or any character but it's going to accept just one character because I don't like when you can have to hit y for yes and then hit enter that's too many keys right so we want to take one input okay then I'm just saying echo here basically to create a blank line uh just for uh you know the way it looks on the screen okay then we're going to check the variable uh a the response okay if it does not equal n that's kind of weird the way I wrote that if it does not equal n so we're going to continue and then we're going to remove the item from the list we're using said said and then we're doing dash i in place and we're taking our text file which is our list and we're going to remove something with the word with our variable of item now notice I have the greater and not the greater than the carrot symbol pointing up here and our variables also followed by the dollar sign this is saying the from the beginning of the line this variable to the end of line this prevents you from accidentally deleting more than one thing if there's partial matches uh so let's say you had bacon on your list and you also had canadian bacon on your list uh if you went to remove bacon and we didn't put this carrot symbol here and this dollar sign here it would remove both canadian bacon and bacon from the list because it's seeing the match bacon but here it's saying okay from the beginning to the end of the line it's got to be an exact match the entire line so that will prevent that from happening and so I wrote this like this another way to write this okay I could have said if it does equal n I could have said then do this but then don't do this it's kind of weird when you're working with negatives but here I'm saying if it does not equal n because n is the only option for not removing because the default is y so basically I'm not checking did you press y did you press n I'm saying okay if they did not press n then go with the default which is y to remove so we're going to remove the item and then go back to our main function and that is our entire script so let's go ahead again and look at our script we can again say print and it will just print the items in our list if we pass no argument well then it's going to give the list as a menu with options I can hit quit and it quits or I can say new item and I can type something whatever it is uh and it brings me back here I can add another item I can add another item if I was to add something that already exists it's not going to I've just misspelled eggs let's let's go if I add something that already exists it's not going to duplicate it because we're we're sorting and uniting that I can remove an item like this um and again to remove an item I can just select it and hit enter twice if I want to if I accidentally select something then I can n and it won't remove it and again if your list gets real long you can filter by typing so I just typed in sdk and it brought up the line it says sdk and then I can remove that yes remove that I go sk remove that item I can go milk remove that item and then I can remove that item I can say new item and I can add something like this and then if I want to remove an item I can then go up to it again with the arrows and remove it and at any point I can hit quit or if I was to choose this blank line it'll quit out of our program so that is our script again I will post the url but there should be a link in the description of this video to the paste bin you can get this exact script there uh play around with it learn something uh but again this is kind of a uh not beginner beginner uh but I think it's a little simpler than intermediate uh because we're doing something we're just writing and reading from files but I just showed you how to add more functionality to that uh to where basically you're reusing fcf and creating menus using fcf and color coding stuff and we're actually using functions which I think is a good habit to get into uh when once your script's starting getting longer that way uh you can do loops like this where you're picking you know going from the menu to adding a new item back to the menu selecting an item back to the menu so I hope you found this useful and I hope that you uh check out the script again on paste bin right there and as always visit filmsbychrist.com that's chris the k there is a link in the description to my page I also have a patreon page patreon.com forward slash mail x1000 I love support would be awesome but I also just appreciate you watching my videos be sure to uh visit my website there's lots of you can search through my videos there you can get links to my scripts and different programs on paste bin or github or I have a whole um note section with a bunch of different scripts that actually search through my paste bin where you can search through them for certain things so I have probably hundreds and hundreds of scripts up on paste bin and I have many many github or sorry git lab projects so be sure to check those out you can also support me different ways and I hope that you have a great day