2323# For an example of a valid input file, check `../infra/vars.tfvars.rename_me`.
2424# All variables need to be set to the environment previous running the script.
2525# Parameters:
26- # -variables <VARIABLES_LIST> Comma separated list of variables names.
27- # -in_file <INPUT_FULL_PATH_NAME> Full path and file name of the input file.
28- # -out_file <OUTPUT_FULL_PATH_NAME> Full path and file name of the output file.
29- # -no_quotes Option to disable enclosing variable in double quotes during substitution
26+ # -variables <VARIABLES_LIST> Comma separated list of REQUIRED variable names.
27+ # -optional_variables <OPTIONAL_LIST> Comma separated list of OPTIONAL variable names (may be unset or empty).
28+ # -in_file <INPUT_FULL_PATH_NAME> Full path and file name of the input file.
29+ # -out_file <OUTPUT_FULL_PATH_NAME> Full path and file name of the output file.
30+ # -no_quotes Option to disable enclosing variable in double quotes during substitution
3031
3132display_usage () {
3233 printf " \nThis script replaces variables from an input file creating/overriding the content of on an output file"
3334 printf " \nScript Usage:\n"
3435 echo " Usage: $0 [options]"
3536 echo " Options:"
36- echo " -variables <VARIABLES_LIST> Comma separated list of variables names."
37+ echo " -variables <VARIABLES_LIST> Comma separated list of REQUIRED variable names."
38+ echo " -optional_variables <OPTIONAL_LIST> Comma separated list of OPTIONAL variable names."
3739 echo " -in_file <INPUT_FULL_PATH_NAME> Full path and file name of the input file."
3840 echo " -out_file <OUTPUT_FULL_PATH_NAME> Full path and file name of the output file."
3941 echo " -no_quotes Do not enclose variable values with quotes."
@@ -42,6 +44,7 @@ display_usage() {
4244}
4345
4446VARIABLES=" "
47+ OPTIONAL_VARIABLES=" "
4548INPUT_FILE=" "
4649OUT_FILE=" "
4750ADD_QUOTES=" true"
@@ -55,6 +58,11 @@ while [[ $# -gt 0 ]]; do
5558 shift # past argument
5659 shift # past value
5760 ;;
61+ -optional_variables)
62+ OPTIONAL_VARIABLES=" $2 "
63+ shift # past argument
64+ shift # past value
65+ ;;
5866 -in_file)
5967 IN_FILE=" $2 "
6068 shift # past argument
@@ -96,30 +104,46 @@ then
96104fi
97105
98106list=$( echo " $VARIABLES " | tr " ," " \n" )
107+ optional_list=$( echo " $OPTIONAL_VARIABLES " | tr " ," " \n" )
99108
100- # Check if all variables are set
101- for varname in $list
102- do
103- if [[ -z " ${! varname} " ]]; then
104- echo " Missing required variable value with name: $varname ."
105- echo " Script will not execute variables replacement, bye for now."
106- exit 1
107- fi
109+ # Check required variables (optional ones may be unset or empty)
110+ for varname in $list ; do
111+ if [[ -z " ${! varname+x} " ]]; then
112+ echo " Missing required variable (unset) with name: $varname ."
113+ echo " Script will not execute variables replacement, bye for now."
114+ exit 1
115+ fi
116+ if [[ -z " ${! varname} " ]]; then
117+ echo " Missing required variable (empty value) with name: $varname ."
118+ echo " Script will not execute variables replacement, bye for now."
119+ exit 1
120+ fi
108121done
109122
110123# Reads from input setting the first version of the output.
111124output=$( < " $IN_FILE " )
112125
113126# Replace variables and create output file
114- for varname in $list
115- do
116- # shellcheck disable=SC2001
117- # shellcheck disable=SC2016
118- if [[ " $ADD_QUOTES " == " true" ]]; then
119- output=$( echo " $output " | sed ' s|{{' " $varname " ' }}|' \" " ${! varname} " \" ' |g' )
120- else
121- output=$( echo " $output " | sed ' s|{{' " $varname " ' }}|' " ${! varname} " ' |g' )
122- fi
127+ for varname in $list ; do
128+ # Required variables are guaranteed non-empty here
129+ value=" ${! varname} "
130+ # shellcheck disable=SC2001
131+ # shellcheck disable=SC2016
132+ if [[ " $ADD_QUOTES " == " true" ]]; then
133+ output=$( echo " $output " | sed ' s|{{' " $varname " ' }}|' " \" $value \" " ' |g' )
134+ else
135+ output=$( echo " $output " | sed ' s|{{' " $varname " ' }}|' " $value " ' |g' )
136+ fi
137+ done
138+
139+ # Substitute optional variables
140+ for varname in $optional_list ; do
141+ value=" ${! varname} "
142+ if [[ " $ADD_QUOTES " == " true" ]]; then
143+ output=$( echo " $output " | sed ' s|{{' " $varname " ' }}|' " \" $value \" " ' |g' )
144+ else
145+ output=$( echo " $output " | sed ' s|{{' " $varname " ' }}|' " $value " ' |g' )
146+ fi
123147done
124148
125149echo " $output " > " $OUT_FILE "
0 commit comments