我在脚本中有这个部分:
cat <<EOF >> /etc/httpd/vhosts/$site$dom.conf
<VirtualHost *:80>
ServerName $site$dom
ServerAlias www.$site$dom
DocumentRoot /site/http/travel/itn
CustomLog logs/access_$site_log combined
DirectoryIndex default.php index.php index.html index.phtml index.cgi index.htm
ScriptAlias /awstats/ /usr/local/awstats/wwwroot/cgi-bin/
<Directory /site/http/travel/itn >
AllowOverride All
</Directory>
</VirtualHost>
EOF
行中:CustomLog logs/access_$site_log combined
解释器似乎将 _log
视为“$site”变量的一部分。 $site 变量是一个动态变量。
我该如何解决?我尝试使用 _$site\_
转义“_”,但它对我不起作用。
最佳答案
相反,使用:
${site}_log
对于 bash,使用 $var
或 ${var}
调用变量是一样的。但是如果您想处理这些情况,括号会非常方便。
另一个例子
$ myvar="hello"
$ echo "$myvar"
hello
$ echo "$myvar5"
$ echo "${myvar}5"
hello5
关于Bash:如何区分符号字符和变量名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21607122/