Exiting with a Return Code

The exit command terminates your script and passes a given return code to the shell. Return codes are the reason that commands could be run in sequence in Combining commands: the shell checks the return code of the preceding command before running the next. Also, a shell script that calls another script can check its exit code in conditional statements to determine what to do next.

By tradition, scripts should return 0 for success and 1 (or other nonzero value) on failure. If your script doesn’t call exit, the return code is automatically 0:

if [ $# -lt 2 ]
then
  echo "$0 error: you must supply two arguments"
  exit 1
else
  echo "My name is $1 and I come from $2"
fi
exit 0

➜ ./myscript Bob
./myscript error: you must supply two arguments
➜ echo $?
1
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.59.163