如何避免定界符扩大变量? [英] How to avoid heredoc expanding variables?

查看:139
本文介绍了如何避免定界符扩大变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想利用ENV替换字符串创建一个脚本文件,但也希望prevent一些逃逸

 出口PLACEHOLDER1 =myPlaceholder1Value
出口PLACEHOLDER2 =myPlaceholder2Value
须藤/斌/苏-c猫<<&EOF GT; /etc/init.d/my-script
#!/斌/庆典

### BEGIN INIT INFO
#提供:我的脚本
#所需的启动:\\ $ remote_fs \\ $系统日志
#所需-停止:\\ $ remote_fs \\ $系统日志
#应该启动:\\ $网络\\ $时间
#应该-停止:\\ $网络\\ $时间
#在Default-Start:2 3 4 5
#默认-停止:0 1 6
#短说明:布拉布拉
#说明:喇嘛喇嘛递减

### END INIT INFO

myvariable_1 = TOTO $ PLACEHOLDER1
myvariable_2 = $蒂蒂PLACEHOLDER2
myvariable_final = \\dynamicvar = \\ $ {} myvariable_1,\\ $ {myvariable_2} \\
EOF

这导致这是不好的 myvariable_final 未逃脱,从初始化脚本依赖的一个取代($ remote_fs,$系统日志,$网络,$时间)

 #!/斌/庆典

### BEGIN INIT INFO
#提供:我的脚本
#所需的启动:
#所需-停止:
#应该启动:
#应该-停止:
#在Default-Start:2 3 4 5
#默认-停止:0 1 6
#短说明:布拉布拉
#说明:喇嘛喇嘛递减

### END INIT INFO

myvariable_1 = totomyPlaceholder1Value
myvariable_2 = titimyPlaceholder2Value
myvariable_final =dynamicvar =


如果我试图把一个反斜杠 \\ 的美元背后 $ ,我设法避免替代,但我得到一个不必要的反斜线 \\

 出口PLACEHOLDER1 =myPlaceholder1Value
出口PLACEHOLDER2 =myPlaceholder2Value
须藤/斌/苏-c猫<<&EOF GT; /etc/init.d/my-script
#!/斌/庆典

### BEGIN INIT INFO
#提供:我的脚本
#所需的启动:\\ $ \\ remote_fs \\ $ \\系统日志
#所需-停止:\\ $ remote_fs \\ $系统日志
#应该启动:\\ $网络\\ $时间
#应该-停止:\\ $网络\\ $时间
#在Default-Start:2 3 4 5
#默认-停止:0 1 6
#短说明:布拉布拉
#说明:喇嘛喇嘛递减

### END INIT INFO

myvariable_1 = TOTO $ PLACEHOLDER1
myvariable_2 = $蒂蒂PLACEHOLDER2
myvariable_final = \\dynamicvar = \\ $ \\ {} myvariable_1,\\ $ \\ {myvariable_2} \\
EOF

结果:

 #!/斌/庆典

### BEGIN INIT INFO
#提供:我的脚本
#所需的启动:$ \\ remote_fs $ \\系统日志
#所需-停止:
#应该启动:
#应该-停止:
#在Default-Start:2 3 4 5
#默认-停止:0 1 6
#短说明:布拉布拉
#说明:喇嘛喇嘛递减

### END INIT INFO

myvariable_1 = totomyPlaceholder1Value
myvariable_2 = titimyPlaceholder2Value
myvariable_final =dynamicvar = $ \\ {} myvariable_1,$ \\ {} myvariable_2


诚征/出席结果对子级一直

 #!/斌/庆典

### BEGIN INIT INFO
#提供:我的脚本
#所需的启动:$ remote_fs $系统日志
#所需-停止:$ remote_fs $系统日志
#应该启动:$ $网络时间
#应该-停止:$ $网络时间
#在Default-Start:2 3 4 5
#默认-停止:0 1 6
#短说明:布拉布拉
#说明:喇嘛喇嘛递减

### END INIT INFO

myvariable_1 = totomyPlaceholder1Value
myvariable_2 = titimyPlaceholder2Value
myvariable_final =dynamicvar = $ {} myvariable_1 $ {} myvariable_2

通过把报价解决各地在 EOF 下面,用反斜杠来控制逸出需要时

 出口PLACEHOLDER1 =myPlaceholder1Value
出口PLACEHOLDER2 =myPlaceholder2Value
须藤/斌/苏-c猫<<'EOF'> /etc/init.d/my-script
#!/斌/庆典

### BEGIN INIT INFO
#提供:我的脚本
#所需的启动:\\ $ remote_fs \\ $系统日志
#所需-停止:\\ $ remote_fs \\ $系统日志
#应该启动:\\ $网络\\ $时间
#应该-停止:\\ $网络\\ $时间
#在Default-Start:2 3 4 5
#默认-停止:0 1 6
#短说明:布拉布拉
#说明:喇嘛喇嘛递减

### END INIT INFO

myvariable_1 = TOTO $ PLACEHOLDER1
myvariable_2 = $蒂蒂PLACEHOLDER2
myvariable_final = \\dynamicvar = \\ $ {} myvariable_1,\\ $ {myvariable_2} \\
EOF


解决方案

只需使用'EOF'来prevent扩大变量:

 须藤/斌/苏-c猫<<'EOF'> /etc/init.d/my-script
                        ^^

男人庆典


  

这里的文件


  
  

这类型的重定向指示shell从读取输入
  电流源,直到只包含分隔符线(无
  尾随空白)看到。所有的线读取到该点的
  然后用作命令的标准输入


  
  

下面的文档的格式为:

 << [ - ]字
              在这里,文件
      定界符

没有参数扩展,命令替换,算术扩展,
  或者路径扩展是在执行字。如果word中任何字符
  被引用,分隔符是引用的去除对字的结果,和
  此处文档中的行不会被扩展。 如果单词
  不带引号,此处文档中的所有行受到参数
  扩展,命令替换和算术扩展
。在里面
  后者的情况下,字符序列\\被忽略,和\\
  必须使用引用字符\\,$,和`。


I'm trying to create a script file using substitution string from ENV but want also to prevent some from escaping

export PLACEHOLDER1="myPlaceholder1Value"
export PLACEHOLDER2="myPlaceholder2Value"
sudo /bin/su -c "cat << EOF > /etc/init.d/my-script
#!/bin/bash
#
### BEGIN INIT INFO
# Provides:          my-script
# Required-Start:    \$remote_fs \$syslog
# Required-Stop:     \$remote_fs \$syslog
# Should-Start:      \$network \$time
# Should-Stop:       \$network \$time
# Default-Start:     2 3 4 5 
# Default-Stop:      0 1 6
# Short-Description: blabla
# Description:       bla bla desc
#
### END INIT INFO
#
myvariable_1=toto$PLACEHOLDER1
myvariable_2=titi$PLACEHOLDER2
myvariable_final=\"dynamicvar=\${myvariable_1},\${myvariable_2}\"
EOF
"

It results in which is not good as the myvariable_final are not escaped and substituted as the one from the init script dependencies ($remote_fs, $syslog, $network, $time)

#!/bin/bash
#
### BEGIN INIT INFO
# Provides:          my-script
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: blabla
# Description:       bla bla desc
#
### END INIT INFO
#
myvariable_1=totomyPlaceholder1Value
myvariable_2=titimyPlaceholder2Value
myvariable_final="dynamicvar=,"


If i try to put a backslash \ behind the dollars $, I manage to avoid the substitution but I getting an unwanted backslash \:

export PLACEHOLDER1="myPlaceholder1Value"
export PLACEHOLDER2="myPlaceholder2Value"
sudo /bin/su -c "cat << EOF > /etc/init.d/my-script
#!/bin/bash
#
### BEGIN INIT INFO
# Provides:          my-script
# Required-Start:    \$\remote_fs \$\syslog
# Required-Stop:     \$remote_fs \$syslog
# Should-Start:      \$network \$time
# Should-Stop:       \$network \$time
# Default-Start:     2 3 4 5 
# Default-Stop:      0 1 6
# Short-Description: blabla
# Description:       bla bla desc
#
### END INIT INFO
#
myvariable_1=toto$PLACEHOLDER1
myvariable_2=titi$PLACEHOLDER2
myvariable_final=\"dynamicvar=\$\{myvariable_1},\$\{myvariable_2}\"
EOF
"

results in:

#!/bin/bash
#
### BEGIN INIT INFO
# Provides:          my-script
# Required-Start:    $\remote_fs $\syslog
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: blabla
# Description:       bla bla desc
#
### END INIT INFO
#
myvariable_1=totomyPlaceholder1Value
myvariable_2=titimyPlaceholder2Value
myvariable_final="dynamicvar=$\{myvariable_1},$\{myvariable_2}"


Wanted/attended result whould have been :

#!/bin/bash
#
### BEGIN INIT INFO
# Provides:          my-script
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Should-Start:      $network $time
# Should-Stop:       $network $time
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: blabla
# Description:       bla bla desc
#
### END INIT INFO
#
myvariable_1=totomyPlaceholder1Value
myvariable_2=titimyPlaceholder2Value
myvariable_final="dynamicvar=${myvariable_1},${myvariable_2}"

solved by putting quote around the EOF as below and using backslash to control the escaping when needed

export PLACEHOLDER1="myPlaceholder1Value"
export PLACEHOLDER2="myPlaceholder2Value"
sudo /bin/su -c "cat << 'EOF' > /etc/init.d/my-script
#!/bin/bash
#
### BEGIN INIT INFO
# Provides:          my-script
# Required-Start:    \$remote_fs \$syslog
# Required-Stop:     \$remote_fs \$syslog
# Should-Start:      \$network \$time
# Should-Stop:       \$network \$time
# Default-Start:     2 3 4 5 
# Default-Stop:      0 1 6
# Short-Description: blabla
# Description:       bla bla desc
#
### END INIT INFO
#
myvariable_1=toto$PLACEHOLDER1
myvariable_2=titi$PLACEHOLDER2
myvariable_final=\"dynamicvar=\${myvariable_1},\${myvariable_2}\"
EOF
"

解决方案

Just use 'EOF' to prevent the variable for expanding:

sudo /bin/su -c "cat << 'EOF' > /etc/init.d/my-script
                        ^   ^

From man bash:

Here Documents

This type of redirection instructs the shell to read input from the current source until a line containing only delimiter (with no trailing blanks) is seen. All of the lines read up to that point are then used as the standard input for a command.

The format of here-documents is:

      <<[-]word
              here-document
      delimiter

No parameter expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word. If any characters in word are quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion. In the latter case, the character sequence \ is ignored, and \ must be used to quote the characters \, $, and `.

这篇关于如何避免定界符扩大变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆