12.07.2015 Views

Advanced Bash-Scripting Guide

Advanced Bash-Scripting Guide

Advanced Bash-Scripting Guide

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 6. Exit and Exit Status...there are dark corners in the Bourne shell, andpeople use all of them.--Chet RameyThe exit command may be used to terminate a script, just as in a C program. It can also return a value, whichis available to the script's parent process.Every command returns an exit status (sometimes referred to as a return status or exit code). A successfulcommand returns a 0, while an unsuccessful one returns a non-zero value that usually may be interpreted as anerror code. Well-behaved UNIX commands, programs, and utilities return a 0 exit code upon successfulcompletion, though there are some exceptions.Likewise, functions within a script and the script itself return an exit status. The last command executed in thefunction or script determines the exit status. Within a script, an exit nnn command may be used to deliveran nnn exit status to the shell (nnn must be a decimal number in the 0 - 255 range).When a script ends with an exit that has no parameter, the exit status of the script is the exit status of thelast command executed in the script (previous to the exit).#!/bin/bashCOMMAND_1. . .# Will exit with status of last command.COMMAND_LASTexitThe equivalent of a bare exit is exit $? or even just omitting the exit.#!/bin/bashCOMMAND_1. . .# Will exit with status of last command.COMMAND_LASTexit $?#!/bin/bashCOMMAND1. . .# Will exit with status of last command.COMMAND_LASTChapter 6. Exit and Exit Status 44

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!