此处文件:无参数扩展 [英] Here document: no parameter expansion

查看:31
本文介绍了此处文件:无参数扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此文档中是否有任何方法可以关闭参数扩展?

Is there any way to turn off parameter expansion in here document?

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

我在analyze.sh中得到了这个:

I got this in analyze.sh:

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

推荐答案

您需要引用定界符:

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

这可防止外壳扩展here-document:

This prevents the shell from expanding the here-document:

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

文档

这在 man bash 中记录:

here-documents的格式为:

The format of here-documents is:

          <<[-]word
                  here-document
          delimiter

不对word执行任何参数和变量扩展,命令替换,算术扩展或路径名扩展.如果单词中的任何字符都被引用,分隔符是对word删除引号的结果,并且here-document中的行不会展开.如果未对word引用,则所有这里文档的行是经过参数扩展,命令替换和算术扩展后,字符序列\被忽略,和\必须用于引用字符\,$和`.[添加了重点]

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. 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 `. [Emphasis added]

这篇关于此处文件:无参数扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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