gpt4 book ai didi

linux - 替换模板文件中的 bash 变量

转载 作者:IT王子 更新时间:2023-10-29 01:21:35 25 4
gpt4 key购买 nike

我正在尝试使用 Bash 来运行某种形式的安装过程。在此过程中,将复制配置文件并替换其中的某些值。这样的配置可以在下面找到:

server {
listen 80;
root ${INSTALLPATH};
server_name ${SITEURL};

client_max_body_size 20m;
client_body_timeout 120s;

location / {
try_files /public/router.php =404;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_pass ${PHPSERV};
fastcgi_index router.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}

location /assets {
try_files /app/$uri =404;
}

}

#Enables HTTPS access
#This requires you to install certificates and is not enabled by default
#If you wish to enable HTTPS, uncomment (remove the #s) from the below lines
#And change the ssl_certificate and ssl_certificate_key to point to the correct
#certificates.

#server {
# listen 443;
# root ${INSTALLPATH};
# server_name ${SITEURL};
#
# ssl on;
# ssl_certificate /etc/nginx/ssl/site.crt;
# ssl_certificate_key /etc/nginx/ssl/site.key;
#
# location / {
# try_files /public/router.php =404;
# fastcgi_split_path_info ^(.+?\.php)(/.*)$;
# fastcgi_pass ${PHPSERV};
# fastcgi_index router.php;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# include /etc/nginx/fastcgi_params;
# }
#
# location /assets {
# try_files /app/$uri =404;
# }
#
#}

我发现大多数示例都与 eval 的使用有关,并且我尝试使用它来进行此替换,但是看起来这里的文件没有正确扩展,bash 尝试执行一些东西但不能'

目前,我有这个

INSTALLPATH="/var/www/html/mycustomsite/"
PHPSERV="127.0.0.1:9000"
SITEURL="example.com"

while read -r line; do
eval echo -e "${line}"
done < template

但是,这并没有正确地替换声明的值,也没有正确地生成文件。例如,它会丢失任何以 # 开头的行并尝试执行其他行(同时会丢失一些空格)。

执行此操作的正确方法是什么,仅使用 Bash 和大多数 Linux 系统上可用的命令?

最佳答案

安全提示

这不会处理安全问题!使用 eval邪恶的!

兼容的答案并没有更好!

当然,你要对自己模板的内容有信心!!

否则,请尝试使用 ! (看我最后的回答)

快捷方式只有!:

你可以简单地:

eval "INSTALLPATH='/somepath/somewhere' SITEURL='example.com' PHPSERV='127.0.0.1:9000'; echo \"$(<template)\""

eval "INSTALLPATH='/somepath/somewhere'
SITEURL='example.com'
PHPSERV='127.0.0.1:9000';
echo \"$(<template)\""

当您使用 eval 时,您可以将生成的配置文件存储到一个变量中:

eval "INSTALLPATH='/somepath/somewhere'
SITEURL='example.com'
PHPSERV='127.0.0.1:9000';
cfgBody=\"$(<template)\""

然后

echo "$cfgBody"

和/或

echo "$cfgBody" >/cfgpath/cfgfile

循环执行此操作

tmplBody="$(<template)"
while read INSTALLPATH SITEURL PHPSERV CFGFILE;do
[ "$CFGFILE" ] && eval "echo \"$tmplBody\"" >"$CFGFILE"
done <<<"
/somepath/somewhere example.com 127.0.0.1:9000 /tmp/file1
'/some\ other\ path/elsewhere' sample2.com 127.0.0.1:9001 /tmp/file2
"

注意:在第二行,有转义空格(用反斜杠\引号'反斜杠 告诉 read 不要拆分变量,引号必须添加到生成的 /tmp/file2.

软方式(兼容答案)

,你可以这样做:

#!/bin/sh

(
cat <<eohead
#!/bin/sh
INSTALLPATH='/somepath/somewhere'
SITEURL='example.com'
PHPSERV='127.0.0.1:9000';
cat <<eof
eohead
cat template
echo eof
) | /bin/sh

这不需要 bash,已在 下测试和 .

没有eval !

sedcmd=''
for var in INSTALLPATH SITEURL PHPSERV;do
printf -v sc 's/${%s}/%s/;' $var "${!var//\//\\/}"
sedcmd+="$sc"
done
sed -e "$sedcmd" <template

可以进入循环:

while read INSTALLPATH SITEURL PHPSERV CFGFILE;do
if [ "$CFGFILE" ] ;then
sedcmd=''
for var in INSTALLPATH SITEURL PHPSERV;do
printf -v sc 's/${%s}/%s/;' $var "${!var//\//\\/}"
sedcmd+="$sc"
done
sed -e "$sedcmd" <template >"$CFGFILE"
fi
done <<<"
/somepath/somewhere example.com 127.0.0.1:9000 /tmp/file1
'/some\ other\ path/elsewhere' sample2.com 127.0.0.1:9001 /tmp/file2
"

兼容的答案,使用sed

这可以在没有所谓的 bashisms 的情况下工作,进入一个循环:

#!/bin/sh

while read INSTALLPATH SITEURL PHPSERV CFGFILE;do

sedcmd="s|\\\${INSTALLPATH}|${INSTALLPATH}|;"
sedcmd="${sedcmd}s|\\\${SITEURL}|${SITEURL}|;"
sedcmd="${sedcmd}s|\\\${PHPSERV}|${PHPSERV}|;"

sed -e "$sedcmd" template >"$CFGFILE"

done <<eof
/somepath/somewhere example.com 127.0.0.1:9000 /tmp/file1
'/some\ other\ path/elsewhere' sample2.com 127.0.0.1:9001 /tmp/file2
eof

比较输出:

diff -u99 /tmp/file{1,2}
--- /tmp/file1 2015-05-31 11:02:03.407463963 +0200
+++ /tmp/file2 2015-05-31 11:02:03.407463963 +0200
@@ -1,22 +1,22 @@
server {
listen 80;
- root /somepath/somewhere;
- server_name example.com;
+ root '/some other path/elsewhere';
+ server_name sample2.com;

client_max_body_size 20m;
client_body_timeout 120s;

location / {
try_files /public/router.php =404;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
- fastcgi_pass 127.0.0.1:9000;
+ fastcgi_pass 127.0.0.1:9001;
fastcgi_index router.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}

location /assets {
try_files /app/$uri =404;
}

}

关于linux - 替换模板文件中的 bash 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30539782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com