Shell Scripting - Functions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Laughter] hey guys hackersploit here back again with another video and welcome back to the shell scripting series or the course and apologies for not uploading in this series and uploading in general i've been working on you know improving everything from the website to the forum a lot of you are complaining that your questions were not being posted so i've sorted all of that out and it should all be good to go now i hope to complete this series and also the web application penetration testing series so those are the two series that are currently underway after them we can then move on to the exploit development and the ctf series will be continuing on a weekly basis as well that being said in this video we're going to be looking at functions in shell scripting and again part of one of the most important sections of this whole uh of this whole series and in programming in general all right so you might be asking yourself well what exactly is a function well a function is simply a piece of code or a section of code that performs a specific task all right now again you might be saying well why do we even use functions well we use functions to help reduce repetition and uh when i say this what i mean is you can use a function and you can use it as many times as you want now the great thing about functions is that they can perform a specific task and you can use that function to call on that specific task at any time or at any position in the in the program itself all right and we also use functions to sort out our code and to sort of maintain our code in the sense that you can have a piece of code that for example prints out specific data you might want to sort that data out in a function so that it is easy for you to debug and troubleshoot in case the program has a few errors all right so it helps you maintain code now the workflow or the process of a function is very simple in terms of how it's executed the code is you essentially call you declare a function uh and then you simply you you after declaring the function you start typing in your commands uh whatever you want to run whatever you know functions you want the uh what whatever tasks you want the function to complete and after that you declare the function and you you call on you call the function and finally uh the function runs and then and then it returns a return code or an exit status and i'll explain all of this in a few seconds now in shell scripting there are two ways one can go about creating a function all right the first way is really very simple and the second one is extremely simple and it all comes down to how how you want your functions to look and what coding uh practice you you you carry forward all right so let's get started so uh let's say i wanted to create a simple script that would test the directories to test uh if certain directories exist for example the i wanted to test if the shadow file exists and the password file exists on um on kali linux on my current working working environment so to do this it's really very simple all we need to do is we need to just uh we you know make sure you've declared your interpreter here using the shebang and we can get sorted so the first way of declaring a function uh is very very simple and that is simply by typing in the word function quite simple right the syntax after this proclaims that you use the function name all right so in here you can create you can give your function a name and then you open two parentheses and your curly braces these curly braces is where all the code that you want to be run in this function will be put all right so that is how to create that that is the first way of creating a function now in here is where you put all your code right so let me just mark that so you guys understand what's going on the second way of calling a function is uh very very similar but in this case you do not declare the word function all you do is you type in a function we can say function maybe function one and you specify the parenthesis again and your curly braces like so all right now again it's very the syntax remains the same for everything else uh now when i when it came to me doing research about this and which one was the best way of doing it it really doesn't matter which one you choose it all depends on how you want your code to look and how easy it is for you to interpret or go through it in case you ever need to so it's more of a sorting uh it's more of your own personal preference here okay so i usually like having the function declaration because it helps me understand what exactly is going on here that's just personal for me so uh when when it comes down to creating a function and a working function as i said you have your code and then after the code uh you need to call upon the function which means you want to use that function because if you do not if you do not call on the function the function will not be executed and any of the code within it will not be executed all right so that is the rule so functions have to be declared before they can be called all right so remember the two rules is you need to call on a function to use it and the declaration of the function has to be before it's called now how do i call upon a function well if i wanted to call upon this function i would simply get out of the function and type in function name all right without the without the parentheses and that essentially would mean that uh this function right here would be executed when this program is run okay but the incorrect way of doing it is going to the beginning before the function is declared and typing in a function name all right so function name that would be wrong this program would not execute because the the interpreter is going to look at this it's going to essentially understand that this is a function but remember the function has not been declared before it so it's going to go to the next line and it will not essentially call upon that function because uh it is interpreted in a batch type manner okay so we are going to be creating a very very simple script that will again test uh for different files whether the different files exist in this case we're testing if the shadow and the password file exists so we're going to be using some test functions here with some conditional statements all right so what i'm going to do is i'm going to start off by creating our first function so i'm going to say function test and we'll call this shadow test shadow i'm not going to put anything in the parenthesis that's something we'll be taking a look at in the next set of videos when we talk about positional parameters uh so in here i i want to test if a certain uh if a certain file exists so i can say if uh we are testing if the shadow file exists right so uh if shadow uh so the directory is etsy shadow um if shadow exists then oops sorry if shadow exists we then want to say then and we say we want to print out yes it it exists you can also uh by the way for those of you asking me if you want if you can list the files yes you can you can also grab the files if you want to so you can for example uh you can list all the files in that directory as a command and then you can also grep for a certain uh user if that's what you want like the root user so that it only displays that if the file exists but i don't want to complicate the program so again echo yes um we can just say it it exists all right uh exists like so sorry about that and finally to end the uh wait actually if the file doesn't exist then that means uh we also need a statement for that so the file uh the file does does not exist and we leave that like that and then finally we end this if statement by typing in fi to close it and that is one function all right so if i wanted to call this function what will happen is i need to test uh outside the function i need to call on the function so i would say test shadow all right the name of the function now if i don't do this something interesting is going to happen this this this function right here and this code within it will not be executed now i can show you that right now so i'm going to just save the file as it is and i'm going to exit and i'm going to launch that shell file called functions.shell and i've already given it execute permissions i type in enter and nothing is displayed because that function and any code within that function was not executed so again if i want to call from that function i need to display it outside so i would type in um the name of the function so i'll say test test shadow like so and i once i hit exit here now let me launch the functions functions.shell file again and voila you can see that the file exists and i made a spilling mistake there let me just correct that really really quickly and now we'll add the second function as well uh the one that will test for the password file so exists like so all right so now i'll uh let us exit out of that function and we can create another function so i'm going to create uh let's see function test password right and we're going to be testing that file whether that file exists and uh yeah within this again the same if statement can be run so let me just copy that uh sorry about that let me just copy this like so and for some reason that did not copy but anyway i think i have uh yeah i did not enable emacs there sorry let me just go back up here and like so all right so let me just get rid of that and we are testing for the password file now so like so so password and again that will do the same thing but again this function will not exist i will not be called or executed unless i call upon it so just below this i'll also call this function so i'll say password like so now the the other interesting thing is that you can also call a function uh you can also call another function within one function now how can this be done or you might be saying well what exactly do you mean well let me save this first and let me execute and show you that indeed does work so you can see for the first one it says it exists and for the second one so both the the shadow and the password file exists not something new that we've talked about now uh we can also call functions within one function so let's say at the end of the test shadow function i wanted to execute the test password function right well to do that all i would have to do is i would have to just type in after that conditional statement test password all right now let me get rid of the the call here which essentially would um which again will uh will essentially call the function for us which we don't want to do because i want to demonstrate uh that that does work indeed so there we are so i called the functions again without calling the functions outside the functions and i called uh the test uh the test password function within the test shadow function and as you can see it it was able to execute so that is how to also call functions within another function so if you just take a look at what we did uh within the first function we executed all this code and then after that we prompted it to execute the test password function and that's done and that is because after the interpreter has executed the test shadow function it then executes every piece of code within that function and within this code it's telling the interpreter okay i want you to also execute the test password function so you can see the versatility of functions are now and how useful they can be all right so that is going to be it for this video guys i really just wanted to cover all the basics of functions and how to use them and now since we know all the basics we can now move on to creating really awesome scripts so with that being said i'll be seeing you in the next video peace [Music] you
Info
Channel: HackerSploit
Views: 46,427
Rating: undefined out of 5
Keywords: hackersploit, hacker exploit, shell scripting, shell scripting for beginners, shell scripting in linux, shell scripting tutorials, shell scripting tutorial edureka, shell scripting windows, shell scripting projects, shell scripting examples, shell scripting tutorial for beginners in bangla, shell scripting crash course, shell scripting tutorial for beginners 1, linux, shell, kali linux, hacking, bash, os, software, bash scripting, bash scripting tutorial
Id: Jx9yhQ76o8U
Channel Id: undefined
Length: 13min 2sec (782 seconds)
Published: Mon Sep 10 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.