我在 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