bash 脚本将内容写入文件。文件内容需要 bash 变量。怎么做?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17915797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:02:27  来源:igfitidea点击:

bash script to write content into file. File content requires bash variables. How to do that?

bashunixfile-io

提问by Kim Stacks

The following is a piece of content my bash script will write into the nginx configuration file.

下面是我的bash脚本将写入nginx配置文件的一段内容。

WEBAPP=example.com
APPFOLDER=abcapp
CONFFILENAME=abc.conf

read -r -d '' FILECONTENT <<'ENDFILECONTENT'
server {
        listen 80;
        client_max_body_size 2M;
        server_name $WEBAPP;
        root /var/virtual/stage.$WEBAPP/current/src/$APPFOLDER/webroot;
}

ENDFILECONTENT
echo "$FILECONTENT" > /etc/nginx/sites-available/$CONFFILENAME

The code works successfully to write the content inside /etc/nginx/sites-available/abc.conf.

该代码成功地将内容写入其中/etc/nginx/sites-available/abc.conf

However, I have two bash variables in $WEBAPPand $APPFOLDER. They are manifested exactly like that inside the file instead of example.comand abcappwhich is what I intended.

但是,我在$WEBAPP和 中有两个 bash 变量$APPFOLDER。他们表现完全一样的文件,而不是内部example.comabcapp这正是我打算。

How do I make the script work as intended?

如何使脚本按预期工作?

回答by rici

bash allows newlines in quoted strings. It does parameter replacement (that is, $FOOgets replaced with the value of FOO) inside double-quoted strings ("$FOO") but not inside single-quoted strings ('$FOO').

bash 允许在带引号的字符串中换行。它在双引号字符串 ( ) 内而不是在单引号字符串 ( )内进行参数替换(即被$FOO替换为 的值)。FOO"$FOO"'$FOO'

So you could just do this:

所以你可以这样做:

FILECONTENT="server {
        listen 80;
        client_max_body_size 2M;
        server_name $WEBAPP;
        root /var/virtual/stage.$WEBAPP/current/src/$APPFOLDER/webroot;
}"
echo "$FILECONTENT" > /etc/nginx/sites-available/$CONFFILENAME

You don't really need the FILECONTENTparameter, since you could just copy directly to the file:

您实际上并不需要该FILECONTENT参数,因为您可以直接复制到文件中:

cat >/etc/nginx/sites-available/$CONFFILENAME <<ENDOFCONTENT
server {
        listen 80;
        client_max_body_size 2M;
        server_name $WEBAPP;
        root /var/virtual/stage.$WEBAPP/current/src/$APPFOLDER/webroot;
}
ENDOFCONTENT

In the second example, using <<ENDOFCONTENTindicates that the $VARs should be replaced with their values <<'ENDOFCONTENT'would prevent the parameter replacement.

在第二个示例中, using<<ENDOFCONTENT指示$VAR应该用它们的值替换 s<<'ENDOFCONTENT'将阻止参数替换。

回答by Jeff Bowman

You're actually deliberately turning off parameter subsitution by enclosing 'ENDFILECONTENT'in quotes. See this excerpt from example 19-7 of the advanced Bash scripting guide on Heredocs, slightly reformatted:

您实际上是通过'ENDFILECONTENT'用引号引起来故意关闭参数替换。请参阅Heredocs的高级 Bash 脚本指南的示例 19-7 的摘录,稍微重新格式化:

# No parameter substitution when the "limit string" is quoted or escaped.
# Either of the following at the head of the here document would have
# the same effect.
#
# cat <<"Endofmessage"
# cat <<\Endofmessage

Remove the single quotes around 'ENDFILECONTENT'and BASH will replace the variables as expected.

删除周围的单引号'ENDFILECONTENT',BASH 将按预期替换变量。