运行文件并解析占位符变量 [英] Run through file and parse placeholder variables

查看:232
本文介绍了运行文件并解析占位符变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经过了这几天,我完全陷入困境。我在我的文件中放置了占位符变量,格式如下:* $ PLACEHOLDER_VAR *,原始变量为$ VAR 。



原始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 #2

This 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆