From 47a7b4532a0f7cfd8c0b7f258f53df5d9929d0ab Mon Sep 17 00:00:00 2001 From: Richard Maw Date: Thu, 5 Aug 2021 19:17:40 +0100 Subject: bash: Support --env option in generated scripts --- share/bash/template/template.sh.tera | 39 ++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'share') diff --git a/share/bash/template/template.sh.tera b/share/bash/template/template.sh.tera index 938509a..0e67c4e 100644 --- a/share/bash/template/template.sh.tera +++ b/share/bash/template/template.sh.tera @@ -2,6 +2,40 @@ set -eu -o pipefail +declare -a ENV + +TEMP="$(getopt --long env: -o e: -- "$@")" +eval set -- "$TEMP" +unset TEMP + +while true +do + case "$1" in + '-e'|'--env') + case "$2" in + *=*) + # Use value from command-line + ENV=("${ENV[@]}" "$2") + ;; + *) + # Forward variable from environment + ENV=("${ENV[@]}" "$2=${!2}") + ;; + esac + shift 2 + continue + ;; + '--') + shift + break + ;; + *) + echo Unexpected argument >&2 + exit 1 + ;; + esac +done + ############################################################################# # Functions that implement steps. @@ -133,6 +167,11 @@ unset $(envnames) export PATH=/bin:/usr/bin export SHELL=/bin/sh +# Include any configured environment variables +for var in "${ENV[@]}" +do + export "$var" +done ############################################################################# # Run the scenarios. -- cgit v1.2.1