Project

General

Profile

Necessary Maintenance #15940

Armor all sourced scripts against unusual user-set shell options

Added by Christopher Green over 2 years ago. Updated over 2 years ago.

Status:
Accepted
Priority:
Normal
Assignee:
-
Target version:
-
Start date:
03/22/2017
Due date:
% Done:

0%

Estimated time:
2.00 h
Scope:
Internal
Experiment:
-
SSI Package:
-
Duration:

Description

Any bash-specific scripts which are sourced should save the state of shell options and activate a known set, restoring the original state at the end of said script, something like:

# Save user's options for restoration later.
saved_options=$(set +o)

# Make sure they get restored when we're done.
trap "eval \"${saved_options}\"" RETURN

# Make sure our preferred set of options are active, and no others.
source bash_options.txt

...

Note that shell-agnostic scripts should not need to do this, since they should be sufficiently armored already.

bash_options.txt (423 Bytes) bash_options.txt Basic set of options to provide a known environment inside a sourced bash script. Christopher Green, 03/22/2017 08:24 AM

History

#1 Updated by Christopher Green over 2 years ago

  • Description updated (diff)

#2 Updated by Christopher Green over 2 years ago

  • Description updated (diff)

#3 Updated by Lynn Garren over 2 years ago

  • Status changed from New to Accepted

One way to test for problems is to "set -u".



Also available in: Atom PDF