How to Write Simple Shell Scripts!

Shawn Powers2,579 words

Full Transcript

do you want to hear a secret linux system administrators have been doing devops for decades we just didn't call it that of course the thing i'm talking about is shell scripting and it's basically just doing commands that you would normally type on the command line and putting them inside like a program that will execute them all for you automatically it's a way to automate things that you have to do over and over and over and while it's not exactly the devops world that we're talking about now conceptually it's the same sort of thing now the first thing we have to understand is what exactly i mean when i say shell because that's a question i got actually in one of the previous videos where i mentioned the shell that we're using and so when i talk about a shell it's the program that is running on top of the linux kernel so it's part of the operating system and it basically allows us to have an interaction with the underlying linux system so when you're typing in a terminal window you're using a program that is the shell okay so for us it's going to be the born-again shell or bash you've probably heard somebody talk about bash scripting and that this is my grandpappy who is going anyway the shell is this wrapped around thing around the kernel and it allows us to interact with it now you can write programs that the shell will interpret for you and do a bunch of commands and that is what we're going to do we're going to learn the basics of shell scripting so you can automate processes that you would normally have to type by hand every time that's really what a shell script is now before we get started actually writing a shell script i have to show you two different editors the first editor is vi and vi is what i normally use if you've seen me doing things on the command line it's probably been with vi now the problem with vi is it's really complicated to use like you have to press i to go into insert mode and then you can type and then you have to press escape to get back into command mode and then the command mode allows you to save but to do that you have to do colon w and then that writes it to the file but then if you want to get out you have to do colon q and that quits the program vi is very powerful and it's in every linux system everywhere basically the problem is it's so difficult to use and get the hang of that i do not recommend you use vi unless it's something that you are already using and you're familiar with or if you just want to like be super nerdy and you want to learn vi i'll probably make videos about using vi but in the short term what i recommend is that you use a program called nano nano is on almost every opera or linux operating system that you're going to use and it's very user friendly so if we were to use nano here we would just say instead of vi we would say nano and then the name of the program we want to edit and here it is and the nice thing about this is arrow keys work just fine so we go to the end can just backspace and get rid of all this stuff and you'll notice down here on the bottom there's a bunch of commands and it what that means is if we want to exit we can do control and then the x key and it'll say do you want to save we can say yes what file name do you want to save it as well the file name that it was so press enter and then it does it so it's really really easy to use nano and i recommend that you use nano if you don't have something that you already have fallen in love with okay that said i'm gonna use vi only because i've been using it for decades and that's just the way my fingers think okay this isn't about text editors so if i'm using a vi you can use nano and it's gonna be just the same as far as the the actual text files themselves just know that i'm using vi only because i've been using it for so long all right anyway back to the command line so let's edit this file again and i called it test.sh you don't have to end it with a dot sh but generally if you want to be able to identify a shell script a lot of times we'll end it with sh but again we don't have to but let's look at test.sh now first of all if you start with the hashtag or pound symbol whatever you want to call that thing that is how you put a comment in a shell script so this is not interpreted by the the shell itself however this is all a lie when it comes to the very first line of a text of a shell script if we go pound exclamation and then we give a path to what shell interpreter we want to use so in our case bin bash what this is going to do as the first line in our shell script is tell linux if we execute this program this test.sh we want it to use bin bash to execute all the things that we're going to put in we can tell it to do something different than what our default shell is but if we don't specify up here it will execute the commands we're going to type using whatever our default shell is and that might not be bin bash that could be something else like bin sh or bvsh or something if we want to make sure that it runs how we expect it to run we want to put this first line in shebang which is the pound sign and then exclamation and then the interpreter that we want to use so always start your bash scripts with this that's the short version of it now what we can do is any command that we would put on the command line we can type right into here okay so we could say echo hello and then we can save this file and we really quickly want to make it executable so we don't know how to do this yet but chmod plus x test.sh and this is going to then make it an executable file and then we can do dot forward slash test.sh and it will execute it it says hello okay so we've automated the process of that echo command i know that seems silly let's add to it okay so we know that echo works what if we wanted to use a variable if you remember we used variables before so we could say message equals hello there okay now it does not have to be all caps this is just a lot of people will make their variables in all caps just so that we know it's a variable so message equals hello there and then we can say echo dollar sign message all right we'll save this and now if we execute it hello there it just it used our variable as a way to store a value and then we did something with the variable later on in the script but when you're in a shell script there are super cool variables that you can use too that don't really work unless you're in a shell script and what that what i mean by that is you can actually give a shell script an argument and then that argument will become a variable inside the shell script it's super super cool and i'll show you what i mean if we edit the file again but we add dollar sign one dollar sign one is a way that we can send information into the shell script so this means the first argument that we send to a to the shell script is going to be assigned to this variable so it's going to say echo whatever that message is and that's already assigned up here and then whatever argument we give it on the command line what do i mean by that let me show you we'll save it we'll execute test.sh and the argument i'm going to give it is sean so now if we press enter it's going to say hello there sean if we do test sh bobby hello there bobby what it's done is that first argument becomes dollar sign one and we can do it with multiple too so let's edit it really quick and now let's add dollar sign to now you can see where i'm going with this i'm sure if we do test.sh sean bobby hello there sean bobby okay what if we do this i'm going to get fancy now you ready sean powers bobby would do bobby i think this was his spelling bobby fischer now what i've done is i've given it two arguments and not four arguments because since i put them in quotes this is considered the first argument and this is considered the second argument so what we should see is hello there sean powers bobby fischer because again this whole thing is dollar sign one and this whole thing is dollar sign two so it's a really neat way to pass information into a shell script and you can see how useful this would be if you're trying to manipulate data you can pass the data to the shell script and then it can use that data internally but wait there's more i clear the screen let's edit the file one more time okay i'm going to get rid of all this we'll get rid of all the stuff that we did and okay and now what we're going to do is create a for loop now what a for loop is if you're not a programmer it will iterate through a list of information that we give it and assign the variable every iteration what do i mean okay watch we'll say 4 let's say our variable is x just the capital x in i'm going to say one two three four five so this is our set of data is one two three four five then i'm gonna say do and now i'm going to have it do something with that and i'm gonna say echo dollar sign capital x okay and then i'm going to say done so what we've done here is what i've done here i'm so funny oh this is our set of data one two three four five and this can be more complicated but this is a very simple explanation of a for loop right so this is our set of data the number one two three four five and what it's going to do is it's going to assign this value to x and then do everything between do and done and then it's going to go again and it's going to assign x the number two and it's going to do everything in here so what we should see is on the command line when we executed it should say one two three four five because it's going to go through five different times assigning the variable x to every value that we've iterated here okay so hopefully that makes sense what we're gonna do let's see if that works as we expect one two three four five and i'll be honest i usually forget the exact syntax for how to do a for loop in a bash script so if you have to google for loop bash example every time you set it up that's fine i actually had to do that when i was preparing for this video because i forgot like okay is it like dude does it go on the same line does it is it do and done or how exactly does that work so if you have to google it that's fine but for loops are very very powerful and you use them a lot when you're doing shell scripting okay now there's one more really odd thing that was included you know we're we're actually doing these um linux essentials objectives and for some reason they put into the objectives for this video exit status and what an exit status is if you have a command and it has an error it will give an exit status of anything other than zero if the command executes without any error the exit status is zero so it's that easy it's a way later on when you're doing more advanced scripting you can use the results in a conditional like if it exited fine do this if there was an error do that okay but right now they just want you to understand what an exit code is so i'll show you because it assigns it to a variable on the command line we can use this in a script but right now i just want to show you what an exit status is or exit code or exit status because it's just apparently really important for you to know so if we do something like type ls it's going to show us the stuff in there right and then if we echo dollar sign question mark it's going to it should give us a zero let's see yes zero because what happens when you execute a command of any sort on the command line here when it completes if it did so without error it will assign this variable dollar sign question mark or just the variable question mark denoted by a dollar sign so this is the exit status of the previous command in our case since ls was executed without error it is zero but if there is an error like if we were to say ls blah blah blah it's going to say there is no such file or directory and now if we do an echo of this exit status it's going to give us something non-zero now it's not always going to be 127 but it's going to be something non-zero and i'll be honest i kind of want to keep going with bash scripting and show you all the really cool things you can do like conditionals and while loops and and all sorts of like until loops all those things um but for the the purpose of linux essentials just a very basic understanding of how a shell script works is important if you want me to do further videos outside of our our series here let me know because i will happily do more shell scripting but until we get to that point i want you to remember to learn everything do what you love and most importantly be kind i'll see you with the next video and um yeah let me know if you want other other scripting stuff i'll see you later

Need a transcript for another video?

Get free YouTube transcripts with timestamps, translation, and download options.

Transcript content is sourced from YouTube's auto-generated captions or AI transcription. All video content belongs to the original creators. Terms of Service · DMCA Contact

How to Write Simple Shell Scripts! - YouTube Transcript |...