 Okay, so I think it's about time for some videos on shell scripting and people have asked me to do this for a while and There are a lot of videos on YouTube of people going over specific commands and stuff like that But that's not my type of thing. I I'm a practical person. I like to see things in action and stuff like that So I'm not gonna do contrived examples, but I'm gonna show you in this video I'm gonna show you a particular script that I've written that gets me a whole lot for very little and Explain how I guess the different mindset that you need for writing shell scripts because Writing a good code in I guess a shell script or a bash script or something like that It's not always the same as writing something good in Python or something like that It requires a slightly different mindset because oh well as the decoration up here suggests The shell scripting is more loyal, I guess to the Unix philosophy You are modifying text strings directly or your text dreams directly So one program outputs some text you modify that in another program You modify that in another program and if you don't know what I mean, hopefully it'll make sense by the end of this video So let me show you this I'm gonna showcase a particular script that I've written And so what this is for is for mounting usb drives, so I'm gonna insert a USB drive into my computer Actually, I think I'll insert to just for example So I now have two USB drives in here So how the script works it is for mounting and then I have another script for un-mounting these drives So I have it mapped to super f9 So when I press that what it does is it looks for all the mountable drives and lists them out And I can type in which one I want And select the mount that it then asks me where do I want to mount this? So let's say I'm mounted at MNT slash USB 2 okay, and then if that folder doesn't exist it'll ask me to create it I'll say yeah go ahead and create it and you can check with LSBLK and see that this has been successfully mounted I also have an unmount script, which is similarly constructed I might not talk about this if I don't have time or I might talk about it in another video But it basically is constructed the same way So let's go into the mounting script. So I'm gonna open up The script here now. It's called d-menu mount because this little Menu up here is just d-menu. That is the one dependency required. I'll talk Specifically about how d-menu works in a second But the idea is I actually have a video on it You can check it out if you want but the idea is you can just send d-menu text and it's going to give you Options up here that you can select from and then it returns the thing you selected. That's all that does But so let's go into this now if you look at the actual script. It is not a complicated d-men It's nothing fancy or anything like that. It's really just a very short bash script It's less than 25 lines to get rid of the comments. It's less than 20 very short It's nothing complicated and it does exactly what I need it to do So I'm gonna just go through this and explain to you how I Actually do this without needing any other dependencies from d-menu and getting all this stuff done So let's go ahead and start up here. I'm gonna skip this line I might talk about it later But the first important line is this thing here now the first thing I want to do is I want the script to Detect what drives I can mount So at any given time if I run notice I here I run lsblk And if I run that that of course lists out all your drives and partitions and this LP option if I give lsblk LP This happens to give you the full Location of the drive so dev sda is where this drive is located so to speak And it gives you this more modifiable Text stream or something like that now what I want this to do is this has produced a little some output text and I want to Only select those drives that we could potentially mount that is I don't want these lines that say disk I don't want those I can't mount disks I can only mark mount partitions and I also don't want lines that have you know That are already mounted like our boot drive home drive root or this drive here. That's already mounted So what I can do for this is use grep as this thing here says so if I grep out So if I run this command and I pipe the output into grep, let's say I'm piping it to grep and I'm gonna search for the pattern part now I you may know what grep does already, but it's pretty simple to understand what grep does is you give it a text stream and It will search for a particular pattern Let's say the word part and it will only return those lines that have that pattern in it So here we have disk and you know this line here But if we put that through grep, it's only gonna return that line That's the lines that have part in it that pattern part. So the next thing I can do Let's say I mean I really want to get rid of these things too because they're already mounted Really all I want is these three drives to appear So what I can do is I can say Part space and then dollar sign and what this means is You may know regular expressions, but dollar sign just means end of the line So it's going to search for part and then there's an empty space here. You can't really see it But it's there and then there's an end of the line So this line matches this line matches this line matches But these won't because there's a space and then there's something else besides an end of the line So if I run this command, you'll see that it actually only returns the drives that we want to be able to choose from to mount Okay, so that's good. That's what we want So this is what that does and the last thing I do is I use an awk command because when I run when I run the script here I don't need a excuse me. I don't need all of this information I only want you know that where the drive actually is and it you know, it's capacity It's like capacity. It's Size or whatever Just so I know which one's which so what I do here is I just awk it so I'm gonna And then awk oops miss the key there What you do with awk is what this effectively says is print the first column that is going to be this Then print an open parenthesis then print the fourth column, which is one two three four Then print a closed parenthesis and then close that command. So I'm going to say one comma adds a space Then the open a parenthesis for close Parenthesis and then close the print command and if I do that you'll notice that this is the output We have and that's the exact same that I have. Oh, it's exact same. I have up here now What I do so what I am doing in this line here is I am finding all of the available drives And I'm assigning them to the mountable Variable so I now have this variable mountable that has all the drives we need in the format We want them to appear in this little menu up here So the next thing I do this line isn't particularly important But well, I guess it is what this does is it checks to see what this says is basically if the mountable if the mount the variable mountable is equal to nothing just exit the script and that's in the case if I If I run this command and there are no partitions I could mount just exit the script You can't do anything and exit with an error You know, I probably it might make sense not to have an error there But this is just in the situation where you know, you don't have scripts One means error if you have zero or nothing that means it'll exit without an error So anyway, so you find the mountable drives if you have no mountable drives this script quits That's all this says now in line 12 here You know move this up a little bit So in line 12 here what I do is I take those mountable drives and I'll pipe them I'll echo them pipe them into D menu. So as I said before D menu is a very nice program I'll just show you how it works. So if I take let's say I take You know, I'll echo. I'll just actually show you what it looks like, right? So we can echo mountable usual Oops, I didn't actually set this equal to a variable. So I'm just going to Replicate the line and the shell script here. So if we echo the variable mountable it's going to output all of these things here and we can pipe that into D menu and What that's going to do is it's just going to make each of these lines appear as an option in D menu that we can type We can type in and select and when we select one and press enter D menu is going to return that text. That's all this is So that's all D menu is doing as I said, this is not something that's installed default on most systems So but it's pretty much in every Linux repository. You can just install it. It's an extremely useful program I use it for so many things So anyway, so now what we're doing here is we're just Echoing these choices to D menu so the user can choose one And what these options here? This is for case and sensitivity. So normally if I you know type something capital It's not going to select something, but if I put an I it will You know, it's still going to you know, it's case and sensitivity as you would expect and the P option is You know, this is the prompt If you put P before that so that'll you know put whatever text you have here So here I have MountWitchDrive or whatever and the last thing here is I actually have awk because you'll see by default we're going to Return this you know this entire line here But I only really want this thing here because that's the thing I'm going to be actually running the mount command now on later on So what I do here is just awk and I print Only the first element, okay, so it gives me this I can choose one and bam there it is right all right, so That so in this line. So just the you know recap we have listed all the mountable drives We've given them to the user for him to choose one of them And once he does that we assign his choice to the chosen the chosen variable. So I'm actually just going to I'll actually do this repeat this in You know my shell here So let's say chosen pick that and if I echo chosen You'll see that right. So now this is equal to what we just selected. Okay, so Let me scoot down a little further. So here we have the same thing notice As I said before we have this check to see if mountable there are actually mountable drives if there aren't any exit Here I say check to see if the user actually selected a drive if there's something that's actually equal to chosen Or if it's just in empty and if it is empty Just exit the script because he didn't choose anything. Maybe he chose it change this mind. Okay So that's what's next and if we scroll down a little further Now we're going to try to mount the drive. We know so we've now gotten basically what driver we want to mount Now we just have to mount it now the first thing I do here is I actually just run pseudo mount on the chosen drive and Why I do that is because many so you can store drive Mounting information in Etsy FS tab and a lot of people will store their drive information there And so this will try to just mount it without a particular mount point But if this drive is in fc fs tab With directory mounting settings it will automatically mount it. Otherwise, it's just going to say, okay I can't mount this in me. I don't know where to mount it. But so Long story short what this line does is if you've already stored information in a fast fs tab where you want it to be Mounted it just mounts it there and exits the script successfully you win Otherwise it does the thing where you have to Select what drive or what a directory you want to actually mount it in so how do we do that? That's actually relatively easy In the same way, right? So if we select one of these drives, it actually produces. Sorry my mouse I don't have a mouse pad right now. It's sort of annoying It gives you all these list this list of all the folders that you can choose from now This is actually automatically configured or we we can produce this automatically with this simple line here So this variable here dears. I set that equal to The output of a find command. So I'll go ahead and show you what this looks like. So by default Find let's say I run find on user or something like that. It's gonna find everything that's in that directory So I'm gonna find in this command I'm gonna find everything in mount everything in media everything in home and everything in or mount Sorry MNT media mount home. That was very dyslexic reading there It's gonna find all of the files in here But I'm gonna specify. I only want directories. Okay, so type D means only find directories So if I go up here to find user and say type D Now it's only printing out directories that exist not just files and the other option I hear you have here is max depth So this is because I don't really wanted to search for every single folder on the computer because that's gonna take a long time So instead I only wanted to search in three folders deep. You'll notice that that so here's our first option finding everything That takes a little longer But if we only search three deep that's a lot quicker and especially if you're running something on home You're definitely gonna want this. So what this does is I'll just actually run this command. So Find everything in mount in media or MNT and then the actual mount drive and then home only those that are directories and Only as deep as four or excuse me three folders in so if I run that it's very quick You have all of these different folders outputted now notice also I put this thing here and this is just if you have any errors actually let me rerun that notice that there I get It gets some errors here because there's some things I can't access or there There might be folders that don't exist. You might not have a slash media Directory and in that case I want I don't want to see those errors So you can sin send errors to dev not and so now those don't appear So I am going to now set this output equal to or I'm gonna set dears equal to this output So I can echo dears and that's gonna output all of that stuff or so it's prettier You can put it in. Yeah quotation marks. Okay, so now we've been doing this manually So it takes forever, but man, it's it's a I don't know it's in terms of actual shell Well in terms of like actually your computer performing operations. It's very light and of course this we're basically done at this point So the last thing we want to do is we want to echo dears into D menu And we get this option all of these different options here and this is what we do here We're just echoing these options into D menu and the thing that someone chooses is going to be set to the variable mount point Or the mount point variables is going to be set to it. You know what I mean So if I say I want to put this in you know MNT or something like that It's gonna return MNT and that is what is going to be equal to the actual mount point Okay, now at this point we have everything we want it took to D menus, but we have everything we want We have what drive we want to mount and we have what directory we want to mount it in now the little very last thing I do this little if statement here This just says check to see if that that directory we actually put in if that directory actually exists Now if it does not exist what it's going to do is say give you another prompt in D menu where it gives you The options of no or yes, and it's going to say That mount point doesn't exist Do you want to create it and if you say yes, it will create it and you'll have that mount point and you will mount it Now the final line of the script is the actual important one So you're just going to pseudo mount the chosen drive at the chosen mount point And then this line here is just for notifications like when I'm actually mount something So let's say I you know mount this drive at you know You know USB to or something like that. It's actually going to show that little notification that disappeared instantaneously, but it was there So that that's actually it So this is what again what I use for mounting drives And I guess the takeaway you have to get from this is you know You don't have to know anything really special about the system all of it is all of it is really just text Manipulation the input for this program Aside from D menu, which is the thing that the user uses really just came from the LS BLK command That's where we got our drive information and the find command That's where we got our directories and then we just modified that text to give us the input to D menu We chose the required the things that we want from D menu and then we just actually You know, we just ran really one command that does what we wanted To do now I should say if you're going to copy my script and use it on your computer just note that You I do have pseudo the way I constructed the script it isn't universal Notice that I do use pseudo here a couple times and this is basically going to require that you use You have your user has permission to run this kind of script without a password because there's no password prompt for this So just bear that in mind So if this doesn't work on your computer just look up how to run something run a particular command without a password and that'll work But really the objective here isn't the script. It's the actual I guess the mindset behind it now. I should say as I said before I do have an unmount script as well So actually I'll show you how that works. It works the same way. It just says unmount which drive And I'll I'll say, you know, let's say we unmount this USB drive and it basically does the same thing I'll brief you show it briefly show you what that looks like But we'll go through it super fast. It's also very Simple what I do here is I run an LS BLK command and I grep out this particular pattern that only appears Notice that all of the mounted drives are going to have a T a space and a slash in them Because this is mounted. This is mounted and this is mounted, right? So we're just grepping out only those Drives sorry my nose is so itchy today. I think it's actually when I talk a lot my nose gets itchy It's only when I'm like doing live streams or like long videos that this happens. I don't know why So we can grep out all of our selected drives. I also have an excuse an exclusion reg X here Now what I mean by that is I don't want to give the user the ability to unmount boot or home or root so Basically, this line says exclude those drives. That's what that's for And then I'll Ock it in the same way that I awked that last, you know the thing in the last excuse me You know, excuse me. I lost my train of thought I just awk it in such a way that it shows in the format I want so like this drive On this location with you know, this much capacity and it works in the same way It just asks do you want to unmount it you select it and then that's that so you can check out both of these scripts Both of them are on my github and so hopefully this is either giving you ideas for kind of things that you can do You can of course steal my scripts But I again the objective is getting in the mindset of doing shell scripts for Personal use and other things so hopefully this has given you some ideas if you have any questions suggestions Feel free to throw them in the comments and I'll see you guys next time