All right let's look at the exit status what are the exit status.
Every time a command is ran in a shell are in I would say let's go one step back into Linux.
It always has a status associated with it.
If it is successful it will always give us status of zero.
And there are statuses from all the way from 0 to 255 every status of the different meaning.
These are the actual error codes are the numbers that developers associated with different meanings.
Every developers or every application have a different meaning.
But for us we only going to focus on a very few statuses and that is number one is zero which is yes
or no if this X command that we ran previously is successful or not.
If it is not then it will give a message one status one which is known as a minor problem or number
two.
It's a serious trouble.
Or everything else which existe that is from three to two hundred fifty five everything else.
So why do we need actually actually exit status in our scripting.
Good question.
Well many men we have to do a lot of comparisons in our shell scripting following this lessons and in
comparison you need to know if your command ran.
If it did ran and it was successful then do this.
Otherwise don't do that.
As we learn that there are statements which if else while statements every every one of those scripts
or statements do need some kind of status before proceed to the next execution.
So let me show you real quick by logging into my next machine.
I am logged in as myself and I am in my home directory.
So now let's see if I run a command.
Ellis minus L.
And it runs successfully right.
It gave me everything that I was looking for but if I wanted to know actually the status of my preceding
command I would do echo dollar sign and then the question mark.
This is telling the shell or the bash that was the command that ran previously.
And tell me what that command was successful or not.
If I get a zero return.
There you go.
It means it did ran right because I wanted to get a listing of all my directory files and pictures in
this directory and it did give me.
And that's why I have ZERO.
Let's take another command Let's say I want to wish directory I'm in I'm in home.
I have Saldo actually perfekt Iran and if I do go dollar question mark again it will give me zero again
that it was successful.
What if I run a command let's say p w d d.
Right.
This command and there's no such command.
So if I run that it says bash command found similar command is PWT give me a recommendation.
But anyway the command was not successful.
So now if I asked the shelve that the last command was successful or not are run the same command echo
dollar question mark.
And it is giving me an error code 127 which is different from zero.
So as I said anything from 0 to 255 is not is arccos but anything above 0 is all error codes.
Anything that is 0 is successful.
Now when we run when we create a script so let's say I go into I create a script I call it test code
just that and I will
write I will declare my shell.
And now I will not define all those stuff that I know you already know since this is a very small script.
I'll just do echo.
Hello.
And then I will ask if the command was successful.
And I will save it test code.
Now when I run that you'll see I'm getting the hello and that I'm getting the 0.
It means it was successful.
Same thing if I wanted to do something else.
Let's see if I go back to test code.
And this time I will do instead of echo I'll just replace David Icke.
So there is no such command is Ikey's.
Anyway let's see what the error message comes up.
Now our run it again and now it's giving me the same error message that it was not successful.
Same way if I try.
LS minus LLNL and it does exist actually.
That's a ls minus Z.
So many options right.
LS.
Dash dash dash dash P.
OK.
So there you go see under-recognised option.
So now let's see what the error code is.
The area code is to see is it different and that is a bar code that shows serious trouble.
So this is about you have not defined the option successfully.
Now let's run another script really quick.
I'm going to create a script I'll call reply exit code exit code script and well define the shell and
this time I want to make sure I run LS minus l home.
I absol last check.
This is the command I want to do.
So what is going to do is going to do mine as well and it's going to only find out if home I have it
all and there is a file named check exist.
OK.
So now I have to define the statement If
dollar question mark which is the command that I ran equal to zero which means if the command ran successfully
then do what that echo file exist else echo file does not exist.
And fee.
And let's run it.
And give executable permission let's run the script and it will say cannot access home.
I have checked no such file and directory.
Then it says of course file does not exist because the error code does not match 0.
So if I create a file check in my home directory so I mean home directory.
I'm just going to do touch and check the file is 0 right.
So just verify it check and it is there right.
So now when I run that script again that I just created and it did give me that file with my last mysell
came in and it says file exist.
So that's why it is.
That's why the exit status important is there.
So this way you could actually associate each code with some kind of meaning.
So developers like C++ or other developers like object oriented base programmers are people who write
operating systems.
They are the ones you have Elysia use these codes because these codes make a lot of difference.
But in our class or in our lessons going from this point on we mostly going to use 0 to make sure things
exist or not and that's why most of the time the system administrator engineers use in every day Linux
scripting languages.
So try it out and see how you could create different scripts with error codes and what their code gives
you.