Stop Shell Script Execution on Error + Other Shell Controls

In some cases, running a shell script after an error in one of the commands defeats the purpose of executing it. You can control to stop script execution when any command returns an error by including this statement in the script:
set -e
You can also start the script with -e option to achieve same result
#!/bin/sh -e
Let's look at set command and other useful options it offers.

Syntax:
To turn on an option:
set -<Abbr.>
To turn off an option:
set +<Abbr.>
To turn on an option:
set -o <Option>
To turn off an option:
set +o <Option>

Examples:
set -o errexit Executes ERR trap on a non-zero error
set +o errexit ERR trap is not set
set -e errexit Executes ERR trap on a non-zero error
set +e errexit ERR trap is not set

More Options
Option Abbr. Brief explanation
allexport a Variables are automatically exported to subshells
bgnice Assign low priority to bg jobs
noclobber Overwrite protection on redirection
noexec n Commands are not executed. A syntax check is done
noglob f Ignore wildcards
nounset u Error on unset variable
xtrace x Print the commands with the argumets when executed for debugging

Comments

Popular posts from this blog

OS/DB Migration - CMD. STR, TOC, EXT, R3load, DDLDBS.TPL and more

Fixing Inconsistent Table - Table activation fails due to inconsistency between DD and DB

301 Redirect Using SAP Web Dispatcher