From 035efed074f0d02adc5b270b5ea27b6196884bd5 Mon Sep 17 00:00:00 2001 From: Todd Gamblin Date: Wed, 3 Jul 2019 12:43:13 -0700 Subject: [PATCH] setup-env.sh: make setup scripts work with `set -u` - Add set -u to the setup-env.sh test script - Refactor lines in setup-env.sh that tested potentially undefined variables to use the `[ -z ${var+x} ]` construct --- share/spack/qa/setup-env-test.sh | 3 +++ share/spack/setup-env.sh | 7 ++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/share/spack/qa/setup-env-test.sh b/share/spack/qa/setup-env-test.sh index ee1b8acf8cc5be..830fc14fbcf101 100755 --- a/share/spack/qa/setup-env-test.sh +++ b/share/spack/qa/setup-env-test.sh @@ -160,6 +160,9 @@ unuse() { # Make sure no environment is active unset SPACK_ENV +# fail with undefined variables +set -u + # Source setup-env.sh before tests . share/spack/setup-env.sh diff --git a/share/spack/setup-env.sh b/share/spack/setup-env.sh index c5bd32882d256f..31e965f4f32263 100755 --- a/share/spack/setup-env.sh +++ b/share/spack/setup-env.sh @@ -47,8 +47,9 @@ spack() { fi # accumulate initial flags for main spack command + # trnaslation for humans: "while $1 is set and $1 starts with -" _sp_flags="" - while [ "${1#-}" != "${1}" ]; do + while [ ! -z ${1+x} ] && [ "${1#-}" != "${1}" ]; do _sp_flags="$_sp_flags $1" shift done @@ -63,7 +64,7 @@ spack() { fi _sp_subcommand="" - if [ -n "$1" ]; then + if [ ! -z ${1+x} ]; then _sp_subcommand="$1" shift fi @@ -102,7 +103,7 @@ spack() { case $_sp_arg in activate) _a="$@" - if [ -z "$1" ] || \ + if [ -z ${1+x} ] || \ [ "${_a#*--sh}" != "$_a" ] || \ [ "${_a#*--csh}" != "$_a" ] || \ [ "${_a#*-h}" != "$_a" ];