【发布时间】:2019-06-12 07:26:56
【问题描述】:
我的 bash 脚本使用 perl 命令将文件中的变量替换为另一个文件的内容。我认为很标准,但我在这里真的很挣扎。
#/bin/bash
display_usage() {
echo -e "\nUsage: This script must be run with an environment parameter (file in directory env/)."
echo -e "Example: ./configureEnv <env parameter>\n"
}
# If no env argument supplied, display usage.
if [ $# -eq 0 ]; then
display_usage
exit 1
fi
# Replace the placeholder of FIREBASE_ENV in index.html with the Firebase env settings.
perl -pi -e 's/%FIREBASE_ENV%/`cat testEnvConfig`/g;' index.html
if [ $? -eq 0 ]; then
echo "Updated Firebase settings based on environment file: $1"
exit 0
else
echo "[Error] Environment settings configuration failed. Please check parameters are correct."
exit 1
fi
如你所见,关键是:
perl -pi -e 's/%FIREBASE_ENV%/`cat testEnvConfig`/g;' index.html
它应该将 %FIREBASE_ENV% 的占位符字符串替换为文件的内容,而是将 %FIREBASE_ENV% 替换为 `cat testEnvConfig`。
【问题讨论】: