Transcript for:
Introduction to Shell Scripting Basics

hello everyone my name is Abhishek and welcome back to my channel so today I'm here with an exciting content that is shell scripting tutorial okay so I have posted this a couple of weeks back or a month back on my channel that I'm going to do a complete shell scripting uh course where I'll talk it's kind of shell scripting Zero to Hero right and today I'm here where I'll talk about the basics of shell script we'll try to understand what is shell script we'll try to look at the cell scripting in the prospective of a devops engineer right so why cell scripting is used by a devops engineer what is the role of shell scripting in a devops engineer day-to-day activities and further we'll also try to look at some Advanced as well as intermediate level shell scripting what are the different components that are involved what makes a cell scripting complicated or I'll give you an example of a complicated cell script so this is what we are going to learn today and I'll also try to put timestamps of each and everything that we are trying to discuss not each and everything but most of the things so that if you already know some of these com Concepts you can look at the timestamp and you can directly jump over to that specific section of the video okay so without wasting any time now let's start with the basics and try to understand what is cell scripting at all okay so what is Automation in your uh thought process okay so automation is a process where you will try to reduce your manual activities right let's say you are given a manual activity so now to reduce this manual activity you choose to automate this one this is common in any field right this has nothing to do with devops or software so even in your day-to-day activities if you are doing something constantly and something that is very tedious or something that requires a lot of your effort you will try to do automation so similarly if you are doing any such thing on your Linux hosted machine or on your Linux compute whether it can be a Linux machine on your AWS right or a Linux machine that you install on your laptop I mean if you take a laptop and if you install Linux software that becomes a Linux virtual machine you can use a virtualization software or you can use Oracle virtualbox and you can create a virtual VM now what what are the kind of things that we can automate on a Linux machine so let's take a very simple example somebody asks asked me okay says Abhishek write numbers from 1 to 10. so what I'll simply do I'll use the Echo command I'll print the numbers from 1 to 10. now you might say that this is okay this is very simple and I've done it without any automation or without requirement of initial scripting now what if this number increases from 10 to 1000. still okay so I can spend more time and I can increase this numbers from 10 to 1000 I can keep writing now what if I increase the zeros so I keep on adding more zeros so now it is technically impossible so in such cases or let's say somebody asks you to create okay uh 100 files on your Linux machine what are files files are very similar to your windows so on Windows you create a notepad or on Windows you create a MP3 file or mp4 file similarly in Linux also you can create those files you can create zip files you can create tar files so somebody came to me and said that Abhishek create 100 files so I'll use one of the Linux commands probably called as touch let for example so tax is a command which creates files which will look into it so what I'll do is I'll use the touch command to create 100 files so somebody says that okay not hundred two thousand so I'll take more time and further if they keep increasing discount so this is why you need automation or this is why you need cell scripting so this is a very basic or very simple example for you to understand what is shell scripting in Linux so shell scripting in Linux is a process of automating your day-to-day activities or regular activities on your Linux computer and like I mentioned this can be anywhere so irrespective of your AWS hosted Linux virtual machine or your laptop Linux virtual machine as far as you are using the same shell so in your cell scripting you can Define uh which shell to execute this script which I'll show you so this can be executed anywhere okay so this is what we are going to learn today so this is the reason or this is the one of the uh how do I say like why do you require shell scripting is an example that I just stated now so what I'll what we'll try to do is we will try to learn how how to write a shell script okay now that we understood what is cell scripting now we need to start writing a cell script first of all so once you start writing a cell script or what are the basic requirements so like any programming language whether you're learning Java whether you're using whether you're learning C so whenever you submit a set of actions okay so let's quickly jump on to the terminal and let's start looking from here so this is my Linux machine so I'm currently using a Mac Mac laptop so I don't have to install anything but if you're on windows so what you need to do before this video you need to create an account uh with AWS or any Cloud platform and or you can also use Oracle virtualbox and install a virtual machine but to make it easy uh you can go to AWS create a virtual machine and you can start uh following this demo so first of all to write a shell script what is the basic thing that requires is you need to have a file so inside your file you write a script so how do you create file in on a Linux machine so one of the basic commands that works on any uh Linux machine is touch so you can say touch and probably let's say this is okay first shell script okay because I am writing a shell script the extension would be dot sh similar to if you are writing a python file it would be dot py if you're writing Java file so I mean depending upon the program language you use the extension so this is how you create a file on your Linux machine now as soon as I do it now how do I list the files so list the files is nothing but if you are using a Windows machine you can simply go to any specific folder like you have C drive you have D drive what you will do you will you will use your cursor you'll use your mouse and you will go to the C drive and you will click on the button on the C drive and it shows all the folders that are present or all the files that are present inside your drive similarly in Linux you cannot do all of those things if you don't have your uh you know uh prompt or if you don't have uh I mean if you're simply using the command line the command that used here is LS so as you do LS so this is the file that I just created first shell script dot sh so this shows all the files now if I want to look the files with timestamp okay so which file is created first and which file is created next I can simply say LS minus LTR and it shows the files that is created who created the file when did they create what are the permissions which group they belongs to now I'll explain about each and everything uh in detail but for now you understood what is LS and what is LS minus LTR now you can ask me that I just remember the command called LS which is used to list the files but I don't know the extension that you use that is minus LTR okay or you might say that okay I know the ls command but I totally forgot what and why this command is used so simply Linux provides you an option which is very good called man man is nothing but manual so just suffix any command with man and simply type the command so it gives you the details of the command like it can be any command previously we use the touch command so I can suffix touch with man and once I enter press enter it provides the details of this command so what does it say touch change file access and modification times so description touch utility sets the modification and access times of the files if any file does not exist it is created with default permissions okay so what does it say if you're using the touch command and if the file does not exist it is created with the default permissions that's what it describes so similarly if I use the ls and if I want to look at all the options that are provided by the ls command like I just used minus LTR so what is minus t stands for if I simply type minus t here okay so here it describes about the option for minus t similarly you can look at all the options that are available for this command okay for any command so what is the thing that we use here we simply suffix it with the man command so till now we just learn about three commands Okay so that's how your Linux Is So within a matter of one minute we learned three commands already what are the commands that we learned First Command that we learned is LS okay what is LS used for LS is used for listing the files or folders right files or folders or simply if you want to list a directory then you simply use the ls command and I also showed you how to create a file that is using the touch command right and we also learned about the another new command that is man command what is man command man command is used as a manual for any command that you want to reference so how do you use the man command simply suffix suffix is use man in front of the command that you want to use simply like man LS or man touch or any other commands that we are going to learn so if you are forgetting something or if you just know the command but you are not sure what this command is used for what is your go to option the manual command that is present on your Linux machine so any Linux machines will have this basic commands okay so these are installed by default right so I'm I have not installed any of these things by default whenever you have a Linux machine which comes with shell so they have all of these uh binaries or all of these commands pre-installed on the Linux machine okay so perfect now we just learned about the three commands now let me open the file okay so what I've done I've used the touch command to create the file I've used the ls command to verify that the file is created but what I want actually is to write a shell script in this file so to write a shell script in this file I have to open the file right so similarly if you are on Windows and uh you somebody asked you to write a notepad probably you create a notepad to list some of the items that you want to reference in the future so what you will do you will right click and you will create a file using the new file option and then you open the file in a notepad and you start writing similarly you can also use the VM command so Vim or VI so Vim is not available by default you have to install it but VI is directly available on any platform so if you're using any Linux machine VI is by default available so you can directly use VI so now I'll use the vi and I'll open the file that I just created so now you might say that okay there are hundreds of files here so what you'll do simply use LS command if you don't remember the file name that you created double click which will save the file name so Linux is kind of different so you don't have to right click and use the copy option that you use on Windows if you simply double click on any of the content it gets automatically copied and you can directly use command V or Ctrl V depending upon your operating system and that gets pasted on your terminal okay so if people are not aware how I am using this terminal what is this terminal so it's very easy you have multiple terminal options so all that you need to do is go to your browser and let's say you're on Windows come here and say download putty so putty is one of the terminal which provides you the graphical user interface okay so using putty you can basically SSH to your virtual machine and you can start using the terminal uh that that I am kind of using I'm basically using a tool called terminal the name is also terminal or item that is I term that is provided by Mac okay so now without wasting time now let me open this file what is the command to open the file VI and if you want a good uh user uh friendly interface then I can use Vim so Vim is also it's not difficult to download I'll show you how to download Vim as well but if you don't have whim don't worry you can directly use VI so now the file is open so what is the command to open the file you can simply use them now let's say what happens if I don't use the touch command and if I directly use the Vim command okay so let me call Vim second shell script dot sh so any guesses what happens here okay as soon as I press enter so the file is automatically created it says this is a new file and you can start writing on this file okay so even you can basically create a file using Vim command but what is the point of using the touch command so you might ask me okay if touch and whim are using the same thing and Vim has advantage over touch or VA has advantage over touch that is you can create the file and you can write the file at the same time then why should I use the touch command so you might get this question but touch command is basically used in your automations so it is very important to learn the touch command and if it is very important to remember the touch comment because whenever you are doing automations you cannot use the Vim command because Vim is basically used to write inside a file but whenever you whenever you're doing some automations like you might have you have you might have a requirement to create thousand files okay for some random reason let's forget about the reason but somebody comes to you and says Abhishek create thousand files now you cannot create no you cannot use Vim inside your script because Vim opens the file okay it creates and also opens the file what happens if the file is opened let's say uh you have a Windows laptop and I'll play 1000 movies at once what happens your machine gets crashed okay similarly even in Linux you cannot open a lot of files and if if a lot of files are open then it's a problem or uh what happens if a movie is opened and left like that and if you try to close that process window says that okay this process is used by somebody else first close that file and then only you can close this process right where let's say you're watching a movie and you want to close the uh mp3 mp3 or MP4 player that you are using so what when you try to close the MP4 player what your windows says okay you cannot close the MP4 player because somebody is using the MP4 player similarly you cannot keep a lot of files open okay so that is why we also use Touch command so now let me open this one so I'll just double click and using the Vim or VI I'll open this file and now to start writing the shell script the very first thing that you do is use this specific indentation or use the specific syntax slash okay uh hashtag followed by exclamatory followed by slash so this is called she bang so what is this coil this is called shebang so what is the use case of shebang I'll explain and Then followed by you will say slash bin slash bash or slash sh or slash ksh what are these things okay so now this is something very important to learn so before I start writing a shell script first of all I need to explain you what is this first line that everybody writes in a shell script so now you go randomly to any shell script okay so let's say I will go to a GitHub okay GitHub shell script examples okay and I'll randomly open any GitHub repository so I just opened a GitHub repository here and let me increase the font for you and let me open this script folder and there is a simple shell script here called addition.sh and what is the first line that this person wrote shebang user bin EnV Bash so everybody uses the shebang and writes the specific syntax or effect.sh randomly you open any of these files okay so the first line would be using the shebang and the shell that they are using now what is the requirement why you have to do that okay so let me explain you this with a detailed uh I mean what is the history of writing these things so let me explain okay so you are seeing my board right now let me erase all this content here uh my band what is happening yeah so why would somebody write a she Bank inside every shell script okay so what is the purpose of writing C Bang followed by slash bin followed by any specific thing here so somebody is using Bash somebody is using Dash somebody is using sh somebody is using ksh what are these things okay so basically if you follow the history of Linux so these are the different executables of your shell script okay so a Linux machine depending upon kind of the Linux machine that you are using these are the different executables so whenever you are writing a shell script and you are executing the shell script somebody has to take your action right so let's say you are running a Java a Java file or you're running a Java program who is executing this Java program who is executing this Python program similarly there has to be an executable who is executing this shell script as well so these are the executables what are they if I have to repeat one more time there are there is badge there is ksh there is sh and similarly there is also something called as t a s h Dash so these are the different options there are other options as well if you are using ax there is different things so I'm not going into details of it but these are some of the most used things so these are the different options that are available so you have to specify you have to inform your Linux or your kernel that okay so this is the executable that I want to use for executing my shell script so what is the difference between each of them so each of them have their own syntax differences so there is there is not a much difference so there is not a drastic difference between bias and ksh and dsh so more or less the kind of cell script that you're writing or the kind of commands that you're writing is similar but they vary slightly in terms of syntax so be very careful on what you want to use what so now you might ask okay Abhishek don't confuse what is the thing that I have to use so one of the popularly used or one of the most widely used is Bash okay so bash is something that is most widely used so instead of learning all of them because there is kind of syntax difference for example the way you write a shelf uh for Loop in bash and the way you write it for Loop in case it is totally different okay so uh bash provides you more easy way of writing a for Loop whereas with case h it is slightly different and because it is obsolete you don't have to learn it what is absolute mean like nobody is using it or very less people are using ksh so what is the most commonly used is Bash now one more thing that you find here is if you look at your program uh like if you look at the shell script that are written in your organization or if you randomly look at cell script uh that are used that are provided in the GitHub so you widely see opinion between these two things so most of the people are writing he bang followed by slash bin slash sh and some other people are using shebang slash bin slash bash so what is the difference between them what is the difference between sh and Bash now previously I explained you what is the difference between ksh bash and uh also the other things like Dash or Dash but what is the reason why people are using slash sh also for using the Bas scripting even though people are using back scripting they sometimes use sh so previously okay if you if you again go back to the history of Linux so slash bin slash sh is something that redirects your like even though they are providing slash bin slash sh so there is a concept in Linux called linking so there is soft link and hardling which we will not cover in this video but there is a option option called linking so using linking even if you are providing this one this previously it was redirecting to slash bin slash basets that means so even though you are providing this the request is taken by sh but it is forwarded to bash okay so it is using the concept of linking okay so don't worry if you see sh or bsh in your previous scripts the scripts that were written uh previously in your organization so uh if you see Slash bin sh and uh if you think that it is executing bash that is because of the linking concept that is provided but but but these days like over couple of years I can say or like just uh previously in a year what happened is some of the operating systems like Ubuntu they have started using I mean they have started linking Dash slash Win slash sh two slash bin slash Dash so instead of bash they started using Dash as the default now why they are using it is out of scope of this video but you have to understand that now you cannot use this syntax so you have to be very careful if you are writing a back scripting always use slash bin slash bash okay so do not complicate the script or if you uh like you know if you start writing this one your script might might not work in some cases like uh in a Ubuntu machine like you share your cell script you write a bash scripting and you share your scale scripting to one of your colleagues and inside their machine slash bin slash sh if by default it has set a link to slash bin slash Dash your script will not work okay so that's why you have to be very careful so always the linking for slash bin slash sh is not pointed to slash bin slash Bash so previously it was happening but now some operating systems have decided that they want to use Dash by default and not bash by default so be careful whenever you are writing a new shell script always use the proper syntax that is slash bin slash bash so this is your interview question as well so what is the difference between slash bin slash bash and what is sorry slash bin slash sh and slash bin slash Dash so if somebody is asking what is the difference you have to clearly explain that okay previously both of them were same because slash bin slash sh was redirecting using the linking concept to slash bin slash bash but now it is not the same because some of the operating systems have decided to use Dash as default so your ship your script might not execute if you are writing in bash scripting uh on a machine where Dash is default okay now we have discussed a lot about these uh the difference and we totally understood what is the first line here that is slash Win slash Bash because we are in because we are learning bad scripting we'll always use the same first line now I'll try to keep the script very simple and the purpose of this script is to just print my name now how do I print my name so the requirement is you have to write a shell script what that shell script has to do is whenever somebody is executing the shell script it has to print my name is Abhishek so every time people can't write right every time like you say 100 people have to write this thing so you are given an automation that write a small shell script that will enter I mean whenever you execute the cell script it has to print my name is Abhishek so to print something if you are on Windows what you will do is you will simply go to your keyboard and you will start writing saying that my name is Abhishek but on Linux it is different whenever you are using the Shell scripting there is a command called Echo using Echo Oracle what you'll do is you will write my name is Abhishek so if you are using Java you might be using print statement right similarly in cell scripting we use the echo statement that's it now let me save this file now how do I save this file so this is one of the challenges that a fresh or a new Linux users might face so if you are very new to Linux first of all you have to get used to the Linux environment so how do you open a file how do you close a file like I told you to open a file I just used Vim command but let's say I just opened this file okay let me go back and I use the Vim command and I just opened this file by default if I enter my keyboard okay so the first thing that I have to do is come to the insert mode okay if you see here I'm I'm in the inset mode what is this thing why is it so complicated complicated so okay so if you're using a Linux machine or a Linux environment so there are set of things that you have to follow that once you open this file the Linux has to know that what what is the purpose of opening this file are you trying to read this file are you trying to write inside this file or you are trying to do like just copy some content from this file and paste it somewhere so for that purpose you have to inform that I just want to open this file and write something so for that you have to use the SK button and press the I okay once you press I it says that okay now we are in the insert mode so what I have to do so whenever I'm opening a file I just have to open the file press escape and click on I so once you click on I so it goes to the insert mode how do you know if you're insert mode so to your left bottom you will see something thing called insert so you are now in the insert mode now once I am insert mode I can start writing like the one that I just wrote slash bin slash okay so in a root I just wrote something into this shell script and now I want to save this file to save this file again you have to press Escape okay followed by colon you just have to give the colon and if you see here carefully notice what I'm doing here then press W Cube WQ is for saving the file and followed by the exclamatory Mark and then press enter so now your file is saved with this content what happens if I don't do all of those things here whatever you wrote onto the file will not be saved okay so let's say now I also have one more file here right so second so I just opened this one let's go to the escape and press insert mode and now I'll start writing something here slash bin slash bash and I'll say Echo I'll just say hi okay instead of Escape colon WQ what I'll just say is just Q okay and I'll press the exclamator mark what happens here is if I reopen this file there will be nothing inside this file now why there is nothing because I did not use WQ but I used Q is just like quit so you are not writing anything inside the file and you are just quitting the file so if you want to save the file what you have to do you have to use colon WQ exclamator mark so this is the thing so this is a view save a file so we learned how to open a file we learned how to write a file and we also learned how to save a file so these are the different things now you wrote this file what is inside this file let me again see so every time I can't use the Vim command and always close it right so there has to be easy option so every time I open this file I have to press Escape call and queue close I don't want to do all of these things so simply I just want to copy this file name and I want to look at the contents of the file okay so for that we can use the cat command what is cat command cat command is used to print the content of the file foreign did you see so now I don't have to open the file and close the file I can simply without opening and closing the file print the contents of the file so that is the purpose of using CAD command okay so we also learned about the cat command now how do I execute the script okay now I spoke a lot of things like open the file close the file without opening the file print the file but I want to see how to execute the file itself so for that what I have to do is either I can say sh sh for you can simply suffix with SH and enter your file name okay I can also do this or I can simply use dot slash okay so two options so dot slash can be used to execute any file whether you have a python script also you can use dot slash basically whenever you have an executable so shell script this is an executable file right you can execute this file so any executable file in Linux can be executed using dot slash if I don't want to use dot slash and if I want to definitely mention it as a shell script so I can simply say sh followed by this thing okay so two options either use sh as Civics suffix or either use dot slash ssfx whatever you would like to so as soon as I press enter this one this has to be executed but it says permission denied okay so this is one other New Concept that you will learn in Linux okay so what happens in Linux is let me go back and explain you here okay so in Linux although you are the creator of the file okay you just created the file but Linux terminal want to understand whenever you create a shell script or any file Linux terminal wants to understand okay who who can execute this file okay you just created the file you wrote something in the file but Linux the main purpose is security okay so Linux is very very secure so as soon as you open a file you write something in the file okay you create this file what Linux says is as soon as you save this file and you try to execute this file using dot slash or sh index is okay wait look before I execute this one I need to know who can execute or it asks who are you do you have permissions only if you have permissions I can allow you to execute this file so for that whenever you create a file you have to Grant permissions to your file so this is a New Concept that you are learning and this concept is not straightforward because in Linux granting permissions is not straightforward okay on Windows machine even in Windows you can do it like you know you can right click and you can say who which user has access which group has access but like into like I mentioned here most of your Linux missions does not have a graphical user interface okay you cannot uh directly use your cursor or something because you're when you're working on an organization you don't install a GUI on all of your machines so for that what you will do is you will only use the command line and using command line the command that we are using here is watch carefully this command so this command is not complicated but you need to understand the details of this command okay chmod so CH mod is the command that grants permissions grants permissions to a file okay now what is this command I'll explain you in a very simple terms just divide this into two parts one is ch and one is mod okay so CH stands for change so what you are basically doing is you are changing the permissions of a specific file using the chmod command okay so just use chmod followed by any file name and it should change the permissions of the file but what are the permissions so you have to provide the permissions right so in Linux you cannot write saying that like you know chmod provide right permissions to this file so that is how not that is not how you will do so Linux has a specific mechanism okay so what are the three things that are required for the chmod command I'll explain in a very simple terms okay so CH mode first you understood what is chmod Command right now we will try to learn about the arguments that you will pass to ACH mod command so chmod command basically like I mentioned you it provides permissions okay so what it is divided into three categories foreign so the first category is which user has access which group has access to a file and what are your permissions okay so the first is what sorry here not this is not which user what are the permissions for a root user okay so root user is a super user in Linux I'll explain you what is a root user but first category is what are the permissions for the administrator user second is what are the permissions for the group third is what are the permissions for you okay so these are how the three categories are divided because administrator should have access to a file a group should have access to a file and personally you should also have a access to your file because you are the creator of the file or you are the one who is using this file so by default if I log in I'll be the user I am trying to access this file so the last one defines what is your permissions which group and firstly the administrator okay so this is how you define permissions for a specific file now so this is for the all users what is the permission for all user what is the permissions for your group and this one what is the permissions for you so this is how you define and in Linux what you'll do is you will try to categorize this one okay so using the numbers so using numbers you will actually Grant permissions for the file so how do you do that so by default if you want to Grant access to everybody you can simply say chmod 777 followed by the name of the file so now if I try to use the file gets executed and it printed whatever you wrote in the shell script okay so now chmod command if you try to learn about this file you can simply use man chmod there is lot of information that is provided but in a simple terms if you want to learn about chmod like I mentioned here chmod grants you access for three different things what is the permissions for all users what is the permission for your group what is the permissions for you so these are the three different things that chmod grants so now how do you define you define using the number terminology okay so what is the number that I just used and how this number is categorized Okay so 7 is your magic number okay so if you see carefully I granted seven seven so I said seven for myself seven for my group and seven for everyone so literally everybody is logging to this machine has the number seven as the access permission granted uh my group also has seven and myself I also have seven now what is this 777 so Linux basically used a formula called four two one what is 4 what is 2 what is one okay so four is for read 2 is for right one is for execute okay so you can basically provide your permissions using this let's say I will say that one file uh like I'll use this command c h mod 444 XYZ XYZ is the file name okay so here the permissions that I have granted is 444 what is 444 read read read so indirectly I'm specifying that okay uh we will try to uh write very carefully here so that everybody will understand okay so like I mentioned user group everyone so here user means owner group means owner who is the owner Whoever has created the file okay so by default I can create a file and again also not give permissions to myself and group is the group of the owner of this file everybody is literally everybody who is logged into this file so whenever I am using chmod 444 XYZ is the name of the file what happens is like I mentioned you the formula is 7 what does 7 mean 4 2 1 what is 4 4 stands for read 2 stands for right one stands for execute okay now using this formula if I am saying 444 that means read read read so as a user as the owner of the file you will also get read permissions as a group you will also I mean the entire group your group will get read permissions and everybody will also get read permissions so that means nobody can actually write this file so for example let me do that chmod 444 shell script file now I change the permissions of this file to 444 now let me try to edit this file okay so because we just learned that 444 will only give you read access and if I start writing something and let me save this file let's see what happens okay let me open this file oh sorry what did I mess up here sorry just give me a second here I think I okay so now if I uh try to execute this file using the same command uh followed by I mean dot slash and if I execute it says permission denied okay this is because I change the permissions of the file so if I do it back like chmod 777 first shell script.sh now let me try to use the command I one more time okay so now it got executed okay however it said that there is a syntax error in the file but the file got executed so that is the purpose of using the chmod command Okay so now let us take a pause and see what are the commands that we have learned okay now that we have learned a lot of commands we need to just summarize what are the different commands that we learned so now Linux provides you a very good option that is called history so history is a another command if you click if you just type history and press enter it will show you all the commands that you have entered so let's say you forgot a specific command that you are using on your production machine like a regular command that you use or a regular shell script that you use so you can simply use the history command to see what are the different commands that are entered so these are the different things that we have learned till now okay now let's take a pause and let's try to summarize what are the different things that we learned just okay so let me write this up so isn't uh shell scripting losing uh looking simple like what is the only thing that you need to do is you have to practice these things every day okay as long as you don't practice then it is very difficult to learn so now let's start with it firstly what is the command that we started with or the basic comment that we looked at the First Command that we looked at is of to open a file so for opening a file there are two things firstly you have to create a file so either you can create a file using touch command okay or directly you can create an open file using Vim command so now what is difference between touch and whim so touch will only create the command whereas boom will create and also open so what is the advantage of using touch so touch is basically used in automations so whenever you want to create hundreds or thousand files you cannot use the Vim command because it creates and also opens so that's why there are two different commands now I don't want to open the file but I want to read the contents of the file so in such cases you can use the cat command so cat command can be used to print the contents of the file again what is the command to list all the files in the directory you can use the ls command so LS stands for list so you can use you can list all the files or all the folders in a directory using list command so for that you use the ls command and after that what is another command that we learned we learned about the CH mod what does CH mod do chmod provides you the permissions or if you want to Grant permissions to any file you can use chmod and after that we also learned about the man command so what is man command man is basically a manual command so if you suffix any command using man that will show you the details of the command like you can simply say man LS so what happens is it will provide you the details of the ls command all the documentation for the ls command and we also learned about other commands what is the other command that we learned am I missing something here okay so we also learned how to write inside a shell script right or how to write inside a file like for that you also YouTube like you cannot simply go to the vim and uh it keeps on writing right so for that firstly you have to use the SK button and using Escape you have to press I once you press Escape press I so that it goes to the insert mode once it goes to the insert mode you can start writing inside the file and after that if you want to save the file you can simply use colon WQ which will save the file or if you want to Simply quit the file and not do anything you can simply use colon queue so these are the different things that we also learned so these are the very basic commands Okay so using this basic commands now you have the capability of creating the files reading the files you can list the files onto your uh what are the files that are pro that are available on your system okay so now let's look at the history if we missed anything right any other command that we learned and we forgot to discuss here okay that pretty much seems to be all the commands that we executed and all the commands that we learned now let's learn about a new command so if you're on a Windows okay how you will understand that which folder you are on okay so you can simply use your cursor to understand which folder you are on but in Linux like I said we most of the times use the command line interface so for listing the current working directory or the present working directory in which directory you are you can simply use the PWD command Okay so PWD says that okay this is your path so in this path you are currently in and that's how you identify what is your present working directory so let's say you want to go One Directory inside a specific directory okay so you can use PWD to understand in which folder you are and once you identify which folder you can go to different folders so what is the other command that we learned just now that is PWD so PWD is very simple and straightforward comment so now I explained you how to create files but I also want to explain you how to create folders okay so creation of files and folders in in Windows machine is same just right click and it creates a file or using new folder you can also create a folder like you can use new file or new folder but here in Linux we have one more command that is called mkdir so using mkdir which is make directory mkdr stands for make directory just say mkdir my first folder okay now I want to see if the folder is created or not like I mentioned you LS command minus LTR will list what are the different files and folders with timestamp so if you see what is the latest thing my first folder so I created the my first folder now I want to go inside this folder okay so I am currently in the Shell scripting tutorials folder but now I want to go to my first folder so like if we compare to windows again you double click on this specific folder and you go inside the folder but in Linux you use the CD command so CD is change directory so using change directory you can go to the any directory that you want okay now if you see the folder got changed from Shell scripting tutorial it got changed to my first folder if you don't have this user representation how do you identify which folder you are in you can simply use the PWD so here PWD says that I am currently in my first folder okay if I go back using CD dot dot and I use PWD now it says that okay you are in the Shell scripting tutorials folder so that's how you understand using the PWD and using CD you switch the directory or change the directory and using mkdr you create a directory okay so now let's go back and summarize what are the different commands here apart from this commands I just explained you about three different commands first is PWD what is PWD PWD is to identify the present working directory where you are correctly on your Linux machine your Linux machine have hundreds and lakhs or thousands of folders but you want to know currently where you are so for that you use the PWD command you can also use these commands in the Shell scripting so that's why I'm talking about all of these commands so PWD is used to identify the present working directory okay so talked about all of these things I want to create a folder so for that I use the MK TIR command so mkdr is used to make a directory or create a directory once I create directories I can go into the folders or directories using the CD command what does CD stands for C stands for change d stands for directory so using CD you can change any directory okay so within matter of uh some 30 minutes we learned about 10 commands Okay so it is very very very easy just that you need to have a Hands-On and can you can keep continue I mean you can continue learning different commands or you can continue learning shell scripting in Linux okay so we know we learned about different like 10 different commands now let's try to put all of these things for most of these things and write a simple shell script okay so I'll not complicate this one if I do LS let me go back let me go to the my first folder okay and inside this folder if I do LS one more time there is nothing here okay now I'll simply write a shell script to create a folder to create a file so every time this shell script executes it has to create a file it has to create a folder and inside this file you know we'll also change the permissions of this file and finally you know we'll see if the uh everything got executed or not using the cell script so as usual we will use the fim command to write the shell script and also open the shell script because our purpose now is not just to use the touch command because if I'm using the touch command here I have to use two commands one is to create another is to open the file so here I'll use the Vim command and I'll say sample shell script dot sh okay and inside this file what I'll do is as usually I'll start writing using shebang followed by slash bin followed by slash bash and now I'll write here the first thing okay now whenever you are writing a cell script it is always a good practice to write comments not just shell script but any any any programming language you have to write comments because it you only you should not understand what you are writing but any person who does not know the programming or if they want to glance through your script at a time like you know they cannot read the script in detail like they are in a hurry and they just want to look at the sales script and understand what you have wrote so for such things you just write a simple comment so how do you write comments in shell script use the #so hashtag references comments so whenever you write something followed by hashtag this line is ignored so shell script will not execute this command who is the shell script so this bash whenever it finds hashtag followed by something it simply ignores it understands that okay this is a comment I don't want to execute so it simply ignores and proceeds this as a commander so what I'll say here is create a file what is the command to create a file I can use simply the touch and using touch I'll say uh first file for example and I'll also create second file let me create two files okay so create two files just for just for an example okay before that what I'll do is I'll create a folder as well okay so create a folder now I'll name this folder as Abhishek for example so what is the purpose is I want to go inside this folder using CD Abhishek and I want to create two files so this is my shell script that's it so whenever somebody is executing this shell script it has to create a folder called Abhishek it has to create it has to go to the folder called Abhishek and it has to create two files now let me save this as soon as I save this what is the thing that I have to do I have to give permissions to this okay because even though I'm a user I cannot execute this one now we are not much bothered about security so I'll simply say chmod 777 okay so I'm granting complete permissions to myself the owner of the file complete permissions to the group which which is my group I did not create any User Group here so if at all I am creating a user group and I'm adding my colleague as a user in my group let's say I'm in development team or devops team so you can add people to devops group and what happens is Linux will grant devops everybody in the devops group read write execute access and finally anyone not just me or not just my group but anybody who is logging to this Linux machine and if they are trying to execute so I'll say Simply Seven so anybody can access this file okay so things are clear till here here I have done is I have just granted permissions to everyone sorry my bad so CH mod 7 sorry 777 what is the name of the cell script I totally forward so let me just say LS okay using LS if ever if at all I am forgetting the name of the file or if at all I am forgetting anything I can simply say LS to list all the files and folders in this specific directory okay okay I think there was a disturbance in my desktop so okay now everything seems to be back so as soon as I press LS so this is the name of the file now I'll say chmod 777 if I don't want to Grant permissions to all the other people I can simply say seven seven zero so that they cannot read this file they cannot write this file they cannot open this file they can do literally nothing with this file but if I only want them to Grant access and everybody who is logging to this Linux machine let's say that they only want to execute this I only want to give them the permissions to execute not read or not write they cannot read the script they cannot write or make any changes to the script but they can only execute the script so in the in such case I'll give one one stands for execute like I mentioned the formula is four two one four two one is the formula so 4 stands for read 2 stands for right and one stands for execute so if I have to Grant all the permissions 4 plus 2 plus 1 which is 7. okay sample shell script dot sh okay perfect now let me execute this file sample cell script.sh let's see what are the things that are happening okay it executed it did not print anything okay now let me press LS what happened this is the script that is there but it also create a folder called Abhishek now let me see what is inside folder Abhishek so let me go to the folder Abhishek and let me press enter LS okay it created first file and second file that is your task and which is accomplished okay so this is how you can simply write a shell script is it clear is it not so easy right so this is how simply you can write shell scripts so it is as simple as this thing so within one hour now you learned how to write a sales script what are the different things and what are the like you know what are the how do I put that so what are the different components that are involved in writing a shell script so the only thing that I would like to summarize here is shell scripting is very very easy oh okay so the screen sharing got skipped my bad okay so for some reason because there was a power cut the screen sharing got uh cut so let me uh re-explain this thing here no worries I can explain this one more time so what I'll do is I'll go back here and uh let me go back to the folder called my first folder okay so I think you were able to see my screen till here but now I'll re-explain whatever I explained so this is the shell script that we just wrote sample shell script let me delete this folder so that you will not be confused so for deleting we can simply use RM minus RF I'll explain about the RM command as well but now if I LS this is the file that I just created which is the shell scripting file and now what I'll do is I want to execute this file so let me execute this shell script as soon as I execute what happens is the script got executed and it created a folder why the folder is created because we use the command in the shell script for creating the folder okay so if I open this file one more time to explain what is the things that I wrote so firstly we created a folder using mkd act so that's where a folder is created now if I go and see inside the folder there should be two files called first file and second file let us see if the files are also there so CD Abhishek and if I LS yes first file second file both of them are available so this is the shell script that we just wrote isn't it so simple it created a folder it created files so similarly whatever the automation that you are doing inside your uh office so these are the different kinds of activities that you will get so this is a very basic script that I wrote but you can complicate the script now without wasting your time I'll try to explain what is the role of shell scripting inside a organization so we will focus mostly about devops right so because we are talking mostly about devops Let Me Explain you the scope or role of shell scripting inside devops okay so I hope you are seeing the right screen let me just check if you are seeing the right screen yes I think you are seeing the right screen only perfect let me erase this one here okay let me refresh the board okay let's just pause this video and see what is going on okay so there was some issue with my screen but now it seems to be perfect okay so what is the uh purpose of cell scripting in devops that is what we wanted to discuss right so okay so let's try to understand this one so devops no as a devops Engineers so you have different activities like you have infra maintenance right you maintain all the infrastructure of your organization you maintain all the code of your organization using git repositories okay so sometimes you have to interact with Git so again to interact with Git you use the Linux only right like you can ask me that why cannot why can't I do it using Windows but most of the organization prefers to use Linux because of the security and lightweight nature of Linux so you might use Linux windows on your laptop but if you are using any virtual machine so it will most probably be a Linux machine so you also manage your code using git repositories and that is also mostly on Linux and apart from this you also do a lot of configuration management so for all of these purposes on a day-to-day basis you use shell scripting okay so infrastructure automation is there configuration management and also your code management using git repositories so you can say that there are different tools like ansible that are available here but even though these things are available you have to write some shell scripts for regular automation of cron jobs or you know for uh switching into the folders where the sensible scripts are available or directly executing uh that like you know directly log into this machine uh on a single automation what a user expects is you have to log into a specific machine where ansible automation is present and you have to execute the ansible automation as well or you want to monitor the health of your nodes so let me give you a very simple example so let's say there is a person called a devopsy I mean he's a devops engineer and let's call the name of this person as John so now what is the responsibility of John let's say John so John is working as a devops engineer in or it Amazon this is just for an example okay he's working as a devops engineer for Amazon and he has noticed that Amazon has close to 10 000 virtual machines this is just for an example okay or the teams that he is working with has 10 000 VMS what are VMS virtual missions and all of these VMS are Linux based VMS okay love for some reason he has to manage all of this 10 000 virtual machines and he has to monitor the node health of all of these virtual machines okay so every time he has to go to these virtual machines and uh their developers are facing some troubles with this node that they say that okay this virtual machine or any of these virtual machines is not performing as expected they say that the CPU is going out CPU is getting completed or they say that memory is running out or they also mentioned that you know the Linux is uh becoming very slow or they say that whatever the processes that are running on these virtual machines are basically running slow so every time John goes to these machines and uses the Linux commands to fix all of these things what he will instead do is he will make use of shell scripting okay so he will write one simple cell script and he'll save the cell script in a git repository and whenever some devops engineer says that okay there is issue with 9999 VM what he'll do is using the shell script he will just execute the shell script on his local machine and what the cell script does is it logs into this one of these he mentioned that 9999 mission is not working so this shell script automatically logs into this machine and it looks into all of these parameters like CPU memory and why are the processes going slow which files are running how many files are open on this machine so he writes a shell script and this shell script returns an output saying that okay so I executed this script and you know what I noticed is the memory is completely used on one of these nodes okay so it returns whenever he runs the shell script it says it evaluated all these parameters and it says on 9999th machine he said that the memory is going out of place so the memory is completely used or instead of these people raising what John thought is okay okay I'm getting all of these requests like every time I'm getting requests from the developer saying that the memory is completely used so now let me uh do a simple thing that on a frequent basis like once every day or once every two days he decides to basically execute this script automatically okay so without even his consent this script should get executed and this script should look at all of this 10 000 VMS okay like I mentioned there are 10 000 VMS what this script should do is this script should log into each of these 10 000 machines look at the status of each of these 10 000 machines and it has to send out an email notification saying that hey John I evaluated all your 10 000 machines and out of which 10 machine seems to be suspicious okay 10 Mission seems to be suspicious and in five machines there is issue with memory and in five machines there is issue with CPU so this is one of the use case for devops engineers so similarly there are multiple use cases for devops engineers with shell scripting and shell scripting is a must learn for devops engineers okay like even in the roadmap video so I made a roadmap video I think all of you watched that video so roadmap for devops engineer for 2023 so devops roadmap shell scripting and Linux is one of the most ones so if You Learn Python that is even more useful for complicated automations where cell script cannot be executed so python is also a good thing to learn but before to python you should definitely learn shell scripting so this is one of the use case for Shell scripting in devops engineer's life so whenever somebody is asking why are you using devops cell scripting so you can say this example that I just mentioned you can say that I have automated all the uh node health of my virtual machines so we are we have some close to thousand virtual machines and every time it is difficult to monitor the uh node health or status of this virtual machine so I've decided to write a script so people might ask you okay there are some automated tools why you want to write this shell scripting so you can say that you know uh in our organization we are not using such tools or you can simply say that you know these tools are restricted for generating some parameters only a restricted number of parameters but in my script I am fetching more parameters that are not provided by these tools so you can mention in that way so this is a basic use case you can think of multiple use cases like I mentioned infrastructure automation configuration management or the example that I just mentioned or your day-to-day basis activities like you know on your day-to-day basis you might want to monitor uh some specific tools and send notifications uh to email I mean email notifications to you or any any such things you can do with shell scripting okay so now what we what did we do today we learned about what is the reason for Shell what is the reason for Shell scripting we learned the difference we learned an interview question that is what is the difference between pin sh and bin bash so this is something that we learned and after that we learned the shell commands so if you want to write cell scripting what are the components that are involved you will write shell commands so we looked into different cell commands we looked into listing files we looked at creating files we looked at removing file using RM we created directories using mkdir we deleted directories using RM minus RF okay and we use the man command we use the present directing directory command we also used you know commands like chmod to Grant permissions okay we learned how to write inside a file so we learned about a lot of things today now finally I also want to explain you about something like I just mentioned uh you know you can look at the memory inside a machine so to monitor the node Health like I mentioned you can write a simple script so for that to monitor the node health you can monitor the CPU as well as RAM so these are the most important things even in your laptop if you are using over using your laptop so sometimes you get error with CPU sometimes you can error get you get errors with ram so how do you find out CPU and how do you find out Ram so for CPU there is a command called n proc so nproc will list the CPUs on your machine and to understand what is the memory that is present on your laptop you can use the free Command okay so using free Command you can identify what is the free memory what is the total memory what is the used memory okay now instead of using all of these things you can use a specific command or a special command called top okay so using top command you can basically identify what are the processes that are running on your machine okay so which process is taking more memory which process is taking less memory so that's what I'm showing you here now okay so it says total 625 processor running on my machine uh there are 625 processes out of which fire running 622 are sleeping okay and it also provides you information about which prep which process is using how much amount of memory which process is uh running with with process ID all the other things like how much CPU is piecing used the CPU usage memory usage all the other things are provided using the top command okay so what is the command top so these are some of the interview questions that people will ask you so how do you monitor the node Health you can simply say I can use the top command or I can write the custom shell scripts so what are the different parameters that are used to evaluate the node Health some basic parameters are CPU and RAM and all the other commands that we learned today so now with without going into the depth of other things like you know how do you trap signals or so there are Advanced topics in Shell scripting like how do you trap a signal so for example if I want to stop a executing cell script so I can simply press Ctrl C using Ctrl C I can close the executing cell script so what is happening is you are sending linux's signal so now how do you trap the signal so even if somebody is pressing Ctrl C the script should not be stopped so these are some of the things which are the advanced topics so if you feel that this video is useful to you if you found this video useful and if you want me to continue and make a video on the advanced shell script Concepts so simply comment on the video saying that please do an advanced video on the shell scripting so that I can go ahead and make the advanced video as well okay so your feedback is very important to me because uh in this video I just explained you the basic and intermediate level Concepts and what is the use of shell script some interview questions what is the use case of shell script in a devops engineered life but if you want to go to a step ahead and if you want to learn about the advanced concepts so again that will take close to 1 hour where we can learn the advanced concepts like we you know instead of using this top commands we'll go ahead and look at how do we write some custom shell scripts we'll also look at how do we uh interrupt signals by default Linux signals like just Dimension using the Trap command or all the other things like how do we write cron jobs so if you want to learn about such things uh do comment on the video and like every time please like the video comment on the video share this video with your friends who find this useful or you know who is looking for such videos on shell scripting or devops and finally don't forget to subscribe my channel because that gives me a motivation to do many such videos and I'll continue doing this videos thank you so much for all the love and thank you so much for all the support so I'll see you next time in a new video catch you next time bye everyone take care