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

标签 linux bash

我正在尝试使用 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/

相关文章:

linux - 如何比较两个未排序且行略有不同的文本文件

c - 使用 Control + C 的进程突然结束是陷阱还是中断?

linux - 旧版本的 salt-minion 中不存在 http 代理设置

python - 在 bash 脚本中使用反引号,将 python 脚本调用到变量中以前可以工作,但现在已损坏

bash - 将文件分成单独的文件,每个文件 10,000 行

bash - Awk:停止 'print' 添加换行符以及如何循环/自动化

json - 将 JSON 解析为 shell 脚本中的数组

python - 在 Linux 上使用 discord.py 我得到错误 'Bot' object has no attribute 'join_voice_channel'

linux - 如何传递变量来执行外部操作而不是 bash 中的命令参数

windows - Windows 上的多个并行 MSys/MingW 安装