skip to main content

SAP NetWeaver Newbie

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

No comments:

Post a Comment

Email Subscription

Get every new post into your inbox by subscribing us.

Want a reason to subscribe?
1. This sitemap might convince you to subscribe.
2. We do not misuse email IDs. We respect privacy.

© 2008 - 2017 sapnwnewbie. All rights reserved.