 Today we are going to be looking at some real-world programming. This is something as someone who writes or creates tutorials for Programming who go, oh, can you give me a real-world scenario for that? Well, a real-world scenario is whatever your problem is. So we're going to write a program here a bash script today Using the rules I've talked out in my previous video, which I'll talk about in a moment But basically we're going to do something that I have to do at work all the time Although I think the solutions have gotten better. We still don't have great solutions as a firefighter I need to make sure that our medical supplies are always stocked. So for the station or truck I have to make sure that we have so many gloves so many masks so many Band-aids whatever and somebody I do regularly in the interfaces like I said have gotten better But aren't perfect today. We're going to look at doing it with a with a bash script Which is something is the way it should be done, but the real-world scenario scenario Unfortunately, most people don't use text interfaces even though in many cases for something like this or far spirit to a GUI interface In a future video, we'll look at making GUI interfaces using HTML and Java script And then we might even do redo it again the same code again using Godot and so you can compile stuff or package stuff for Windows Mac Linux everything I'm going to do today is Cross-platform runs natively on all systems. It's going to be a bash script or a shell script in general And we're going to use primarily core tools Echo read W get all these things can be found in one binary Busy box, which will run on all systems. You can get binaries for it for Windows These type of tools are already going to be on your Mac and Windows or Mac and Linux systems and your Android systems Probably already have them if you have it a terminal emulator installed The only thing outside those core tools that we're going to use is FZF Which is a free open-source lightweight tool for filtering and searching through Lists in a fuzzy way meaning as long as you type something kind of like what you're looking for It should come up in the list and you can select it and again, that's how you can get binaries for Windows Mac Linux BSD will run on your Android device So you just would have to install it and it also you can get arm compiles for it Like I said so they can run your Android devices the way I'm going to do this It's like I talked about in a previous video where I talked about my rules We're going to try to limit what the user inputs force them to select from lists The lists are gonna be searchable hence the FZF and but we're gonna start off We're gonna I'm gonna show you how quick rapid development. We're going to create this application Really really quick to someone who is branded programming how they would do it But then we're gonna improve upon it to show you that you can write a program like this in a couple of minutes But then taking a few more minutes. You can make it a hundred times better Let's jump right in. Okay. Here. We are also. I want to say this isn't like a step-by-step tutorial I'm gonna be going pretty fast But basically give you the concepts of why I'm doing and why I'm doing them You can see we are in a directory with nothing. Let's go ahead and create a script I'm gonna use Vim as my text editor use whatever text editor you prefer as long as it's text editor and not a word document editor I'm gonna call it order Dot sh Okay, we're gonna start off with our shebang line saying that this is a bash script or whatever shell you would like to use Now to do this all we have to do is basically some read commands. So read Dash P for prompt and we're gonna say let's copy that a couple times. Whoops That's from the last tutorial Okay Enter your station And we'll say station here. We'll say enter your name and we'll say name and we'll say Item to order item and we'll say how many and We'll just call that one Qty for quantity and that's all you have to do to get the information from the user and we're gonna submit this to a server I've set up a script Let's see. Let's go here You can go here films like Chris comm forward slash scripts forward slash 2022 Medlist we have a few things here for this first part. We're looking at the submit PHP Basically, we're gonna submit it and it's gonna it's not gonna actually log anything. It's just gonna echo back Let's say we were 10 gloves. I would say 10 gloves ordered by Chris for station, whatever So that's what we're going to do. Let's actually go ahead and copy this URL and It'd be best to put that in a variable, so I'm gonna say URL equals Paste that URL and down here. We're just gonna say W get dash Q for quiet because we don't want the you know showing how much has been submitted uploaded But we're just gonna output the response from the server to the shell next. We're gonna say dollar sign URL And I'm gonna say Question mark and we have our variables. I'm gonna say station Equals dollar sign station, which we collect up here. Okay next variable and name equals dollar sign name and item equals dollar sign item and dollar sign Quantity or sorry, we got to say Qty equals quantity you might ask why I put curly braces around this I really should be more consistent and do it around each of them if I'm gonna do it basically I am I Don't like that. It was gonna be next to this dollar sign here. I don't think it matters. Let's see my color Oops color coding should tell me it's okay not to have the curly braces. I'll leave that so I don't confuse you I have videos on why you would use curly braces in certain situations So we save that we'll make it executable only have to do this once once it's copied to a system and then dot slash That and it's gonna say enter your station I'll say station 20 and I meant to put a space in 20 And again, this is why I going back to the rules of selecting from a list and not letting the user input stuff even there I already messed up and didn't put a space in there or may I just type 20 and not station 20 What do they want? I don't know. I'm just typing something in anyway Do I type my first name last name here last name then first name? I'll just say Bob Smith, which is not how I would want it, but that's how a user would probably input it What am I gonna order? gloves Excel or is it Excel gloves again if you don't give them a list They don't know what to input and it's gonna your database transsearch. There is gonna be horrific and in how many will just say 10 and It's submitted to the server it responded 10 gloves Excel have been ordered by Bob Smith for station 20 So to write a program that submits an order a supply order That's all you have to do thing is now if I want to order something else. I got to run the script again I have to choose my station again. I have to choose my name again and again Inputting this stuff manually is just not a good idea. So let's go ahead and make this better First off, let's use fcf and let's use lists So if we go back to my web browser here again films like Chris comm 4 slash scripts 4 slash 2022 and Medlist I have three lists here. I go here. They're not gonna look at lists But if I look at the the raw data, you can see they're written as lists Why did I make them PHP files and not text files? So they display properly in the web size because I'm assuming that you wouldn't have a static list You probably have a database that this is being generated from so if I was to grab the URL for here And let's say I was a W get dash q capital O and you could use curl if you prefer I do that I get my list That's great The list is somewhat alphabetized, but not really so I can just pipe that into sort like we've talked about and now I have an alphabetized list. How do you select from that? That's what fcf is for so let's go ahead We're going to grab our URLs here So what I'm going to do here is Now I could put the full URL for each one But I would say in general That's that could cause problems down the line So what I'm going to do here is I'm just going to say URL for this and Give it the directory list and then I'm going to create a variable called submit one called Stations One called names One called items. So I'm going to say equals dollar sign URL forward slash Submit dot PHP For stations I'm going to say that way if you move it to a different all these files to a different directory on your Website you just have to change the URL variable once and not every single line stations dot PHP dollar sign URL Names dot PHP and hopefully I'm Doing all these properly This should be items not item That PHP and I you know that I don't know if I mentioned earlier in the video I have not oh, it's called medical items. See I need to Make sure I'd enter these properly. So we have name stations and medical items I Haven't written this script yet. I've thought it out in my head, but I have not written it So I might hit some bumps along the way. I wanted to be a real with this I did the server side stuff, but haven't written this script yet, but I've thought it out in my head Okay, so down here for submit What we're going to do is change this to submit, okay, so we have our URLs Why did I put a dollar sign by the names there? Okay? But we're not going to use read at all we're going to use FCF. So let's go ahead and delete all these and First thing we're going to get is stations. So what we're going to do is W get That's Q. I'm sorry dash Q capital O and we're going to pass it Stations dollar sign stations And we're going to want to put that Into FCF. Let's go ahead and come out that comment out this bottom line and dash dash prompt and say Select a station Okay Let's go ahead and run this code see if we've typed everything properly so far. Okay, that's a problem I obviously messed up the URL for that Stations Stay Sean it's stations That's why it was not found. So it's good to test your code as you're going now stations I can type in 20. I get station 20 if I to gives me everything with number two in it I had entered in an output station 20 perfect. Let's go back into here I'm going to say our station equals and I'm going to put that in quotations again. I'm not going over the details of this I'm assuming you know basics of shell scripts for this video. It's not a tutorial but more of an overlook. Okay, so we've grabbed that variable Let's go ahead and just save ourselves some time copy that I'm going to say Name so we're going to get your name and this will change to names and here is I Guess I could have left the select select your name okay from names and Then items we're going to say items and we're actually going to change this later on but I'm going Basic stuff right now again dollar sign We're going to say W get Inside the parentheses Q o dash sign items pipe that into fcf with a prompt of Select an item to order and the last one we're going to get the quantity so quantity equals and then here I'm just going to say Echo zero dot dot 100 I doubt there's any items I'm going to order 100 of but I'll put the list that big and I'll pipe that into We want to have each thing on a new line So we're going to say space replace that with a new line I did that in the other video again. That's probably a better way to do that. We're going to say fcf dash dash prompt and we're going to say How many? We actually use the variable from the item above how many dollar sign item Would you like? Let's just say let's just say quantity or How many sure that works and then we can uncomment that and now if I typed everything properly we're in our script station I'll say station 20. I'll say John again our I our list was not Gloves medical large How many 55 there we go and 55 gloves medical large have been ordered by Foster John for station 20 Let's go back into our script here something. I didn't do with names with names We are going to sort that because the list was not alphabetized now. It is that's how easy it is to make things alpha numeric You just pipe it through the sort command If there were repeat items we could also do sort dash you for unique, but there isn't in this list. That's great so That's that's a hundred times better than the original script and did not take that much longer to write It takes a little more skill. You have to understand shell scripts a little bit better, but again 20 John Foster gloves and 55 and again I'm grabbing the list from the website so that if you have a new employee you just update the list the database online and Right away. They're available on all the computers running this script same with items you add a new item It's automatically there you add in a new you open a new station You add that station in but we can make it better because now or through now if I run again I have to pick my station again. I have to pick my name again I shouldn't have to do that if I multiple items to order I should be able to start up the script Tell it what station my name and then go item station item state or item amount item amount item amount So let's go ahead just exit out of this So what we're gonna do here is all we have to do is just say while one Do should I like to put that on this line here? I think and we're gonna say done let's indent things properly and and The only issue with this is you may not see the output So we have a choice do we pause for a moment so that they know that items that has been ordered We'll get to that in a moment again all these different things you can do Okay, so I'm going to order for station 55 will say this time and we'll say That my name is Mary Swartz Schwartz and Now I'm going to order baby aspirin and I'll order 10 of those and Now it goes to the next item. It's the mid the order That's what I'm saying is right now we didn't see that it was ordered So we probably want to give some output to that the simplest way would just to be Output that that has been ordered for a second and then go back in it might slow down your ordering a little bit If you're one of those people who types really fast, but bio hazard bag So bio bag I'll order one of those next item will say Yeah, just one of these Okay, so we've already improved our script a lot But now also I can't get out of my script without hitting control C a lot which the average user would not know So what we're going to do here is we're going to make some changes So after we submit an order what I'm going to say here is sleep for one second and I might even clear the screen. I do like to clear the screen a lot, which is Well Okay, so let's go back. We'll order again. I'm sorry again So now I'm going to put in my order and also one of these sort of things is if I'm logged in as a user It should know who I am so I shouldn't have to put in my username But we're just saying that we don't for this example. Let's always say station 22 today. I'll say that my name is Roger Alan Rogers, okay, and I can also again if I don't know what I'm looking for I can look through the list right Let's say I want a cuff of a BP cuff. Oh, there we go BP cuff adult BP cuff. I just need one of those That did not work like I thought it was going to let's order something else. Let's just order two of those Hmm. Okay. Let's exhale. I did something in the wrong order here. Oh I cleared the screen at the wrong point That's why let's run it again 22 let's take them something grab random name Yeah, one of these and there we go. I see it has been ordered Okay, or I could just you just have it say order successful or whatever if the order was successful We'll get some adult pads That should say electro pads again I grabbed this medical list just off the inventory from where I work So I didn't create this list I heard 66 of them and you can see there for a second It tells you you can do that. There's different ways Or you can have it prompt you different ways just so people know that they have ordered it Now again to exit out I have to hit control see a couple times. So what we can do here is We can add a few checks Also, I could also have it print out the full list of everything that was ordered at the end. So Let's go ahead and start off with the exiting out properly So I can add to any one of these lists Exit I'm not gonna worry about station and name. What I'm gonna do is down here with item So we're piping this output Into fcf what I can do is I should be able to say Echo I think I could just say Exit or I could say quit. I you like saying quit better quit if I put parentheses around this I think We're also going to improve getting that list which actually let's let's improve that first. Well, we'll test this out again I'm doing this all off the top of my head. So station. We'll say 44 and I'll say Mark Brown there my first option is quit so I can choose that and what I can do here is add in after this I can say Do an if statement I can say dollar sign item Equals quit Which I don't have to worry about case sensitive of it because it's going based on what you selected from a list Well, if that's true then exit Okay Or I could break out of the loop might be better in case I want to do something after this loop So I'll say break should work order so again, I'll 30 station 34. I'll say Mark Norris and I'll order something. I'll order five of these I see it's ordered now. I'm done. I just hit quit and Exits out for me. I have quit. So actually if we come in here, let's get rid of this clear I think that might be better. So now if I do that, I'll say order Again, I'll pick a station 12 I'll pick another mark and I'll order a few items again Baby aspirin, I'll order one of those assuming its bottles now. I'll order a Zip lock bag Three of those. Oh, it's not putting it on new lines. We should so now I quit I was hoping we'd give a full list out there. So let's actually go ahead and hear Them orders after the W get we're just gonna say echo easy way to do that adding a new line So again, we'll go 78 station 78. We'll say brown one of the brown people. What did that sound bad? Thompson brown And I'll just pick a name from the list. I'll say 43 of these there. That's been ordered Again, I don't have to put my name or station I'm just gonna pick something safety glasses for them and you can see it's generating lists So now when I hit quit I can confirm everything that I ordered everything ordered great one issue one last issue With this code is that every time we loop we're saying W get and we're getting the items So we're doing an HTTP request every single time. We don't need that. We don't want that So what I'm gonna do up here is I'm going to say I'm gonna replace items with its output of and We will say Get items inside this So here when the script first runs, it's going to grab the items and then here all I have to do I should be able to just say Echo dollar sign actually no one echo command now Dash and for that so we recognize the new lines. I believe I'm doing that right Dollar sign items Let's go ahead and run our script pick a station Pick a name. Nope. I did something wrong Did you do do do? No, not that ever do echo. I I'm not sure if that is my issue, but we're gonna give that a try again Psycho station select a name Looking for match. I'm missing line 11. Oh Forgot the quotation mark here It's like a station selecting name, okay now I can pick something from the list order it There we go. It's like something else order it Select something else from the list order it and then I'm done. I'm gonna take quit There we go. I mean how long to have I've been recording 20 minutes So let's say you work somewhere in IT There's so many IT guys that don't know how to program and if you don't know how to program You don't really know how computers work This is a very very look at this. We've got 23 lines of code if you count empty spaces blank echo commands. I mean really This is probably 15 lines of actual code and it's not that complicated If you're not a programmer, you might be confused by this But these are basic shell script this will run on any system. It's lightweight So even if you had old hardware, it should run pretty smoothly But this is How it should be done in my opinion I'm not saying it can't be improved upon and yes We did though that first order with those four lines read this read this read this read this submit and done But this is so much easier for the end user because now they select things from lists. It's all searchable They don't have to wonder. What do I enter here? but also It's easier for you because they're not entering things. You're not expecting If they need to order something off the list, they should contact you through email So yeah, I need to order this and then you can add it to the list We're pulling stuff from the server you add to the list and it's there right away next time they run this command They might have to exit out of the program if they already have it running But that's it. I'll post this code up on past been and linked to it in the description of this video And again, if you want to access the lists, they're all here. You'll see that in the script and again The submit just echoes back That with the variables you pass it. That's it. So yeah In real life if I needed to do this for work It was 15 minutes worth of work and it makes life a lot easier again Nowadays a lot of people don't like these text interfaces. It doesn't get any simpler than this though It's it's fast to write it runs on all hardware almost all operating systems at least all modern operating systems natively and It's super simple to use again again, you just have them click on an icon that runs a script I wouldn't expect the average person to open up the terminal and run obviously either they would just type order or item You know medical orders and that but you give them an icon either on their phone or on their desktop computer And it opens up this and they just start typing. It's very clear I mean you might have to walk them through at the first time, but it's fairly clear once you do it once what? They're you're you're expecting from them Bandage bandage ace bandage three inches For these and again the item this item list I just grabbed from my works item list So things could be definitely written better. You can also have in it like off to the side like the minimum order You can just have it listed here It doesn't matter because you know exactly what they're gonna put through so you can parse through that on the server side But that's it and then I'm done I hit quit and they can confirm Okay, all that was ordered and of course on the server side. It'd be dumped to preferably a database But it could just be a log file, which is super simple and you can also have it Text the the person who fulfills the orders When when you exit out of that loop you can have another order or you can just have them have it to where anytime You know, there's new stuff on the list. They get an email Super simple super simple and of course there be a time stamp and stuff on it That's all done on the server side not the client side so they know when it was ordered and now they know who it was ordered from and again things like the station and The the user especially the user a lot times you're gonna be logged into a system They should know who you are although a lot of places have this one everyone logs in or the same user name So that's just weird Yeah Learn to do a little bit of program goes a lot away again This is a text interface which for forms is far superior than most GUI interfaces for stuff On the phone on your phone it would run just like this using the keyboard as fun So like a GUI interface might be a little more convenient for a phone, but this would run fine on a phone but Obviously people feel more comfortable a GUI interface. So in the next video We'll go over doing this sort of thing with With a web interface which of course can be packaged as an application that but why would you do that? So people just log into the website and order stuff Making the list searchable and only having to put in the information once not multiple times And then also I might also go over doing something like this in Godot Which is a game engine, but I love using it for creating Applications as well. So thanks for watching check out films by chris.com. That's Chris the K There's a link in the description to my website check out there. You can check out my scripts my codes You can support me through LibrePay paypal or patreon if you can't support me financially think about liking share of scribe and commenting I hope that you have a great day