简体   繁体   English

此处文件:无参数扩展

[英]Here document: no parameter expansion

Is there any way to turn off parameter expansion in here document? 有什么办法可以关闭此处文件中的参数扩展?

Shell command: Shell命令:

$ cat > analyze.sh <<EOF    
awk -F: '{print $NF}' asr.log | sort > tmp  
awk -F '(' '{print $NF}' asr.log | awk '{print $1}' |  sort > tmp2
EOF

I got this in analyze.sh: 我在analytics.sh中得到了这个:

awk -F: '{print }' asr.log | sort > tmp
awk -F '(' '{print }' asr.log | awk '{print }' |  sort > tmp2

You need to quote the delimiter: 您需要引用定界符:

$ cat > analyze.sh <<"EOF"    
awk -F: '{print $NF}' asr.log | sort > tmp  
awk -F '(' '{print $NF}' asr.log | awk '{print $1}' |  sort > tmp2
EOF

This prevents the shell from expanding the here-document: 这样可以防止shell扩展here-document:

$ cat analyze.sh 
awk -F: '{print $NF}' asr.log | sort > tmp  
awk -F '(' '{print $NF}' asr.log | awk '{print $1}' |  sort > tmp2

Documentation 文献资料

This is documented in man bash : 这是在man bash

The format of here-documents is: 此处文档的格式为:

  <<[-]word here-document delimiter 

No parameter and variable 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. 如果在word中引用了任何字符,则分隔符是对word删除引用的结果,并且本文档中的行不会扩展。 If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion, the character sequence \\ is ignored, and \\ must be used to quote the characters \\, $, and `. 如果未用word引起引用,则本文档的所有行都将进行参数扩展,命令替换和算术扩展,字符序列\\将被忽略,并且\\必须用于引用字符\\,$和`。 [Emphasis added] [增加重点]

You can quote the EOF to disable expansion: 您可以引用EOF以禁用扩展:

cat > analyze.sh <<'EOF'
awk -F: '{print $NF}' asr.log | sort > tmp  
awk -F '(' '{print $NF}' asr.log | awk '{print $1}' |  sort > tmp2
EOF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM