[英]How to write $something in a file with sudo tee with bash?
This is my bash script that write a custom nginx server block aka virtualhost. 这是我的bash脚本,用于编写自定义nginx服务器块(也称为virtualhost)。
sudo tee /etc/nginx/sites-available/master <<EOF
server {
listen 80;
server_name localhost;
root /home/capt/WebRoot;
location / {
index index.php;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 404 /index.php;
location ~ \.php$ {
include fastcgi_params;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
EOF
Everything was well written except where there is a $ as in this part: 一切都写得很好,除了在这部分有$的地方:
fastcgi_param PATH_INFO ;
fastcgi_param PATH_TRANSLATED ;
fastcgi_param SCRIPT_FILENAME ;
How to resolve this issue? 如何解决这个问题?
Just quote your EOF
: 只需引用您的
EOF
:
sudo tee /etc/nginx/sites-available/master <<'EOF'
BTW: anything with $
should be broken, not just the mentioned lines. 顺便说一句:带有
$
任何内容都应该被打破,而不仅仅是上述几行。
Example: 例:
#! /bin/bash
cat <<'EOF'
a=$1
EOF
cat <<EOF
a=$1
EOF
Execution: 执行:
bash test.sh
a=$1
a=
Escape each $
character with backslash \\
. 用反斜杠
\\
分隔每个$
字符。
So the three lines should look like this: 因此,三行应如下所示:
fastcgi_param PATH_INFO \$fastcgi_path_info;
fastcgi_param PATH_TRANSLATED \$document_root\$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
I guess there is also problem with (at least) $uri
earlier in the file which you probably missed. 我想您可能错过的文件中的(至少)
$uri
也存在问题。 So just precisely escape each occurance of $
. 因此,只需精确地避免
$
每次出现。
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
Since the words above beginning with $
are considered variables in bash, and are not yet initialized, they are empty. 由于以上以
$
开头的单词在bash中被视为变量,并且尚未初始化,因此它们为空。 You should expect exactly the output you get. 您应该确切期望得到的输出。 As provided, escape the
$
with \\$
and you will be fine. 由于提供,逃避
$
用\\$
,你会被罚款。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.