Dar*_*tue 3 bash shell-script cat here-document variable
我正在做一个脚本来自动安装一些东西,包括一个 nginx web 服务器。
我正在创建这样的 nginx conf 文件:
cat >/etc/nginx/sites-available/bookstack.conf <<EOL
server {
listen 443 ssl;
listen [::]:443 ssl;
ssl_certificate /etc/nginx/certificats/domain.tld.crt;
ssl_certificate_key /etc/nginx/certificats/domain.tld.key;
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
server_name domain.tld;
root /path/to/bookstack/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_index index.php;
try_files $uri =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
}
server {
listen 80;
listen [::]:80;
server_name domain.tld;
return 302 https://$server_name$request_uri;
}
EOL
Run Code Online (Sandbox Code Playgroud)
我的问题是 cat 将 $ 字符作为变量名(这通常是有意义的)并且没有将我想要的内容放入我的.conf文件中。这是脚本完成工作后我的.conf文件的示例:
location / {
try_files / /index.php?;
}
location ~ \.php$ {
fastcgi_index index.php;
try_files $uri =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME ;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
Run Code Online (Sandbox Code Playgroud)
请问如何防止这种行为?我尝试使用引号,但没有用。
谢谢 :)
如果您不想在 here 文档中进行任何替换(<<EOL构造的输入),请在分隔符规范中使用引号。任何引用都可以:您可以写<<'EOL'(推荐)<<\EOL、<<E''OL、 等,甚至(令人困惑地)<<"EOL"。
$ cat <<'EOL'
> $foo
> EOL
$foo
Run Code Online (Sandbox Code Playgroud)
如果您有时想扩展变量和命令替换,请<<EOL在您不想扩展的每个特殊字符之前保留并放置一个反斜杠。需要转义的特殊字符是$\`.
请参阅有关此处文档的 bash 手册。这也适用于普通的 sh。