Shell Scripting Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
well hello internet and welcome to my shelf scripting tutorial in this one tutorial I'm going to teach you the basic syntax of how to write bash shell scripts and I'm also going to focus in specifically on input output variables math creating your own functions how to execute Python functions conditional strings arrays looping and we're also going to cover a lot about how to use vim because that is going to be the editor R that I'm going to be using here and basically a shell script is just going to contain commands that are executed inside your terminal just as if you typed them in the terminal but like I said previously we're also going to cover all the other different things like input/output conditionals strings arrays and all that other stuff so I have a lot to do so let's get into it okay so basically I have two terminals open here one on the left and one on the right and I'm gonna start off here by talking about of them now to install them if you do not have it installed you're just gonna type in sudo and apt and get and install them like that and hit enter and it might ask you for a password and then it will install it and you may have to restart the terminal after that but otherwise everything is ready to go so now that we have them installed let's talk about how to use them and I think this is very important let's just go and create a little file called test and there we are we are inside of them and you can just start typing away by hitting AI that's gonna put us in insert mode and then you're going to be able to put all sorts of random things on multiple different lines and I'm doing this for a reason because I want to jump around here and show you a couple different things and how things work inside of them all right so there we go we created our a little file here now how do we move around inside of the file well let's go and hit escape and has just jump us out of insert mode and if you would like to save but not exit you're going to hit colon and W and that's gonna save your file you can also save and quit once again by hitting colon and your cue and that's gonna save and quit you're also going to have the option to quit and discard any changes that you have again when I hit escape and you want to hit : and then you're gonna hit Q and an exclamation mark jump back in here again you could also move around in nice ways again if you're not in insert mode of course if you want to move to the front of the next word just hit W lowercase W then jump you through your file you're also going to be able to move backwards to the front of words by hitting B lowercase B once again you can move to the start of the line by hitting zero and you're going to be able to jump to the end of the line by hitting the dollar sign you can hit an uppercase G that's gonna jump you to the last line and then you can hit an uppercase a and that puts you in insert mode or a pen mode that's gonna move you to the end of the final line and allow you to start typing okay so and also you're gonna be able to move around with your arrows just like you wouldn't in any other different text editor we could also decide that we want to display inline numbers by hitting escape once again and colon and set number like that you could turn in syntax highlighting on once again by hitting escape and colon and then typing in syntax on and hitting Enter you're also going to be able to decide how many spaces you're going to have with a tab by hitting colon and set tab stop is equal to two you don't need to memorize all these things because more than likely you're going to go and put them in a file and we'll show you how to do that in a second another option that is available to you is Auto indent which is going to indent all of your new lines so set Auto indent you may want to use these you may not just don't be completely up to you and where you're gonna save all of these changes so they are available every single time you open up them is inside of your home directory so mine of course has my name on it Derik baƱos and you're just going to save them in a file that is called a dot vim RC and we can open that file off and if you do you can see right here that I went and set it so that line numbers always show up that syntax is always on that my tab stops are going to be equal to two spaces and I so have Auto indent and you're gonna go and save that once again into dot vim RC inside your home directory and then go and reload all your terminals and all that stuff will be there every single time you load up them you could also come into them and verify which them RC file you're gonna be using by just coming in and typing in echo and then dollar sign and my them RC and you can see that it is using the one that is inside of my home directory okay so there's just the basics of how to use vim as this tutorial continues we're gonna be doing a lot more with it you could also come in here and select a whole bunch of these files just by going Capital V like this and then pressing down on your arrow keys to select everything and then hit D to delete everything now that we got that out of the way let's go and write ourselves a hello world script and see exactly how that works now you're gonna start off your file with the shebang well first off you're gonna hit I to go into insert mode and you're gonna go shebang which is just a hash symbol with an exclamation mark and this is gonna tell the system the interpreter that you want to use for your script in this situation I am going to use bash and then if I wanted to go and make a comments I can do a comment just with a hash symbol and comment and then if we want to echo hello world out on the screen we can do so just like this so echo hello world and then you're going to escape and you're gonna hit Collin and WQ and you are out of them over here on the right side of the screen I can now go in here and execute this let's just do LS to see exactly what we have here and we can see here is a little world which is what we're going to be doing our little work with there and there it is indeed and let's just clear that now if we want to come in here and execute our hello world script we would just go period and forward slash and then hello world like that and you can see that hello world shows up there on our screen so we're just gonna be bouncing back and forth over here I'm going to write the script over here I'm going to execute it now if you did not have that executable the hello world script it's very easy to change it into an executable all we're gonna do is go CH emoji like this and seven five five and hello world exactly like that and now it is going to be executable now these numbers right here that you see after we have CH mo D is gonna define who can do what with our file the order of these numbers are going to represent first off the owner of the file and then the group and then everyone else and the permissions that they have now whenever we put a seven here this is going to say that the owner of this file is going to be able to read write and execute it when we have five here that is saying that the group in this situation as well as everyone else is going to be able to read and execute if any of these were six that means they would have permission to read and write four would mean read only three would mean write and execute two would mean write only one would be execute only and zero would mean that they cannot do absolutely anything with a file and that is how we can change permissions on our file if you did not know that already now we're going to be able to come in here and we're going to be also be able to create a variables of course and we're going to define variables like let's say I wanted to have my name like that I then would not put a space and no space there and I would type in direct like that so that creates a variable called my name with the value of Derrick inside of it and variable names are going to start with either a letter or an underscore underscore meaning that guy right there and then they're going to also be able to contain numbers anywhere else inside of the variable name now the shell is gonna treat all of our variables as if they are strings but we're going to be able to work with numbers as you'll see in a moment and it's just important I want to make sure you know whenever you're declaring variables you can have white space on either side of your equal sign now we can also come inside of here and we can declare a constant if we'd like to even though that isn't very common and how we do that is declare - R and then we could say something like num1 is going to be equal to five that is going to make that a constant and let's come in here no one called num2 and give that the value of four and then you can do some basic arithmetic if you'd like to go and create number three and you want to do arithmetic you're going to have to go and put a dollar sign inside of here and and two of those brackets and then we can add num1 plus num2 we could also come in here and highlight this if we want to save a little bit of time escape out of that capital v we're gonna hit lowercase Y to copy that for us and then we can do capital P and then we can do capital P and another capital P and another capital P and come in here and get back into insert mode change this to for change this to five change this to six and then we can also do is subtraction and we can do multiplication and we can do division we're also going to be able to use exponents as well as modulus I'll show you those in a second though if we want to then echo out all of this information on our screen we can just go echo and we could do something like five plus four is equal to and if you want to put the variable directly inside of your output just go dollar sign and number three in this situation can highlight this again copy it by hitting Y and then I'm gonna go and paste all these out again and if I'm ever moving too fast feel free to hit the pause button I like to move these tutorials along quickly and I just leave it up to you to hit the pause button to make sure that they move at the rate that you would prefer them we can escape out of that and then go and W and execute and you can see exactly how all those work for us we're also going to be able to go echo and we don't we can use exactly the same parenthesis we have here and let's just do exponents again so let's go to like this and we could do echo and modulus is going to return the remainder of a division and you can put spaces inside of here if you'd like and you can see that those all calculate out you're also going to have assignment operators that provide for shorthand arithmetic options for you and those are going to be plus equals two and basically what this is going to mean is let's say we have I plus equals two - that is going to be equivalent to I is equal to I plus two so it's just the shorthand way so you don't have to type in I multiple different times you're also going to have a subtraction option and a multiplication option as well as a division option I think modulus also works I'm not certain though because I never use it so let's go and create another random variable and it give that a value of five and let's perform some arithmetic on it and we can go Rand plus equal to four and then we can echo out that out on our screen as well again I'm hitting : and W to save that and there you can see that nine comes back you're also going to be able to use a shorthand way of incrementing values and it's going to create that and let's just go echo and we'll use Rand again so we'll have Rand plus plus that is the shorthand that I'm referring to and let's see exactly what happens whenever we use this shorthand notation go Rand plus plus and then also we will change the position of again I'm gonna hit a V right here and why to copy that and then I'm gonna hit P and P and P I'll show you a couple different ways to use incremental and decremental so let's come in here and let's increment this here and put that there and then here we're going to decrement this and decrement that as well and when we execute that you can see exactly what's going on here so it starts off with a value of nine now whenever we call for this to increment what it is doing is as saying print the value of Rand and then after you print it on the screen then increment its value here what it's saying is first increment the value then print it on the screen and that is the reason why we jump from nine to eleven likewise here we're saying printed the value of Rand and then decrement it after you print it and that's why it stays 11 in here we're saying first decrement it and then print it on the screen and that's the reason why we go back to nine now if you want to use floating point values what we're gonna have to do here is this there's a couple different ways of doing this but I'm going to show you how to use Python for this so let's go and create number seven and give it a value of one point two and number eight and give it a value of three point four you're gonna be able to use a whole bunch of different Python commands right here inside of your shell script and we can go number nine is equal to and here we're gonna call Python to execute of course you have to have Python installed on your system for this to work and then you're gonna be able to use any type of Python commands you'd like to use here so you can say number seven and number eight and make sure you put that quote right there and then after you do that you're gonna be able to come in and get the value of number nine we can come over and execute it and you can see indeed that it was able to perform that arithmetic for us now a final thing I'm going to show you here is how we can come in and print out information over multiple lines with what is called the hair script and here I'm going to use cat which is gonna print a file or any string that you pass to it and you can experiment with exactly how that's gonna work basically we're just gonna put cat and you're gonna put those two little arrows inside of there and then you're going to put how the lines are going to end and I like to use n some people use other things and here we can say this text prints and then you put end here at the end again we're gonna skip out to that jump over here and execute it and you can see it prints exactly as you went and to find it right inside of there there you go that's a rough overview of some of the basics using math and printing information out on the screen and using Python and a whole bunch of their different things now I'm gonna jump over and talk about functions and how they work inside of your shell scripts now functions are gonna of course allow you to avoid the need to write duplicate code and another thing here we have all this stuff typed out here inside of him you're probably wondering is there an easy way to avoid going through and hand deleting all this of course there is first you want to hit escape then you're gonna hit lowercase G two times and then you're going to hit and capital G that is lowercase D and capital G there you go everything's been deleted all right back inside of insert mode and of course in the description I have a link to a cheat sheet that has all of the things I talked about here so you can use that now if you want to define a function you're just gonna go and type it in here like this let's just say get date for example and if we want to go and get our current date and time one of the little functions that's available to us is the date function and this is gonna get the current date and time and then we can call return and return is gonna return an exit status number between 0 to 255 you don't really need to worry about all that just know it's going to jump us out of our little function after we get it done and now that we have that set up we can call our function just by going get date and we save that and there you can see it went and printed out all that data information that brings us to the difference between what are called global as well as local variables and how they work in regards to functions so let's go and create a variable inside of here and I'll put Tareq inside of this this is a global variable this is available to every single thing else in your script now I'm going to show you a local error so we'll go demonstrate local go and create our little function inside of here and here we're going to define a local variable that means it is only available inside of this function and I'm going to give it the same name as the name outside of there and then we're going to return outside of our function now you would think I just changed the value for name and you're gonna see indeed that I did not well let's go and call damn local because the function needs to be called for it to execute and then we'll go echo and we will go name and we'll see exactly how this is gonna work for us and after you play with them for a while you're gonna get used to it and there you can see that Dirk printed out the reason why is this version a name is only available inside of here nobody else knows that exists except for this block of your script this is the global name is available everywhere in this situation since we went and created a variable the same name name inside of here is Paul outside of here it is always going to be Derek so hopefully that made some sense for you what a function is going to be pretty worthless for us and unless it is able to come in and receive some values so how we can get our values is let's just go into insert mode here and we'll create a function it's gonna receive two values and then print a sum so we can go get some and you may ask yourself how are we going to get the attributes that are going to be passed to it well we're going to be referring to them with dollar sign 1 and dollar sign 2 for the second one and dollar sign 3 and so forth and so on so let's go and create some local variables and we'll call this number 3 and let's get the first attribute passed inside of there and then we will get number 4 and we'll get the second attribute we can then come in and we can create the sum of both of these values and we're going to add them together and we'll go numb 3 and add and numb 4 and then if we want to pass back the value from our function back to whoever called our function we can just go echo and some like that jump out of there and then we can define our function our values we're gonna pass inside of here so we'll go number is equal to 5 and number two is gonna be equal to 6 and then we'll pass our attributes and to do so we're gonna go sum is gonna have the value pass back inside of here and we'll go get some which is the name of our function and then we pass in the attributes and you just don't put you just put a space inside of here you don't put a column on or anything like that and that is gonna call our functions for us and then we can go echo just to prove that this echo up here is actually returning the value and isn't printing it out we will go echo the sum is some save that and execute and you can see right here the sum is 11 there's a rough overview of how we can go and create functions pass out tributes into them and have values return from them so now let's go and take a look at conditionals well I might as well talk about how we can read information or receive input from the user we're just gonna type in read - P and the P option says that we want to prompt with a string and the string is going to be what is your name and then the value is going that they enter on the keyboard is gonna be stored in the variable called name and then we can come right back and go echo and hello name and execute what is your name Derek hello Derek so you can see how we were able to go in there and get some input for ourselves we could also come in here and say how old are you and then whatever they enter is going to be stored inside of the variable called age and then we're going to use an if statement to decide what we want to do here so we're just going to type in if and you're gonna place your conditions within these brackets like that and the condition I want to check is I'm gonna say if age is greater than or equal to 16 then I want to do something so I'm gonna say oh then and then I'm gonna type in exactly what I want to do I could say you can drive now if I want to check another condition I just go L if like that and then I type in whatever the next condition is and I could say something like age is equal to absolute that guy means right there 15 and then I can say then and echo and say something nice like you can drive next year and then finally we would type in else and this is going to be the default if neither the other two comeback is true and it'll say something like you can't drive and be mean about that and then what you do to close off your overall if statement because there are no brackets or anything here curly braces you're going to type in F I which is gonna be if in backwards of course because that makes sense a hand will come over here and we'll execute how old are you and I could type in 15 whoops got a little bit of an error you jump back over here that's because I didn't type in greater than or equal to was I thinking let's go in here and we'll say greater than or equal to there that is sorry about that save that execute how old are you 15 you can drive next year execute again 16 you can drive and then we'll say - you can't drive thankfully all right so there's an if statement now let's go and let's go and check a couple other things out here gonna go capital V and then D for delete and back into insert mode with I and here I'm gonna say enter a number so I'm gonna go back into I I'm going to show you another different way that we can use conditions here this is gonna be stored in Nam I'm gonna say if and here if I put these parentheses inside here I can now say num is equal to 10 if you don't want to use those other guys that you have available there for you and by the way the other options that were available to do comparisons in other ways like we talked about previously you could go equals with a dash in front of it of course not equal to less than or equal to less than greater than or equal to or greater than so sorry I didn't cover that previously but like I said in the description there is a link to all this code and you can of course jump back and test those out so here what we're using is and what is called the extended integer test so what we're gonna do is we're gonna say if num is equal to 10 then we're going to define what we're gonna do and here we'll say echo your number equals 10 this is just garbage output I'm just put in here and of course you have to close that with fie like that we could also then go in and go if num is greater than 10 then and here we'll say echo it is greater than 10 and then we come in we could still use else and all that other stuff and Elif and all that stuff echo it is less than 10 and you don't put any semicolons there but we do use semicolons as the tutorial continues I'll show you when we use those we could also calm down these if we wanted to do any type of math inside of here so let's say we wanted to check if num is even or not just like and we'll go numb modulus too and we can in check if that is equal to zero in that situation then we want to print out on our screen echo it is even and close off the if statement again and save that and our number we can say ten your number equals ten it is less than ten it is even okay so you can see exactly how that works and the reason why that came back as less than ten is because I said greater than ten if I would have put greater than or equal to go into insert mode with I again greater than or equal to okay save that here ten and you can see it comes out the same and we could change this we could say greater than or equal to ten but either way whatever just writing this code out of my head and that is what we're getting we're also going to be able to use logical operators inside of our shell scripts so we can say something like if and then want to compound these so let's say we want to check if num is greater than 0 and we could also use or or we could also come in here and use not just like that just like other computer programming languages but here we're gonna say that both of these conditions need to be true not one or the other yes a num is less than 11 and then throw then on there like that here we'll say echo num is between 1 and 10 and or a number and what is the same numbers between 1 and 10 let's put 5 inside of there it is less than 10 pops back and 5 is between 1 and 10 so that's how we can use logical operators also you're gonna be able to use logical operators to execute different things so let's say we wanted to go and create a file and then if that worked and the file was created we could then open it up inside of them so this is kind of silly because you would normally just create the file by opening it in vim but so basically what we're saying here is if we were able to create this file then what we want to do is go and have this file opened up inside of them and we can do that of course we could also come in here and use or in much the same way so we could say if sample directory doesn't exist then we want to make it and to check if it exists or not a command we could use here is - D sample directory and then we could go or that's that guy if it doesn't exist we're gonna go make directory sample dir and jump over here execute and our number doesn't really matter and there you can see the sample file was created and here we are going to escape out of this guy and you're going to be able to go LS and you're going to be able to see that the sample directory was created as well as the sample file was created and of course I'm sure that you probably know this but if you want to delete a file you just go or M or remove sample file and if you want to delete a directory you just go RM directory and sample directory and that is how those files are deleted for us we could also come in here and test some strings so what we're gonna do is go escape G G and D G and there that's gone let's go in here and create a couple strings will go string one and let's just have it be nothing and string two and let's give it a value of sad string three and we'll give it a value of happy if we want to test if a string has the value of null and go and put in the string that we want to check and then we'll go then and if this comes back as true then we know that string one is not null and we can close that off we can also check if a string has a value or not by going if and then we would type in - Z and the string that we want to check inside of here so string one then echo string one has no value we can also check for equality between our strings if once again and then what we want to check so it's gonna be string 1 and we're going to check if it is equal to string 2 or string 3 I mean then let's jump up here and change this string 2 and we'll say equals and throw in the value of whatever string 3 is do LF fair just to show you that we can do not equals 2 as well even though we could just go else and leave it the way that it was and then we come in and close that if statement off we're also going to be able to check if they are greater than or less than based off of checking you know alphabetical order of our different strings once again we're going to be doing the same sort of thing here string 2 greater than string 3 then and then once again we could check if they were not equal to each other as well of course or bless them in a situation in exactly the same way that we did previously and then close off that and then jump over here and execute these guys and you can see string 1 has no value but it wasn't null and sat is not equal to happy and sad is greater than happy it just means that S comes after H you're also going to be able to do a whole bunch of checks in regards to files so let's Kommandant man will do an if statement like let's say you wanted to check if file exists you put - e well let's come in here first off and refer to these files will say if file 1 is going to be equal to and we'll have it be called test file of course you have to create these files inside of your directory and it's just a junky and empty file that's all these are and we'll do the same thing for file 2 now we can come in and say if and we want to check if the file exists and we could just refer to the file and then we can go and perform some checks with so we can say then echo and we could say file one exists and we can execute this since s test file one exists ok now there are a ton of other different things that we could then check for so let's come in here and I'll just show you them and you can look at them on your own and here you can see all the other different things you can check in regards to files so we checked if it exists we can then check to see if it is a normal file just by putting the dash F inside of there with dash or we can check if it's readable we can check if it's writable with W X is gonna be is it executable D is gonna be is it a directory capital L is it a symbolic link P is that a pipe s is it a network socket G like that is is it owned by the group capital o is it owned by the user and so forth and so on so those are all the different things you can check in regards to files inside of shell scripting and now I'm going to show you how you can use regular expressions as well so if you want to check regular expressions you're going to have to use what is called the extended test so let's command and let's read some information from the user so let's say we wanted to do something create a script that's going to allow us to validate epsilon that I do here validate a date store whatever they type in inside of date and then we can create a pattern I'm not gonna get that much into regular expressions I already did a tutorial on that you can look on my youtube channel if you'd like check that out let's say we wanted to make sure that they enter in a group of numbers between 0 and 9 and that they provide at least eight of them and we're gonna say that that verifies it is a date and we put the question mark here for saying that's the end of the pattern we're looking for and the carrot says this is the beginning of the pattern we're looking for we can then come in and go F and put these brackets inside of here and then check date equal to tilde and compare it to our pattern and then if it is true we can say that it is valid a great check for this but whatever and then we could say else and say that it is not valid and execute it and as long we go and we put in eight digits it's gonna say that it is valid and if we go and put in anything less than that it's gonna say that it's not valid okay so you can play around with other different regular expressions and see exactly how those work we could also read multiple values inside of our shell scripts I'm not sure if I did that yet or if I covered that yet again capital V and D for delete and here what we're gonna say instead is enter two numbers that we want to sum and if we want to allow them to enter in multiple different numbers we can just go nom one and num2 without putting any commas inside of there and then we can of course come in and some these values num1 plus num2 and then we could echo out on the screen this information and there we go enter two numbers sum and we could say 23 whoop didn't put anything in there so it just came back is 23 and of course Wilco 23 space 45 and there you can see it's able to go and accept multiple different pieces of output just like that another thing we're able to do here and I'm just reviewing some of the different ways that we can output information or or read information I mean let's say that we wanted them to be able to enter information however we don't want it to be displayed on the screen whenever they enter it we would just go read and SP the P is for prompt once again and we'll say in the asus4 secret and we'll say enter the Secrets code and we're gonna store that inside a variable secret and now we could say if secret is equal to password then we go echo enter else wrong password and to enter two numbers the sum whatever we can just do this and whatever enter the secret code and you can see here that if I type in the secret code that it doesn't show but I did indeed enter password and you can see there that enter printed on the screen other things kind of neat is let's say that it seems weird to you that the user would go in and put a space in between the values that they're entering let's say that you instead thought they should put a comma or something else it doesn't really matter what it is here V select all those different things delete get rid of that what we can do is we can store what we want to define as the separation for the attributes that are passed inside of ourselves our shell script but what we want to do here and what how we do that is we go and change what it's called the ifs now what we're gonna do here is we want to input or first save the original version of ifs because that could be useful as our script continues so we go ifs and that's how we get the original value and then we can set what's is going to separate the input value so we'll just go i off s and let's say that we want a comma to separate those that would work perfectly fun and then we could go read - p and then we can say something like enter two numbers to add separated by a comma and then we'll go nom 1 and num2 we could then use parameter expansion to substitute any white space that was entered when they put these attributes inside here so it's gonna accept commas but it's also going to accept any white space that would come around these different values and how you do that is you go nom 1 is equal to getting more into parameter expansion here in a second like literally right after I cover this so you can see exactly what I'm doing here so I'm gonna say num1 and let's say that i want to replace all the blanks or the whitespace inside of there i can do that and i'm gonna replace them with nothing so I'm gonna put nothing at the end of that and that's gonna work perfectly fine for me copy that and then capital P to paste that in there and we'll go and change this to two and change this to two and now we can come in and sum those values just like we did previously so something number one number two and then echo out on our screen and then of course we want to change the IFS back to all the way that it was so it doesn't mess up anything else in our script how you do it's just like this old ifs you put anything inside of there of course and then we can execute enter any two numbers and we can say 23 and comma and we can put a whole bunch of spaces and then 45 and you can see that it's able to perform that information there you didn't have this guy right here replacing their spaces there would be a whole bunch of spaces between or before the plus sign right here so that's the reason why we did that so what exactly do we mean by parameter expansion and what exactly does it do let's go and I'll show you a whole bunch of examples so you can see exactly what it does so let's say I wanted to do something like create a variable whoops let's get rid of that so let's say we wanted to come in here and I wanted to print a value but I wanted to immediately thereafter put some characters after it so I wanted to do something like Derek's toy so here I'll show you so Derek I think I'd be able to type my name by this point so I'm gonna create a variable called Derek and I want to output Derek's toy with a s at the end of it so I'm gonna go like this and I'm gonna put this is what's called a parameter expansion I'm gonna go name like that and then I want to put in and ass at the end of this so that we can do it like that see if I put a name and then I put the S after that without the parameter expansion this is the parameter expansion this guy this guy and this guy if I put it in there that would cause an error and it wouldn't know what I was looking for it definitely wouldn't put the name variable inside of there so that's what that does also we can come in and sample string to explain what I did whenever I replaced all of the blank spaces with nothing here I'll do a sample string if I then wanted to output different information let's say I wanted to change that string to cat I would use a parameter expansion again and I would sample string and I would say I wanna replace dog with cat inside of the strength and that's gonna allow me to do that and there you go energy numbers whatever and there you can see Derek's toy shows up and you can also see that our replace dog with cat another couple things you can do with parameter expansion is let's say that you want to print out a value for a variable but if a value doesn't exist you want to print out a default value well you can do that a little bit of a weird type of thing but trying to cover as many things as I can here so let's say I want the name however if it doesn't exist I want to print Derek instead and also what this is doing right here well if I put just the line like that it's just going to print out Derek if a value for name doesn't already exist if however a value for name does exist it's gonna change the value to it with the equals sign you can play around with that see how that works out for you and now I'm gonna jump in show you exactly how we can use case inside of shell scripts okay so the very first I'm going to do here is I'm gonna ask the user how old are they and then I'm gonna use case to perform different execute different code depending upon the value of age so I'm going to go case age in and I'm going to check some different things so I'm gonna say if the value of age is between 0 and 4 this is really cool I which case work like this for other languages then I want to echo too young for school and then I'm going to put two semicolons to end searching through all the other conditions I could also match only five and in the situation which age is equal to five I could say go to kindergarten and again like that and it's not gonna check in any other different values I could go and check for 6 through 18 just by going like this 6 through 9 and then close that off and then put a pipe inside of there and then put one and then put 0 through 8 so this is gonna check the nope ages 6 through 9 this is going to check the ages 10 through 18 that's what's going on there and then I can come in here of course and perform some arithmetic like let's say I want to figure out what the gray it is could do that just by going age minus 5 and then here I'll go echo go to grade and whatever the value of grade is and then come in here so I'm a colon so close that off like that and then finally we could do a default action and that's just gonna be a star inside of there like that it's good next line star and here we'll say something like echo you are too old for school and close that off and then you're going to type in e-s-a see to end the case and before we execute that might as well show you the last conditional which is the ternary operator and the ternary operator is going to perform different actions based on conditions let's say we have something like and vote give it a value of 0 like false and then we'll have 18 like this what we're gonna do here is we're gonna say if age is greater than or equal to 18 and then we're gonna put a question mark inside of there then we're gonna say you can perform any type of arithmetic or anything you want inside of here but here I'm just gonna do an assignment here we're gonna say the can vote is now gonna have a value of 1 colon and that's gonna be if this condition was true otherwise it's going to be false and in that situation we're going to say I can vote and I just never get the value of 0 and it'll close off all those brackets and then here at the end we can say echo can vote and then see if they can vote or not run it how old are you say 18 go to grade 13 and can't vote comes back as one all right sorry good guys there's a whole bunch of different ways we can work with conditionals now I'm going to take another look at parameter expansions and strings okay now I'm gonna show you what we can do with parameter expansions and regards to strings so let's say we have random string and it's just going to be a random string and let's say we wanted to get the string length could it which could be useful for numerous different reasons Oh string linked and you would just go dollar sign and curly bracket and a hash and then put in whatever your string name is and then close it off with a curly bracket you could also get a strength slice and remember we're using zero index is here and we're going to be going in and asking for a specific index whenever we do this and here we just go curly bracket again and random string and let's say that we want to get whatever is that the second index that's how that's done we could also commit and define a starting index as well as an ending index and get a slice that way so random string and let's say we want to go to two seven and then we could also come in here and get everything that follows after for in this situation I'm going to say at the letter A and then a space and to do that would just go to all this on again and random string and then we're going to put a hash symbol and then I'm going to put a star and an A and a space and curly bracket yeah if we come in here and we save that and jump over here and run it you can get the string length is 15 you can see here random string pops back because we've got everything that follows from the second index on here we go from the second index to the seventh and here once again we get everything that follows after a and a space so there's just a couple little neat things we can do with parameter expansions and strings now let's take a look at looping now we'll start off with a while loop and any time you use a while loop you're going to have to define what you're going to be iterating or incrementing or decrementing whatever what's going to be changing that is eventually going to throw us out of our loop and we just type in while and then we type in num and here I'm going to say less than or equal to ten is how long we're going to loop through this what do here and I'm going to find inside of here what exactly is going to happen in this situation I'm just gonna print out the num and then of course you're going to have to have a value that is going to increment and that's gonna be numb and we're going to have to increment num and there we are and then two and the while loop we just put done at the end and let's go and do another while loop here again this time I'm going to demonstrate how continuing break works so we can just leave the let's just use the number one again so let's come in here and go numb it's gonna be equal to one and we'll throw in another while loop and once again we're going to be incrementing num and we're going to be incrementing as long as it is less than or equal to 20 and then put two inside of here and let's say that we don't want to print even numbers how would we do that well if you remember what we did previously with the if statement we went and took the modulus of our value and then we compared it to zero and closed that off and then we're going to say then in this situation we are going to increment the value of a number and then we are going to continue and what that's going to do is this line is going to take care of the incrementing and it continues going to jump back up here and it's not going to cover anything else that was done anywhere after this if statement now let's say that that condition doesn't come back is true let's say that we want to jump completely out of our looping structure altogether and let's say that we want to jump out if num is greater than or equal to fifteen at any point in time then and to do that we would type in break and then we can close off our if statement and then we can come down here otherwise and just print out the value of num and then of course we're going to have to also come in here increment once again the value of num because it's not going to be incremented otherwise and then finally close off our while loop whoops hit ctrl s there instead of coming and hitting escape and W and what you come over here and execute those and you can see how those all worked out there up here we went in for an ED 0 through 10 and right here we only printed the odd numbers and whenever we got the 15 we didn't print it out we instead jumped out of the loop so there's a couple of different ways we can use while loops now let's come in here and take a look at until which is basically the opposite while loop basically with a while loop we're saying we're going to continue doing something as long as the condition is true and with a until we're going to continue doing something until a condition is true so it's just slightly different and to use until let's just use nom again and we're gonna say greater than 10 so we're gonna loop until the value of num is greater than 10 and here we can then come in and once again echo out the changing value we are once again going to have to increment our value here right like this and then you can close a until loop in exactly the same way and you can go and execute that on your own I think you get exactly how that's gonna work you can also come in here and output file information and let's go in here and whoops let's escape out of that and let's go and do LS to show everything we go out here what's say we willing to read and loop through an output information that is in a file well the file that I'm gonna use in this situation it was gonna be my Barry Bonds stats and he's a baseball player if you didn't know and it's gonna have his batting average his RBIs and his total number of home runs inside air so what I want to do is I want to go and I want to loop through all that Barry Bonds data so we're gonna come in once again we're gonna jump into this just using hello world again and I'm gonna say that want to use a while loop here and I want to read information and the information and there's going to be average RBIs home runs and I'm gonna say do now in this situation I'm gonna show you another way that we can output information I'm gonna use printf in this situation and I can say average let's just make that older case and I can go and get my average by going average like that and then I can throw a newline in there the printf allows me to use new lines inside of here I can then go rbis and grab the RBI's out of that once again we're gonna throw a new line in there and we can also go in and do homeruns and you could do multiple different lines of data also to grab out of there if you'd like and for homeruns of course we're going to type in hrs and close that off and and throw in another new line and you close off the printf statement and now at the end here what I just need to do is pipe data into the while loop and the data being the text file that I showed you just a second ago so Barry Bonds and texts right like that and we could save that and execute it and you can say that it went and sucked all that information directly out of that file and then put all this other information there so there's a way that we can go in here and actually work with the files and grab information if we'd like to leave that out of there and of course there's also going to be a for loop and for loops come in a couple different ways inside of shell scripting so this is the C style way of working with for loop so here what we're gonna do is we're gonna continue cycling through this as long as I is less than or equal to 10 and we are going to also come in here and increment the value of I each time it jumps through our loop close that off again we're going to type in do and I'll just keep this nice and simple once again and output the information there and you close the for loop like that and we can also come in and cycle through ranges with for loops and you're gonna see another way whenever I cover a raise here in a second exactly how we can cycle through an array and we could throw that right there and do and I go and dollar sign I and that's gonna be closed off in exactly the same way and we can jump in and execute it and you can see right here that it went and cycled through all that information and you can also say that went and printed out the values of 0 through 10 so pretty cool stuff yeah let's get rid of that and now I just mentioned Reyes so why don't we just talk about arrays now the one thing with bash arrays is that you're only ever gonna be able to have one dimensional array as inside of bashes and of course the indexes are gonna start at zero unless you define you want them to start something else so I can come in here and create an array just call it favorite numbers very easy and we can go 3.14 again you do not put commas inside of here two seven eight and five seven seven two one and what other number do we have here bonus points if you know why these numbers are my favorites and we can come in here and echo out they thought that is how to create an array of course if I didn't say that we can come in and echo out a value inside of here quite easily dollar sign and just go favorite nums and then let's say we want the zero index of course that's what pi is whoops just gave away a part of the little question I had there and we could also add a value to an array in a similar type of way so let's go the fourth index and let's say that we wanted to add another value inside of there and we could echo out the value in much the same way so let's just say in G our favorite noms in here will echo out our fourth index close that curly bracket we can also add a group of values to our arise favorite numbers and you just go plus equal to and one and seven inside of there and then of course we can output all of our array values using for loops which I told you job is gonna cover and here I am covering it so favorite numbers and to do that you just put bracket like that and star and a closing bracket like that curl the bracket like this and to do and and echo and just grab this value and then done and we can come in and save that and if we run it you can see exactly how all that information was able to print out here I grab the indexer I grab another index in here I printed out all of the information stored in the array using the for loop let's continue doing some more stuff here you're also going to be able to output indexes using much the same style as this for loop so why don't I just come in here and grab this guy once again capital V Y jump down here capital P and all I need to do in this situation is come down here insert and throw an app sign in there that's gonna print out all of my indexes I'm also going to be able to get the length of the array or the number of items that are stored inside of there where do the Iraq and there we go and then just throw the @ sign inside of there and that's gonna work for us we can also get the length of an array element like an individual element so we'll say index 3 length and to do that once again curly bracket favorite nomes and let's say we want to get the length of the individual element there and we can do that we're also going to be able to come in here and sort our rice I will say sorted nums and how we would do that is go dollar sign and then we could go for I in and then the other side favorite nums and then in @ sign inside of there a curly bracket closing I like this and then whoops don't forget to put the semicolon inside of there and then we'll go do and we can go echo and all the sign I and you can I don't know if I mentioned this before you can put semicolons in here but not necessarily do you need to do that and then finally what you're gonna do here at the end is just go dawn and put right there and then you're going to put in sort at the end and then we're going to close off of those brackets and that is how easy it is to sort and write and what the heck let's go and print the array once again so escape out of this V and Y and let's jump down here do a capital P inside of there and we want to get our sorted gnomes sorted gnomes and here what we're gonna do is change this to a star and this will be due and that'll be echo and this will be done and all that works and finally if you want to delete an array element you go unset and let's say I want to delete sorted gnomes and I want to delete the second index that's how that would work or they you know whatever is in index one and of course if you want to delete the whole Ray you just go unset and you want to do sorted numbness and that's exactly how that works and we come over here ok yeah run it and there you can see we were able to come in and get our array length whoop and you can see here I made a little bit of bug now let's come in here if you want to get the length of our right you have to come in here and put a dollar sign in there right like that so a or a hash symbol inside of there like this and then you can come out here and put another hash symbol that's how you get the two lengths sorry about that little error long tutorials sometimes make me make errors and there you can go ray length and this tells you how many items in the array this tells you how many the length of the individual item here we went and printed out all of the different items in a sorted manner so there you are that is a whole bunch of different ways to loop now let's take a look at positional parameters and how we're able to accept the values from the command line okay so positional parameters are variables that can store that on the command line in variable names 0 through 9 however the variable 0 is always going to contain the path to the executed script so the most part you're going to be working with one through nine now you're also going to be able to access names past nine but you're gonna have to use a parameter expansion like this I'm right like this if you want to go and get ten all that stuff probably didn't make any sense so what I'm gonna do here is actually just do it and once I show you an example it'll all make 100% sense so let's go echo and we will go first arguments and let's go and get the first argument right like this and then what we're gonna do is we're also going to come in here and sum all the values that are entered in off of the command line so what I'm going to do here is go wow and we dollar son and hash and that's gonna tell you the number of arguments the dollar sign and the hash and greater than zero so it's gonna cycle through flips make sure you don't put a space there greater than zero and do and now what I'm gonna do is I'm going to get the first argument that is passed in from the command line just show you whenever I run it it'll all make hundred percent sense I'm gonna go sum and I'm going to increment some by whatever the first number is that they passed inside of there and then I'm going to call chef and shift is gonna move the value that is in the second argument that they pass through the command line into the one position and then continue adding all those together and now I'm going to come in here and I'm gonna execute this and it's all gonna make complete sense so we'll go hello world and we'll say something like one two three four and five and there you can see first argument is equal to one and why not end the tutorial with a mistake because it would also make a lot of sense if I want to come in here and also show you the sum of those values and how I would do that is just come in here like this and go sum and escape and W and let's come in and run it again and there you can see first it got the first argument which is this guy right here and then it summed all the other different arguments in the that were passed from the command line so there you go guys that is a rundown of the basic syntax you used in writing shell scripts using bash hope you guys found that useful and just like always please leave your questions and comments below otherwise till next time
Info
Channel: Derek Banas
Views: 862,487
Rating: undefined out of 5
Keywords: Shell Scripting Tutorial, Shell Scripting, Bash, Bash Shell Scripting, Learn Shell Scripting, Learn Bash
Id: hwrnmQumtPw
Channel Id: undefined
Length: 59min 11sec (3551 seconds)
Published: Sat Jun 11 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.