Check out a free preview of the full Complete Intro to Linux and the Command-Line course
The "Exit Codes & Process Operators" Lesson is part of the full, Complete Intro to Linux and the Command-Line course featured in this preview video. Here's what you'd learn in this lesson:
Brian explains how to run a sequence of commands and subcommands as well as exit codes using exit codes, and process operators. Operators allow engineers to run a task once another task has been completed.
Transcript from the "Exit Codes & Process Operators" Lesson
[00:00:00]
>> Let's talk about exit codes and process operators and subcommands. So, every time that your program finishes, it returns a exit code, like how the previous process finished. So if I say date, right, this is a very simple program that just returns to you the current date. If I say echo $?, it'll actually give me back how did the last command finish?
[00:00:34]
This one in this particular case, it finished successfully so it returned zero. Any process that return that finishes successfully, whatever it defines a success returns with the zero. Anything that returns with anything other than zero did not finish successfully. So basically, for the most part, the only thing that you need to worry about is the number zero or is the number not zero.
[00:00:59]
If it's not zero, it did not finish successfully. If it's zero, then it finished successfully. So a good example of this, if I type yes, right, and then I stop it, and then I hit echo $?, it finished with 130, which means it did not finish successfully. Now you might answer what does it mean if yes finishes successfully?
[00:01:22]
It never does, because it never finishes, it will never return to zero. So that's the only exit code that it could possibly have is something nonzero. Now the question might be asking is like what does 130 actually mean? And my first answer to that question is it doesn't matter.
[00:01:43]
It's not zero and that's really all you ever have to care about. But I know some of you are still curious as I was so I went looked it up. And there are some, General rules of thumb of how things should exit if they don't exit successfully. But again, it's up to the program of what they choose to do with about those things and it's not enforced by batch.
[00:02:05]
So it depends program by program. However, there are some numbers that generally people use. So here, I wrote something down here. 0 means it's successful. 1, which is one you'll probably see the most, just means there was an error of some variety, I did not finish successfully because of an error.
[00:02:26]
2 usually means that bash failed but not the program failed. Usually won't see that because bash doesn't fail a whole lot. The one that you just saw here, 130, means the user used Ctrl+C. Then from 137 to 165 or something like that there's some special combination. So those ones are generally not used by users.
[00:02:58]
You have 128 means that the exit command failed like there's there's a bunch of them out here and I just wrote down some. But again, suffice to say, you'll either see 0, you'll see 1, or you'll probably see, 130, cuz that means you stopped the program. Another one is interesting here, is 126 means the file wasn't actually executable.
[00:03:23]
But anyway, suffice to say, zero or nonzero. So why am I talking about this? Well, there's a thing called operators. So let's say I wanted to do something, and I only wanted to do a second thing if the first thing completed, right? So if I said touch status.txt, and I only wanna run the second thing if status.txt succeeds.
[00:03:55]
I can do &&, if you've programmed JavaScript, this looks very familiar. This works relatively the same way, then I'll run date into status.txt. And if that's successful, then I'm going to also append up time into status.txt. So this will run. If it returns a zero, then this will run.
[00:04:23]
And if that returns a zero, then this will run. Since all of those I intend on working, it should all work. So if I cat status.txt, you'll see here the date, right, which is what we saw there. And this automatically puts a new line at the end, so you don't have to do that.
[00:04:41]
And then uptime is a fun command. It tells you how long this computer has been up. So my computer since I think it restarted, Has been up for almost 2 hours. Looks like it has one user logged in, which makes sense because this is the uptime not for my Mac, this is not the uptime for multipass.
[00:05:08]
Has one user connected and it has no load right now, but it'll tell you like I've had 50% CPU usage over the past couple hours bah bah bah. Okay, so that's what the ampersand is. You'll probably see that a lot in like bash commands like that you copy and paste from Stack Overflow is a bunch of ampersand so you can do one or you can do like five commands with just one copy paste.
[00:05:32]
That's what that end end is useful for. But keep in mind if you fail any one of those, it will not run the second command. So if I say false, actually, let's just run false. And then echo $?, you'll see that false. All false does is return an error.
[00:05:50]
That's its entire existence is to not return zero. By the same virtue, you have true as well, and all it does is return zero. So, if I say false, and echo hi, what do you think it's gonna do? Nothing, it's not gonna do anything because this returns one and so therefore it doesn't reach the second clause.
[00:06:12]
They say true and echo hi, lo and behold, it echoes hi, okay? So I wanted to show you as well there is an or operator, Which is again more like JavaScript. It's two pipes next to each other. So this runs only if this was not successful. So, as you may imagine, this could be like, hey, do this and if you were not successful, then run the second thing that cleans up basically, that would be a use case for that.
[00:06:45]
Same case, if I say true here, it won't run that because it was successful. Okay, now what if you wanna run something no matter what? You can say true, and then you put a space semicolon space, false echo hi. So it goes through both of these. It runs both of them.
[00:07:12]
It doesn't actually care what that code is, and so it ends up running echo hi cuz it's just gonna truck on no matter what
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops