我在 bash 脚本中只有一个变量${PHP_V}
,并尝试传入 nginx 配置文件,例如:
cat <<'EOF' > /etc/nginx/sites-available/default
server {
listen 80 default_server;
listen [::]:80 default_server;
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
root /vagrant/webroot;
index index.php;
server_name _;
ssl_certificate /etc/nginx/certs/vagrantbox.crt;
ssl_certificate_key /etc/nginx/certs/vagrantbox.key;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/run/php/php${PHP_V}-fpm.sock;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
}
}
EOF
但没有成功。怎么做?
答案1
您需要取消引号EOF
:
如果引用了word的任何部分,则应通过对word进行去除引号来形成分隔符,并且此处文档行不会被扩展。否则,分隔符应为单词本身。
如果没有引用单词的任何部分,则应扩展此处文档的所有行以进行参数扩展、命令替换和算术扩展。
但请注意,您的此处文档中有几处内容需要单独转义以防止扩展:
cat <<EOF > /etc/nginx/sites-available/default
server {
listen 80 default_server;
listen [::]:80 default_server;
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
root /vagrant/webroot;
index index.php;
server_name _;
ssl_certificate /etc/nginx/certs/vagrantbox.crt;
ssl_certificate_key /etc/nginx/certs/vagrantbox.key;
location / {
try_files \$uri \$uri/ /index.php?\$args;
}
location ~ \.php\$ {
try_files \$uri =404;
include fastcgi_params;
fastcgi_pass unix:/run/php/php${PHP_V}-fpm.sock;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME
\$document_root\$fastcgi_script_name;
}
}
EOF