 Okay, today we're gonna be looking at creating an error function for our script that will display a message and exit out of our script. So let's go ahead and just jump right in. Here I am in an empty directory. I made a folder called errors just for this script. So let me go ahead, I'm gonna use vim as my text editor, but as always use whatever text editor you would like. And I'm gonna create a script called errors.sh. How about that? And it's gonna be a shell script. So I'm gonna say bin bash here, because it's a bash script. Now, we know, we should know that echo dollar sign and then a number would be that argument. So if I say dollar sign one, so I can exit out of the script, make it executable and then I can say errors. If I do that, nothing shows up, because I didn't pass anything. If I say this, it will print this. If I write that, it will write that. If I write this and that, it's still gonna just write the first word unless I put it in quotations. So what we're gonna do is we're going to check, is the user giving that input, that first argument? If not, we want to exit with an error, okay? So, we'll go back into our script here. And all we have to do is say this. So that's just checking, that's like an if then statement, it's shorthand, but it's saying, look at this first argument. If it's empty, do this, if it's something. So we would say, ampersand, ampersand, echo, yes, if they put something, ampers, or pipe, pipe, echo, no, if they forget it. So we'll go ahead, run this script. If we don't give any input, it should say no. And if we give in a first argument here, whatever it is, it will say yes, okay? So we don't just want to display something, we want to exit. And we want to exit with an exit code of one, meaning that the script failed. So there's a few different ways we're gonna do this, but we're gonna create a function. So let's go ahead, go back into our script. And I'm gonna create a function. I'm gonna type function error. But you can call your function wherever you want. You can say fail or fatal error or whatever. Again, as I always say, you don't have to write function. I just think it looks cleaner and makes it more defined. This is a function. And what I want to do here is, well, we can just say exit, right? So I can come down here. If it fails, we will run, I could just say exit here. And it would exit out of the script. We don't want to say exit, we want to say exit one, okay? So we could do that. So we're not even gonna call our function. We'll do this, it says yes, if we don't give it that. And now exits with one. If I say dollar sign question mark, that will give me the exit code of the last code. So that will tell me that script failed. So I can then run this script and check it. Let's go back into here, but I want to display a message as well. And if I was to do something like this, I could create a sub process. I can say echo no, and then I can say exit one. But that's not going to work properly because it's a sub function. So it's just gonna exit out of that sub function. So sub process. So what I'm gonna do here is, I'm gonna forget about the what does when it's proper because we're just gonna exit if it fails. We don't care about what happens. It's gonna continue if it's true. So we don't care about when it's true at this point. So here I'm just gonna say, okay, if this fails, then error. But we don't just want to exit, we want to again exit one. And we also want to have display a message. So I'll just say echo, and then I'll say dollar sign. You can say asterisk or the at symbol. Both will print out everything that is passed. One will just do it as an array. The other one will do it as a string. So here we're good, but I can pass it a message now and I can say you need to enter something. And that should work like that, although I usually use quotations. I'm gonna go back and do quotations, but I just wanted to check this. So I'm gonna say errors and I hit that and it says you need to enter something. But if I enter something, then it just continues without that error. Great, so we have this. We're checking, again, did we pass something? If not, run our error function and give it this. Okay, and again, I don't have to, but I always just like putting things in quotations. You're less likely to get errors that way. Okay, so here we can say, instead of you need to enter something, how about we say you need to enter a file name, right? So next we're gonna check, okay, not just did they enter something, and this doesn't have to be in quotations. I go back and forth on whether I wanted in quotations or not, it really doesn't matter. Down here I'm gonna say dash F, dollar sign one. And what that does is it's gonna take what the user inputs and check it. Is it a file that exists? Okay, if not, then we're gonna say error, dollar sign one is not a file, okay? So again, it's going to exit out. So I can even say down here, this is the end of the script, okay? So if I run this and I don't give it anything, it should say you need to enter a file name. Let's create a file real quick. I'm just gonna say touch file.txt. So if I listed out my script and that file exists. So now, again, if I run it, it gives me, you need to enter a file name and it exits before continuing, right? So now I can give it something, I can say just whatever. That's gonna say what I typed is not a file. But if I give it a file name, file.txt, it says it gets to the end of the script. This is the end of the script. Great, let's go a little bit more with this. So again, we're saying if this fails. We don't have to say if it's true because it's gonna continue in the script if it's true. So you don't have as many indentations and it's just shorter and it's cleaner. But let's go ahead and add a little bit more to this. I also like to do something like this and this is just a personal preference and I'm gonna copy and paste some code here because it's a little bit quicker. But I'm saying red and then I'm giving it a value here, normal and value and what I'd like to do here is I would like to do this. So whatever text you pass into the error function will be displayed in red and then it will go back to normal after that. So again, I can say nothing and it'll display that in red. I can give it something and it will tell me that's not a file. But if I do give it a file name, there we go. And we're exiting with exit code of one so I can actually check to make sure this script ran properly. So if I was to do this, I can say echo yes, but if it fails, echo no. So right now I'm not giving everything so it's exiting and it's saying, no, you failed. The last command didn't run. That's why we're doing exit one instead of exit. So if we just did exit, it thinks the script made it to a successful completion. And then I can give it a file, something like that, it's still gonna say that it failed. But if I give it the file.txt, it should say yes this time because it never got to that error function. Great, let's go ahead clear the screen and we'll go back into our script here. And what we're gonna do here is we're gonna do another check, right? So what I'm gonna do is I'm gonna check to see if there's a second argument, okay? So we'll go this dollar sign two and we're gonna say, okay, if there's not a dollar sign two, we can then do error as well. You know, error, dollar sign two, or sorry, echo second arg needed, right? And that would work, we can run it. So I can run it like this, don't worry about all that. It gets there because second arg needed and then I can give it something. It doesn't check what it is, but it made it to the end successfully. But if we go back in here, let's say I wanted to run a few things, a few options in there. I could go like this, whoop. I can say echo thank you and then I can say this and then I can do something after that. I can like list out files but I'm just gonna do echo commands to keep it simple and I'll say continue or whatever. I'll just say hello world. It doesn't matter. I'm just showing that I'm grouping a few commands here but because I'm putting them in these parentheses, it's considered a sub function. So if I was to run this, again, if I do this, that makes sense but if I don't give that argument, it should have exited right here, right? Because obviously it ran our error function. Well, the thing is since it's a sub process when it exited that sub process function finished and it exited but it didn't exit our full script. So how do we get to kill our entire script and still give that proper exit code? So we'll go back into our script here and what we do is at the beginning of our script, we're gonna do a command like this. We're gonna say, we're gonna create a variable called proc or whatever you wanna call it. We're gonna say dollar sign, dollar sign. What is dollar sign, dollar sign? Well, we know what dollar sign one is, dollar sign at, dollar sign asterisk, dollar sign, dollar sign is the process of the current running script. So in our error function, instead of exiting, what we're going to do is we're going to say kill, we're gonna use the kill command and there's different options here. We're gonna go to 10, I'm not gonna get into it but just when I read that's what we should be using here but there's different options when you're killing the script and we're just gonna say dollar sign proc. Oh, capital P, that's how we wrote it. Or we can call it PID and my notes and my testing of this, I wrote proc. PID might be a little bit more, whatever. So yeah, so now, before when I ran this, it told us the second argument is needed but continued. Let's see what happens now. Ha ha, okay. So our sub-process continued, which is good and our main script exited. But let's also do this. So we want to make sure, in fact I haven't tested this but we're gonna do this, we're gonna do that and it says no that it failed. So actually what I'm about to show you may not be necessary but from my experience you're supposed to do this too. So we're gonna go over it. We're gonna do a trap. So we're gonna say trap exit 110. And supposedly just adding that is supposed to give us our proper exit code but apparently it was working properly without that. Let me see, we'll run this again. There we go, no. Wait, did it say yes or no last time I ran it? Yeah, it said no. So I'm not sure exactly what's happening there but you're supposed to do the trap command which I don't know a whole lot about. I should read more into it and do a video on that. But this is our code so far. And yeah, so let's go over it real quick. We're saying some variables up here for our process, our color codes just because I like colors and errors telling you red, this is an error. And we're gonna say echo and kill with dash 10 for our process if we get to an error. So again here we can get the value of one if there's nothing we're gonna ask for a file name. Then we're gonna check that it's a file. If they did enter something, if not, then it's gonna exit. And then here we're gonna say echo thank you, error. And we're gonna pass it the error and then hello. So this will continue because it's a sub-process which you may or may not want. And then what we're gonna do after that is, well we're either gonna continue or not but here we're going to kill if we don't give it that second argument. Okay, so I think I said and did everything properly. Yes, so we got no there but if I pass it this we got yes, perfect. Also keep in mind if you want to we can also pass new line characters to this but we have to add dash E to this. So what I can do in here is you need to enter a file, file name and then I can say backslash N now exiting. So now if I do our script and I don't give it anything as the first argument it will give it as two lines. So you can pass multiple lines into it that way. That's one way to pass multiple lines. Also, oh, one more thing that I didn't do is we want to at the end of our echo command here what we wanna do is greater than at two. And that will tell when it prints this it's gonna print it as error text. So if you're piping this into another program it's not standard out, it's error out so it can ignore it. Or if you're just trying to get the errors you can grab it it just distinguishes between the two or differ rich, it tells a difference between the two. Okay, so again we got an error here. Give it that, it's gonna tell me it's not a file if I say file.txt, we get that hello world. We need a second argument, give it a second argument and it finally gets to the end of the script. Okay, so I hope that made sense, at least some of it. Play with this script. If you look in the description of this video you will be able to find a copy of this script up on Payspin and if we want to you can go to my way. If we want to, if you want to you go to filmsbychrist.com and of course you can go to the software section. If you click on notes it will list all my Payspin stuff and I just uploaded this one so it hasn't been updated to this list because this list gets updated once or twice a day with the crown job, but you can type through here. So again, if you want to type an error it's gonna bring up everything with error. You know, I can say bash error and it'll bring up everything with bash error. So you should be able to find the script there but again it's in the link in the description but if you wanna look through my other files you can do that there. Anyway, this is Chris from filmsbychrist.com. I hope you enjoyed this video and I hope that you have a great day.