运行文件并解析占位符变量 [英] Run through file and parse placeholder variables
问题描述
原始config.conf
TEST =$ PLACEHOLDER_USERNAME
TEST2 =$ PLACEHOLDER_PASSWORD
我想做的是一个deploy.sh,它包含定义的变量,并且可以运行一个命令将使用定义的变量替换任何占位符变量,如下所示:
新的config.conf
TEST =Tom
TEST2 =abc123
我需要一个单一的命令,用于我的deploy.sh,它可以用于任何占位符变量。
$ PLACEHOLDER_USERNAME - > $ USERNAME - > Tom
一个示例(但不正确)deploy.sh以用户名为例
USERNAME =Tom
PASSWORD =abc123
FILE =config.conf
s ed -is / STRING_TO_REPLACE / STRING_TO_REPLACE_IT / g $ FILE
有人可以帮我完成这个脚本吗? / p>
你可以尝试这样的东西:
code> USERNAME =Tom
PASSWORD =abc123
FILE =config.conf
sed -is / \ $ PLACEHOLDER_USERNAME / $ USERNAME / g $ FILE
sed -is / \ $ PLACEHOLDER_PASSWORD / $ PASSWORD / g$ FILE
#perl -p -i -es / \ $ PLACEHOLDER_PASSWORD / $ PASSWORD / g$ FILE
我还包含了一个sed替代的样本,因为我不知道sed上面将会做的伎俩。
尝试#2
这应该是诀窍。
USERNAME =Tom
PASSWORD =abc123
LIST =USERNAME PASSWORD
FILE =config.conf
对于$ LIST中的项目; do
evalsed -i \s / \ $ PLACEHOLDER_ $ item / \ $$ item / g\$ FILE
done
I have been going over this for a few days and I am completely stuck. I put placeholder variables in my files which are in the following format: *$PLACEHOLDER_VAR* with the original variable being $VAR.
Original config.conf
TEST="$PLACEHOLDER_USERNAME"
TEST2="$PLACEHOLDER_PASSWORD"
What I want to do is have a deploy.sh which contains defined variables, and can run a command which will replace any placeholder variables with the defined variables, leaving it like this:
New config.conf
TEST="Tom"
TEST2="abc123"
I need a single command for my deploy.sh which will work for any placeholder variables.
$PLACEHOLDER_USERNAME -> $USERNAME -> Tom
An example (but not right) deploy.sh using username as an example
USERNAME="Tom"
PASSWORD="abc123"
FILE="config.conf"
sed -i s/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g $FILE
Can anyone please help me complete this script?
You could try something like this :
USERNAME="Tom"
PASSWORD="abc123"
FILE="config.conf"
sed -i "s/\$PLACEHOLDER_USERNAME/$USERNAME/g" $FILE
sed -i "s/\$PLACEHOLDER_PASSWORD/$PASSWORD/g" $FILE
# perl -p -i -e "s/\$PLACEHOLDER_PASSWORD/$PASSWORD/g" $FILE
I have also included a sample of the perl alternative to sed since im not sure if the sed above will do the trick.
Try #2This should do the trick.
USERNAME="Tom"
PASSWORD="abc123"
LIST="USERNAME PASSWORD"
FILE="config.conf"
for item in $LIST; do
eval "sed -i \"s/\$PLACEHOLDER_$item/\$$item/g\" $FILE"
done
这篇关于运行文件并解析占位符变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!