hey guys welcome to the next video on shell scripting tutorial for beginners in the last two videos we have seen how to use functions in shell scripting now in this video to consolidate our knowledge which we have gained in the last two videos we are going to take an example and then from this example some more concept will be clearer to you so let's start with the example and then we will discuss about the concepts so what we want is we want to check whether a file exists or not using our script so whenever user runs the script with hand argument and this argument will be a name of the file for example and the script will check whether this file exists or not and it will print now this example we have seen in the last videos also but in this video we will use first of all functions and then we will use some more concept so let's get started so let's say our function name is does v exist or is the file exists right so is file exists and then we will just use these brackets and these curly brackets right right now we want to pass an argument so the name of the file we want to pass as an argument so what we are going to do is we will just declare a local variable we can say it's file write a file name you can name it anything and this is a first argument so dollar 1 is the first argument to this function write this we have learned in the last video now we want to check whether this file exists or not so we can use these double square brackets for that right now in the previous video you also know that minus F flag can be used to check whether the given you know file is a really file or a directory or whatever right so now we have this file you know variable which is a local variable which comes from this argument so we will check whether this file is really a file or not so we will just say dollar file and then this will give us true or false right now notice are not giving any if conditions here right I am going to use the return value so this if you are familiar with some other programming language you may know the ternary operations so we are checking whether one operation is true depending upon the you know result if it's true we will return some value otherwise we will return some other value right so we will just use double and operator and then we will return zero I will explain you what I am writing or I will return one okay so let me explain you what's happening with this line so we already know that this can be used to you know check that the file exists or not right so we will either get our true or false or in the terms of this script we will either get 1 or 0 so when in the condition a file exists we will get 1 right now if you know the program flow or how program works whenever we get 1 in the first condition and whenever there is end here so for example there is end and we have the other condition right or so condition to I will just write Co and to write so whenever they is one right so it's true here and when it's true then program flow is not going to check this one or this will directly jumps to the or condition because we just want to see if this condition is true or not right so in the end condition we will check if it's zero then it goes to the next and operator which is this one so it would be zero then it's going to go to the return zero you know operation because here is zero and then if we are not getting one here then program cannot move forward so it's going to check whether the other condition is true or not and so on right but here we know that first condition is already to true so it's going to directly go to the return one condition right and if it's false for example if it's false then it's going to check the second condition which is return true and it gets the written zero here and then it's going to return zero right so in case of file doesn't exist it will be zero and then it going it's going to check the other part of the end operation if it's one it's already got its true value so it's going to go to the next operation which is in the or operation so first of all we will check whether user has given any argument or not with the shell scripting right so we will once again use these double brackets and then if you remember this dollar hashed which is a built-in variable is going to return us the number of arguments number of arguments which are given with the script so this is going to give us the number of argument and if this number of argument is equal zero then we will say we want to do something or we want to show some message to the user so for this I am going to you know create some usage function for example so I'm going to just write usage here and let's create this usage function here for example right so usage and then we will just use this curly brace brackets and this just simply for example print some message and exists so we are going to print for example so we will just echo this message that you need to provide an argument and we will just give the usage of this script also so we need to provide the name of the script and the file name right and if the file name is supplied and argument is for example more than 0 then we will go forward right and then what we are going to do in the next step we will just check so we will use if condition and then we will call this function but let's first give for example then and we want to use else also here so we will write else and then if I to end the condition so we will call this function a file exists we are going to get the true value and then we will print the message so here we will just give the first arguments or dollar one now be eye in mind this is the first argument of this function and this is different this is the first argument which is provided to the script right this is the first argument because it's inside a function this is the first argument provided to the function right and this is the first argument which is provided to the script right so these are two declaration of dollar one are different okay and then if pile exists we will just echo that while is found or file found otherwise we will just print file not found right so let's print file not found okay now let's run the script let's first do Alice here list all the files which are there in my desktop directory so this there is only one file which is the script itself right so let's create one file so I am going to just touch for example file 1 dot txt here okay and then I am going to press ENTER and then I am going to to LS and now I have a file name called file 1 dot txt so I can just run by this my script and then first of all let's not give any argument here we know that we need to give a file name as an argument with the script but let's provide our wrong argument or no argument here and then just press ENTER and then it says you need to provide an argument usage is the script name and then the file name and I also see this message that local command not found because it's spelled wrong so it should be locally not lack law local right so this keyword should be local right so now next attempt we will give the name of the file and then press Enter and now we will see file found so what we have learned from this video from this example two things we have learnt we have already learned how to use functions but we learned that this dollar one and this taller one are different this is for the argument which is provided with this one right with the calling of your scrip and this is the argument which is provided to the function right and we have learned how we can use and and/or operation without the if conditions right you could have used this in different conditions also so you could have you know written if this is this and then return 0 or 1 this could be also done but it can take more number of lines right so if you are not comfortable with this code then you can always switch to if conditions like normal life conditions and then also you can you know get your result ok so I hope you've enjoyed this video please rate comment subscribe and bye for now
Video Description: Shell Scripting Tutorial for Beginners 25 - Function Example for Software Development 2023 is part of Complete Linux Course: Become a Linux Professional preparation.
The notes and questions for Shell Scripting Tutorial for Beginners 25 - Function Example have been prepared according to the Software Development exam syllabus.
Information about Shell Scripting Tutorial for Beginners 25 - Function Example covers all important topics for Software Development 2023 Exam.
Find important definitions, questions, notes, meanings, examples, exercises and tests below for Shell Scripting Tutorial for Beginners 25 - Function Example.
Video Lecture & Questions for Shell Scripting Tutorial for Beginners 25 - Function Example Video Lecture | Study Complete Linux Course: Become a Linux Professional - Software Development | Best Video for Software Development - Software Development full syllabus preparation | Free video for Software Development exam to prepare for Complete Linux Course: Become a Linux Professional.
Information about Shell Scripting Tutorial for Beginners 25 - Function Example
Here you can find the meaning of Shell Scripting Tutorial for Beginners 25 - Function Example defined & explained in the simplest way possible.
Besides explaining types of Shell Scripting Tutorial for Beginners 25 - Function Example theory, EduRev gives you an ample number of questions to practice Shell Scripting Tutorial for Beginners 25 - Function Example tests,
examples and also practice Software Development tests.