don't attempt environment substitution if no variable in file

master
David Kebler 2024-05-25 13:40:07 -07:00
parent ca6f831c5d
commit 95a5f21abd
1 changed files with 9 additions and 3 deletions

View File

@ -78,15 +78,17 @@ testenv () {
# https://www.baeldung.com/linux/envsubst-command
# todo allow input file -o overwrite or to another file -f <name>
env_subs_file () {
local fin; local fenv; local useenv; local tout; local fout; local fenv; local tout2
local verbose; local fin; local fenv; local useenv; local tout; local fout; local fenv; local tout2
[[ $1 == "-v" ]] && verbose=true && shift 1
[[ $1 == "-e" ]] && useenv=true && shift 1
[[ $1 == "-o" ]] && { shift 1; fout=$1; }
[[ $1 == "-f" ]] && { fout=$2; shift 2; }
fin=$1; fenv=$2
# echo useenv $useenv, fout $fout, fin $fin, fenv $fenv
if [[ $(cat $fin | grep -F "$") ]]; then
[[ ! $fin ]] && iecho "nothing was passed to merge with environment" && return 1
[[ ! -f "$fin" ]] && iecho "file $fin does not exit nothing to merge with environment" && return 1
[[ ! $(cat $fin | grep -F "$") ]] && iecho "no variables in $fin to merge" && return 2
[[ ! -f "$fin" ]] && iecho "file $fin does not exist nothing to merge with environment" && return 1
tout=/tmp/${USER}-mergedenvfile
if [[ $useenv ]] && [[ -f $fenv ]]; then
tout2=/tmp/${USER}-mergedenvfile2
@ -100,6 +102,10 @@ env_subs_file () {
fi > $tout
[[ -f $tout2 ]] && rm -f $tout2
[[ $fout ]] && mv -f $tout $fout || { echo merged; cat "$tout"; }
else
[[ $verbose ]] && echo "environment variable substition, no variables in $fin to merge"
fi
# env | grep BASE
# # unset_env_file $2
}