Files
2023_WEBSITE_jipf/srcs/env_generator/create_env.sh

48 lines
1.3 KiB
Bash
Executable File

#!/bin/bash
# - this script creates a .env file in the same directory of the model.env
# - the model.env file is given in argument, otherwise it default to current path
# change to where the script is relatively to where the execution is made
#cd $(dirname $0)
CURRENT_PATH=$(dirname $0)
if [ $# -eq 0 ]; then
FILE_PATH="${CURRENT_PATH}/model.env"
ENV_PATH=${CURRENT_PATH}
else
FILE_PATH="$1"
ENV_PATH=$(dirname $FILE_PATH)
fi
if [ ! -f "$FILE_PATH" ]; then
echo ""
echo " no file 'model.env' found"
echo ""
echo " you need to create one in the same directory of this script,"
echo " or to give the path to a valide one in argument"
echo ""
echo " like './create_env path/to/model.env'"
echo " "
echo " note that the .env file will be created where the model.env is"
echo ""
exit 1
fi
TMP_FILE="tmpfile_3289442091310922474928"
TMP_BEFORE="env_before.tmp"
TMP_AFTER="env_after.tmp"
# add lines at beginning and end of a temp env file
echo "set | sort > ${TMP_BEFORE}" > ${TMP_FILE}
cat ${FILE_PATH} >> ${TMP_FILE}
echo "set | sort > ${TMP_AFTER}" >> ${TMP_FILE}
source ${TMP_FILE}
# diff between the two set files == the newly created env var
NEW_ENV=$(comm -13 ${TMP_BEFORE} ${TMP_AFTER} | grep -v "^_=")
echo "${NEW_ENV}" > ${ENV_PATH}/.env
rm -f ${TMP_BEFORE} ${TMP_AFTER} ${TMP_FILE}