微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

replace模板文件中的bashvariables

我正在尝试使用Bash来运行安装过程的一种forms。 在这个过程中,一个configuration文件被复制,并且其中的某些值被replace。 这样的configuration可以在下面find:

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,并且我已经尝试使用它来做这个replace,但是看起来这里的文件没有正确的扩展,而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

但是,这不会正确地replace已声明的值,也不会正确生成文件。 它会丢失任何以#开始的行,并尝试执行其他行(并丢失一些空格)。

shell之间的redirect位置的区别

将所有脚本参数复制到另一个variables

为什么通过“scriptName”调用脚本不起作用?

if 是什么意思?

我怎么能得到一个字,在我提供的数字上有所不同?

什么才是正确的方法来做到这一点,只使用大多数Linux系统上可用的Bash和命令?

使用ps的线程

Linux:如何杀死睡眠

在Linux脚本中是什么意思? #!/ usr / bin / python -tt

做一些程序不接受进程replaceinput文件

shell脚本如果语句麻烦

安全提示

这不关心安全问题 ! 使用eval是邪恶的 !

兼容的答案不是更好!

当然,你必须对你的模板的内容有信心!

如果其他,请尝试使用sed ! (看我最后的答案)

快速的方式只有!

在bash下,你可以简单地:

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 。

软方式(兼容答案)

在posix shell下 ,你可以这样做:

#!/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,是在破折号和busyBox下测试的。

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; } }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐