Using Exit Codes to Report Success or Failure
Every time a process finishes, it has the opportunity to return a single number to the process that called it. This is referred to as its exit code or exit status. A status of zero indicates that the program succeeded in what it was asked to do. Any other value indicates there was some sort of problem and that the command failed in some way. The shell variable $? allows us to examine the exit code of any app we run. We can use this to verify that mysqldump, the command our database backup app uses to perform the actual backup, exits zero on success and nonzero otherwise, like so:
| $ mysqldump non_existent_database |
| No such database 'non_existent_database' |
| $ echo $? |
| 1 |
| $ |
Get Build Awesome Command-Line Applications in Ruby 2 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.