Under bash you can simply: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时,可以将生成的配置文件存储到一个变量中:As you're using eval, you could store your resulting config file into one variable: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" 注意:在第二行上,有转义的空格(以反斜杠\ 和引号'开头. strong>反斜杠告诉read不要拆分变量,并且必须将引号添加到结果/tmp/file2中.Note: On second line, there are escaped spaces (prepanded with a backshash \ and quotes '. The backslash tell read to not split the variable, and the quotes have to be added into the resulting /tmp/file2.在 posix的问题 shell 的问题,您可以这样做:Under posix shell, you may do this way:#!/bin/sh( cat <<eohead #!/bin/sh INSTALLPATH='/somepath/somewhere' SITEURL='example.com' PHPSERV='127.0.0.1:9000'; cat <<eofeohead cat template echo eof) | /bin/sh这不需要bash,已通过 dash 和 busybox 的问题.This don't require bash, was tested under dash and busybox.sedcmd=''for var in INSTALLPATH SITEURL PHPSERV;do printf -v sc 's/${%s}/%s/;' $var "${!var//\//\\/}" sedcmd+="$sc" donesed -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 进入循环的情况下起作用:Compatible answer, using sedThis could work without so called bashisms, into a loop:#!/bin/shwhile 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/file2eof比较输出: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; } } 这篇关于替换模板文件中的bash变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-23 15:41
查看更多