Shell Scripting Crash Course - Beginner Level

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey what's going on guys in this video we're gonna do something a little different maybe a little unexpected this is going to be an intro to shell scripting and this is a pure beginners guide so if you've done any kind of shell scripting with bash or any other dialect you'll probably know everything that I'm about to show you but feel free to watch as a refresher for those of you that don't really know what a shell script is it's a basically a program designed to run in the UNIX shell now we can run commands unix commands on a Mac or Linux or on Windows using something like get bash or the Windows Linux subsystem so we can you know list contents of a directory and we can create files with like touch and you guys have seen me do that I actually have a pretty decent tutorial on its it's actually called Linux tutorial basic command line but of course it works on the Mac they're the same same commands or just Unix commands so I would definitely suggest that no matter what type of programmer you are even if you are a front-end web developer I would definitely suggest at least knowing what is in this video which is basically just navigating around files copying files you know creating folders just basic stuff like that so I would suggest that now this video I want to deal with actual scripting because you can run scripts from within your shell so that's what we're gonna do now as far as you know do you need this now this is very important in the world of like sysadmin or IT you should really know this stuff in web development it's not that important unless you're really you know heavy into like DevOps and managing Linux servers things like that then you should you should know this stuff but even if you're like a front-end developer or a full stack developer you should know the basics because you may come across it in my job with this channel is to just give you guys as much information as I can so that when you run into this stuff you at least aren't completely lost and you know you're not like you know what the hell is a dot sh file alright so we're just going to create a cheat sheet that you can put somewhere on your harddrive and keep it there and if you ever need to reference it you can do that all right and it's kind of just a fun little video if you want to follow along alright so let's see before we start let's talk a little bit about what Bosch is in case you don't know Bosch is basically a dialect of a shell syntax called the bourne shell there's the bourne shell there's the c shell not like see the water but like the letter C and these syntaxes have different dialects and Bosch is one of them there's also like zsh there's some other ones as well then I'm not really familiar with but Bosch is basically the standard okay when you start up your Mac and you open up your terminal that that's Bosch alright and on Windows you guys have seen me use get Bosch over and over and that you know of course that's Bosch as well so that's what we'll be using as basically our scripting language or command language so what we're gonna do here is I'm in a folder here called shell script tutorial and as you can see I I didn't LS there's nothing in it so I'm gonna create a file called my script and I'm gonna give it a dot SH extension okay dot SH is basically the extension for the bourne shell a bourne shell script so now you can see i have that file here now what i want to do is open this file with any kind of text editor I'm gonna use vs code because I use vs code for pretty much everything but you can open with anything that you can edit text with okay so we're gonna open up that file and from now on I'm gonna use my integrated terminal down here so I have to keep you know move going back and forth so before we try and run this file before we put anything in it we try running it I just want to make it executable because by default if we run it we're probably gonna get a permissions denied error so to do that we would just want to use the chmod command change mode and we want to do plus so we want to add X which is executable executable permission and then the name of the file which is my script dot SH alright so once we do that we should be able to run the file and to run the file we just do dot slash my script dot SH and of course it's not going to do anything because we have nothing in here all right now the first thing that we want to put the very first thing is basically a declaration of the dialect we're using which is bash okay now if we go down here into our command line I'm gonna clear this out and I'm gonna just do which we're gonna run the witch command and say bash and this is gonna show me where Bash is on my system which by default is bin slash bash if you're using like Windows with something like git bash it'll probably be something different but we are going to go to the top of our file and we're gonna put something called a shebang and that is a sharp okay so this is a number sign also called a sharp and then an exclamation which is also called a bang so sharp bang it's it's called a shebang for short so then we just want to put the location of bass which is slash bin slash bash all right that's the very first thing that we want in our script so let's do something very very simple we're just going to echo something out now before I do that I'm just gonna put a comment and in comments are just use number signs okay so we're just gonna say echo command and we're just going to simply say echo and I'll echo out let's say hello world alright so we'll save that and let's go down here and we'll run it with dot slash my script dot s H and it echoes out hello world alright so you've officially created your first bash script now we can also create variables so let's go down here and I'm just gonna kind of label everything so that you know when I create least these chichi kind of documents I like to just clearly label everything that we're doing so variables by convention should be uppercase so we're gonna say uppercase by convention and what that means is that it's not mandatory the lowercase variable will work but it's it's the norm to use uppercase so that that's what you should do as far as what's allowed in a variable its letters numbers and underscores okay and to create a variable we can simply just say let's create one called name and we'll set it to Brad okay so that's how we can create a variable then we can go ahead and do another echo and I'll say my name is now if I just do name it's just gonna say my name is name to actually reference a defined variable you want to use the money sign in front of it all right I'm just gonna comment out this here and then let's go ahead and run this and we get my name is Brad okay and you also might see this syntax so the money signed with curly braces so it's kind of like a JavaScript literal variable but if I go ahead and run that we should get the same thing all right so that's variables now you can also deal with user input so if you wanted to have a user put something in and then use whatever they typed in you could use read so read - P we want to prompt the user and we'll just say enter your name and put a colon here and then right here we want a variable that's gonna represent whatever they put in all right and I'm just gonna comment this name variable out so now whatever they type in we can use name as as that data so let's say echo and we'll say hello a money sign name nice to meet you okay so go ahead and run the file and whoops I get a comment this out too so if will say Brad and we get hello Brad nice to meet you all right so that's that that's user input and we'll revisit that we'll use some user input again down below so next thing I want to look at is what is conditionals okay so we'll start off with a simple if statement so for an if statement we're gonna say if and then we're gonna open up some brackets with some spaces and let's actually let's uncomment the name variable and we'll just say if and we want to use quotes here if name is double equals two let's say Brad now here we want to put a then we want to put the word Bend and then whatever we want to do so we'll just echo out we'll say your name is Brad now to end the if statement it's kind of weird we do if backwards so fi okay so let's save that let's go down here and clear this up and we'll run it and we get your name is Brad alright so that's a simple very simple if statement now what I'm gonna do is copy this and then comment it out and I want to do an if-else okay so I'll paste that in and then under the echo let's put an else oops else and then let's echo your name is not Brad alright so we'll save that and then what I'll do is change the name to let's say Jack and then we'll run our script and we get your name is not Brad so very simple now we can also do an else--if and actually it's it's called an Elif LIF so I'm going to just copy this and comment it out and let's say it's put a comment here we'll say else if also called elif paste that in now let's say after we check to see if the name is Brad before the else we also want to check so we'll say LF brackets we also want to check to see if the name is equal to let's say Jack all right and then we want to put then and then whatever we want to do which in this case will say your name is Jack all right and then else we'll say your name is not Brad or Jack okay so let's go ahead and run that and we get your name is Jack if I were to put something else in here like Bob and run it we get your name is not Brad or Jack all right so some very simple logic let's talk a little bit about comparisons okay and I know I'm going a little fast but if you guys know any programming languages at all this this is very basic it's just it's just if else if else if so let's talk about comparisons we have some basically like operators we can work with okay so up here we just did if something is equal to something but I'm gonna pay something in right here just a block of comments and we can use for instance - EQ to see if values are equal and E to see if they're not equal G T to see if they're greater than greater than or equal so we can use these inside of if statements so what I'm gonna do is I'm going to create a couple variables let's say num1 and let's set that to three and then we'll say num2 and we'll set that to five all right and then what i'm gonna do is just i'll just type it out so we'll say if let's say if num1 is let's do greater than so - GT num2 then actually right here so say then and will echo out num 1 is greater then num2 else let's echo out num 1 oops we need our money sign will say num 1 is less than num2 ok and then we want to end that with fi so let's go ahead and save that I'll clear this up right here and let's run it so we get 3 is less than 5 which of course is true let's add a 1/2 this will make num 1 31 save it and run it or run our command and 31 is greater than 5 all right so you can use any of these in your logic alright so next thing I want to look at is file conditions all right so I'm gonna paste another block of comments in here another block of info and basically we can look at files in our directory and see if it's a file if it exists if it's readable if it's if the user ID is set on on the file if it's writable if it's executable things like that and we can use like - D - E all these these flags so what I'm going to do is I'm going to create a variable called file and set it to let's say test dot txt and this could be any file and then I'm just gonna say if and then let's use let's use - F which is going to check to see if it's a file and we just want to put in here the variable okay so if that then let's echo and we'll say file is a file else then let's echo file is not a file and then we'll just end the if okay now if I go ahead and run this you guys probably know what's gonna happen we're gonna let me just comment this out up here all right so save that and let's run this again so we get test dot txt is not a file which is true because it doesn't even exist and we can check to see if it exists with - II will still get the the is not a file message but the logic here is checking to see if it exists so let's put that back to F and now let's create that file so we'll just say touch test dot txt and as you can see up here that file was created so now if we run this which lips we want to run this we get test dot txt is a file ok if I were to delete that and we can delete it with we can say RM test dot txt and that will delete it and then let's create a folder we'll say make directory or mkdir test dot txt ok now this is not a file so if we run our script we'll see it's not a file but if we put an e here which just test to see if it exists let's say exists and then we'll say does not exist so if we run this it does exist ok it's not a file it's a folder but it does exist alright so you can see we can use these different Flags here all right so enough with that let's move on to case statements which are basically like switches okay so what I want to do is I want to make it a little more interesting and we're going to take some user input and we're going to take a look at what they put in and then respond to it so let's say read - P okay we want to prompt them and we're gonna say are you 21 or over we're gonna say answer yes or no or Y or n and then when they put in their answer it's going to get put in a variable called answer okay and then that's what we want to run the case statement on so we're gonna say case answer in whoops not input in and then right here is basically we're gonna add some some test cases so if they put in a lower case Y or an upper case Y or they put in the word yes which we can do like this so another set of brackets we want it to be case insensitive that's why I put in the lower case and the upper case and then at the end of this line we want to put a closing parenthesis which I know looks a little weird this isn't really something you see very often but this is required and that's going to be the first case if they put in any case of Y or the word yes in any case then what do we want to do we want to echo out let's just echo out you can have a beer smiley face smiley face is very important and then we want to end this with a double semicolon okay and then the next one next case we want to check or next whatever input they put in we want a lower case and upper case N or the word no so another set of brackets oh oh parentheses and let's echo sorry no drinking okay and then we'll end this with double semicolon all right now here I'm going to end I'm actually gonna put an asterisk and then an ending parenthesis and then right here is what's is what's going to happen if they don't put either one of these so basically like the default if you're familiar with a like a JavaScript switch we have the default if it doesn't match any any of this so in this case we're gonna say echo and let's say please enter why / yes or in / no okay and then we'll put our double semicolon and then we have to close the case which just like the if statement is just case backwards I'm sorry backwards I let my accident slip out a little too much there so that should ask the question allow us to either put Y or yes and or no if we don't put either of those then we should get this all right so let's make sure we save this and let's clear this up down here and we'll run our script first I forgot to keep forgetting to comment this stuff out here but anyways we'll go ahead and answer this so are you 21 or over we'll say yes or why and maybe we get you can have a beer okay I'm just gonna save this after commenting that oh let's run it again let's say no sorry no drinking let's say n sorry no drinking let's say none of your business please enter yes or no okay so simple as that to create a case statement and integrate user input all right so now we're gonna take a look at loops so before I forget I'm just going to comment that out and let's look at a simple for loop we'll say for loop basically what I'm gonna do here is I'm just gonna create a variable called names and I'm gonna put in here a couple names will say Brad Kevin I don't know Alice and Mark okay so we got a bunch of names and what I want to do is just loop through these names so I'm going to say for name so I'm creating a variable within the for loop called name basically for each iteration in and then we want that names variable so we want to say names okay we're not putting the money sign here because we're actually defining the variable here here we're actually calling this variable and then we just want to do do so we'll say do not does do and then let's do echo and we'll just say hello to each name so we'll say hello name now we want the money sign because they're actually using the variable and then we just want to do done okay so we'll go ahead and let's clear this up by the way to clear this up I'm doing ctrl L so we'll run our script and it goes through the names and just says hello to each person all right very very simple next thing I want to do is I'm gonna use a for loop again but I want to do something a little more interesting let's go ahead and rename some files all right so I'm going to go down to my command line and create a couple files I'm going to say touch 1 dot txt - dot txt 3 dot txt so if I run that you can see up here on the side we have 3 txt files so let's put a comment here and we'll say for loop to rename files so we're gonna say files we're gonna create a variable and basically we want to put here a money sign and in some parentheses and we want to list all the text files so star dot txt this is basically like a wild card so anything that has the text extension is gonna get put in here and then we're gonna create a variable called new and just set that to the text new because what I want to do is rename each one - new - and then whatever the current file name is so let's do for file that'll be our iteration variable in files and then we're gonna say do and as we're renaming them we'll go ahead and echo out renaming file - new - and then file alright and then to actually do the rename we're going to use the MV command so we're gonna say file and then new - file okay and that should be it so we want to say done and save and let's clear this up and run it we want to run our my script and we get renaming 1 2 & 3 dot txt and now if we look up here you can see that they're renamed new new - 1 new - 2 new - 3 all right so imagine if you had like 500 files you wanted to rename this would obviously save you a little time so it's good for stuff like this this is what shell scripting is really is really good for all right so let's comment that out now what I want to do is I want to show you a while loop but I want to do something a little interesting so we're going to read through a file line-by-line okay now none of our files have anything in them so I'm going to open up new - one and I'm just gonna get some sample text one second we just open up a browser and I'm gonna go to lip sub dot-com and just generate some text and just grab just grab a couple paragraphs okay and we'll put that inside the text file save it and close it all right now inside our while loop actually before we do that let's create a variable called line this is basically like the the index or the the iterator we're gonna start at line one and then let's say while read - are so read and then current underscore line so we're basically creating this variable to use and then we're going to say do and we want to echo out let's echo out the line : and then current line okay current line is going to actually be the text of the line okay now we have to iterate this bottom sorry not iterate increment this by one so down here now let's put in our let's put in double parenthesis and we're going to say line plus plus that'll iterate it by one and then we want to do done now since we're actually reading from a file we want to put a less than and then the name of the file so we're gonna do dot slash new - 1 dot txt or not text txt okay so that should do it so we'll save this so make sure all this is commented out and we'll go down here and clear this out and run it and there we go so it's gonna print out each line with the line number okay so we started with one and then it's just gonna go to to the next line three four and you can see it says it's actually counting the whitespace yl4 is nothing there's nothing there five six okay so that's that's a that's a while loop now let's take a look at a simple function okay we can have functions just like we can in any real language so function and very simple this is gonna be a very familiar syntax for anyone that knows any language really we'll say say hello and then let's echo out hello world and then of course we have to run it so we'll say say hello like that and save let's clear this up and run it and we get hello world alright so it looks just like a JavaScript function or a PHP function or something like that alright so what if we wanted parameters so let's comment this out and let's say function with params so we'll call this greet and let's say echo and let's say we want to say hello I am something I whatever name and I am and then the age now we're not going to do like name age like you might expect we're gonna use what are called positional parameters so the first one will be money sign 1 the second one will be money sign 2 and so on so we're gonna say hello I am money sign 1 and I am money sign - okay so that's our function now to call it we can say greet and then we're going to provide our parameters so the first one will be will say Brad the second one is gonna be 26 no just kidding 36 and we'll save that and let's run it so we get hello I am Brad and I am 36 okay so it just looked there positional parameters it's gonna look at the first one I'm gonna add that second one will be that alright so that's pretty much it guys actually one more thing we could do is I'll just show you how we can put basic commands in here like if we wanted to let's say create a folder and put a file in it and then write to that file let's do that so we'll say create folder and right to a file alright so this is something you could easily do in the command line running multiple commands but you could create a script to do it so let's say make directory let's just call it hello OOP set on capital so we'll say make directory hello and let's then create a file in that directory so we'll say touch hello slash and let's put a file called world dot txt okay now I want to add I want to write to that file we can do that by saying echo hello world now we don't just want to echo it in the terminal we want to add it to that file so we use a double greater-than or double arrow and then we want to put that in hello slash world dot txt and then finally we'll just echo out to the terminal and we'll say created hello slash world dot txt okay so that should do all that stuff drips just by running this script so let's try it clear this up and we'll run it and up here you can see we have a folder called hello if we look inside we have a file called world dot txt if I look at that we have hello world written inside of it okay so you can imagine just the things you can do within your file system by writing a script file okay so you can save yourself a lot of time if you do like repetitive tasks stuff like that all right so I hope you guys enjoyed this I know it's something that's a little different but I want to start venturing out into other areas of programming other languages maybe even getting into like Java you know C++ things like that it'll be at a very basic level at first but let me know if that's something that you guys are interested in because I'm not going to say I'm getting tired of JavaScript that's what this channel is really based on and a little PHP some other you know web web programming languages but I'd like to get into some more general-purpose programming languages as well so let me know what you guys think if you liked the video please leave a like and that's it thanks for watching
Info
Channel: Traversy Media
Views: 435,189
Rating: undefined out of 5
Keywords: shell scripting, shell scripts, bash, bash script, unix shell, linux shell
Id: v-F3YLd6oMw
Channel Id: undefined
Length: 32min 18sec (1938 seconds)
Published: Thu May 17 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.