shell - 在 shell 中打印多行 - 提高配置 Vagrantfile 时的可读性

标签 shell nginx vagrantfile code-readability

我使用 printf 来设置我的 Nginx 设置,它工作正常,但读取和更改很尴尬;有没有更好的方法来提高可读性?

config.vm.provision "shell", inline: <<-SHELL

    sudo rm /etc/nginx/sites-enabled/default

    sudo printf '%s\n' 'server {' 'root /home/vagrant/web;' 'index index.php index.html index.htm;' 'location / {' 'try_files $uri $uri/ /index.php?$args ;' '}' 'location ~ \.php$ {' 'fastcgi_split_path_info ^(.+\.php)(/.+)$;' 'fastcgi_pass unix:/var/run/php5-fpm.sock;' 'fastcgi_index index.php;' 'include fastcgi_params;' '}' '}' >> /etc/nginx/sites-enabled/default

    sudo service nginx start

SHELL

最佳答案

假设您的访客计算机上有 Bash,您可以尝试使用嵌套 bash here documents 。不能保证它可以与 Vagrantfile 一起使用,但无论如何值得一试。

config.vm.provision "shell", inline: <<-SHELL

    sudo /bin/bash << 'SCRIPT'
        rm /etc/nginx/sites-enabled/default;

        cat << 'EOF' >> /etc/nginx/sites-enabled/default
            %s\n

            server {
                root /home/vagrant/web;
                index index.php index.html index.htm;

                location / {
                    try_files $uri $uri/ /index.php?$args ;
                }

                location ~ \.php$ {
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
                    fastcgi_pass unix:/var/run/php5-fpm.sock;
                    fastcgi_index index.php;
                    include fastcgi_params;
                }
            }
EOF
        service nginx start
SCRIPT
SHELL

请注意,EOF、SCRIPT 和 SHELL 必须放在行的最开头。这些单词前面不能有任何制表符或空格。

关于shell - 在 shell 中打印多行 - 提高配置 Vagrantfile 时的可读性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29904491/

相关文章:

git - Vagrant ansible git clone 权限错误

nginx - Vagrant 和 NGINX 仅适用于 80 以外的端口

shell - 抑制 Ansible 警告(至少对于 aptitude 安装)

nginx - 使用 Nginx + PHP-FPM 的 PHP 文件访问被拒绝 (403)

reactjs - 在 nginx 中使用 certbot 的问题

由于 ssl 证书,laravel 电子邮件验证在生产中不起作用

virtualbox - 做 vagrant up 时出错

linux - 在 bash 脚本中查找并替换字符串(从字符串列表中)

python - 继电器开关和控件:学习电子编程...

c - 如何在 Linux 中从 C 执行 shell 脚本?